Member-only story
State Design Pattern
The state pattern is a behavioral pattern that allows an object to change its behavior at runtime. here, “state” means the set of data that describes how a given object should behave at a given time.
If we have a product and they have some state then we shall use this pattern.
2 (ss) — We shall mention all possible operation in the protocol for a product(like op1, op2.. so on).
3,4, and 5 are state, like state1 required op1 then mention logic over ther and leave it all with exception error or with some msg and similar for state2 required op2 then write logic for op2 and so on and each state holds product so that we can change the product state.
This pattern involves three types:
- The context is the object that has a current state and whose behavior changes.
- The state protocol defines required methods and properties. Developers commonly substitute a base state class in place of a protocol. By doing so, they can define stored properties in the base, which isn’t possible using a protocol.
- Concrete states conform to the state protocol, or if a base class is used instead, they subclass the…