0% found this document useful (0 votes)
25 views19 pages

State and Singleton Design Pattern

The document discusses the state design pattern, which allows an object to change its behavior when its internal state changes, making it appear as if its class has changed. The state pattern is useful when an object can be in several states with different behaviors in each state. It is implemented with a "context" class representing the interface, an abstract "state" class defining possible states, and derived "state" classes defining the true nature of each state. The context maintains a pointer to the current state, and changing this pointer changes the object's state and behavior. An ATM machine is provided as a real-world example.

Uploaded by

Saad Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views19 pages

State and Singleton Design Pattern

The document discusses the state design pattern, which allows an object to change its behavior when its internal state changes, making it appear as if its class has changed. The state pattern is useful when an object can be in several states with different behaviors in each state. It is implemented with a "context" class representing the interface, an abstract "state" class defining possible states, and derived "state" classes defining the true nature of each state. The context maintains a pointer to the current state, and changing this pointer changes the object's state and behavior. An ATM machine is provided as a real-world example.

Uploaded by

Saad Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

DESIGN PATTERN

State and Singleton Design Pattern


Design Pattern

■ In software engineering, a design pattern is a general repeatable solution to a


commonly occurring problem in software design.
■ A design pattern isn't a finished design that can be transformed directly into code. It
is a description or template for how to solve a problem that can be used in many
different situations.
Types of Design Pattern

■ Creational Patterns
■ Structural Patterns
■ Behavioral Patterns
Behavioral Patterns

■ In software engineering, behavioral design patterns are design patterns that identify
common communication patterns between objects and realize these patterns. By
doing so, these patterns increase flexibility in carrying out this communication.
Types of Behavioral Patterns

■ Chain of responsibility
■ Command
■ Interpreter
■ Iterator
■ Mediator
■ Memento
■ Null Object
■ Observer
■ State
■ Strategy
■ Template Method
■ Visitor
STATE DESIGN
PATTERN
Intent

■ Type of Behavioral Design Pattern


■ Allow an object to alter its behavior when its internal state changes. The object will
appear to change its class.
When to use State Pattern?

■ State pattern is useful when there is an


object that can be in of several states with
different behavior in each state
How is State Pattern implemented?

■ “Context” Class: Represents the interface to the outside world


■ “State” Abstract class: Base class which defines the different states of the state
machine.
■ “Derived” classes from the state class: Defines the true nature of the state machine
can be in
■ Context class maintains the pointer to the current state. To change the state of the
state machine, the pointer need’s to be changed.
Class Diagram
Real Time
example

ATM Machine
Advantages

■ New state can be added easily by defining a new subclass


■ Chances of error are less
■ Easily maintainable and flexible
Now we move on to its example code
THANKS

You might also like