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