Basically this is the easiest pattern to create thus far. It offers a lot of versatility and malleability. All that is nice, but how do we create it!!?? VERY good question! Follow these steps:
- Decide on the type of actions that are going to be grouped together (different states something can be it, be it emotions, labor duties, opp-orations that can be performed)
- Create the State interface that will connect them all (just like the strategy pattern)
- Inside the State interface include the methods that will be used to change between states
- Create separate classes for each State the object will be in, and implement the State interface
- In each class now specify how each of these methods will change to certain states and such forth
There you have it, that's how you use the State pattern. It is that simple. You can make your methods as complicated as you like, but this is the basic structure.
No comments:
Post a Comment