Friday, December 5, 2014

Design Pattern : State Pattern


Type : Behavioral Design Pattern

Summary : The State Design Pattern is an behavioral design pattern. The essence  of  State pattern is that the behavior of an Object Changes based on its state. For instance the behavior of Light changes based on it's state (Turned On, Turned Off etc)

Advantages: 

Reduces If/Else , Switch Conditional statements.
Context Class gets it's behavior by delegating to State Object instances.
Complex Logic can be delegated to specific State Objects.

Example:

A good example of State Pattern would be of  Car. Car has different behavior like (speed, acceleration etc) based on its state. Car could have different States like  running, parked etc.

Class Diagram



No comments :

Post a Comment