1 / 11

Behavioral Pattern: State

Behavioral Pattern: State. Chapter 5 – Page 194. Many programmers have had the experience of creating a class which performs different computations or displays different information based on the arguments passed into the class.

kamin
Download Presentation

Behavioral Pattern: State

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Behavioral Pattern: State Chapter 5 – Page 194 Many programmers have had the experience of creating a class which performs different computations or displays different information based on the arguments passed into the class. This frequently leads to inelegant switch or if-else statements inside the class that determine which behavior to carry out. The State Pattern handles this problem by making an object appear to change class in order to facilitate the desired change in behavior, by isolating the divergent functionality into swappable classes that implement a common interface.

  2. The State Pattern Chapter 5 – Page 195 • The Context defines the interface of interest to clients, maintaining an instance of a ConcreteState subclass that defines the current state. • The State defines an interface for encapsulating the behavior associated with a particular state of the Context. • Each ConcreteState subclass implements a behavior associated with a state of the Context. • When a state transition is needed, the Context merely replaces its internal state member with a different ConcreteState object.

  3. Non-Software Example: Vending Chapter 5 – Page 196 Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is made, a vending machine will either deliver a product and change, deliver a product and no change, deliver no product due to an insufficient deposit, or deliver no product due to inventory depletion.

  4. Software Example: E-Commerce Chapter 5 – Page 197 The ECommerceContext controls the state of an e-commerce application, transitioning from an initial state to a merchandise selection state or to the exit state. The application can remain in the selection state or transition to a checkout state or the exit state. The checkout state leads directly to the exit state.

  5. E-Commerce State Code in C++ Chapter 5 – Page 198 #include <iostream> #include <string> using namespace std; classECommerceContext; class State { public: virtual string whatCanWeDo(); protected: explicit State() {} voidChangeState(ECommerceContext* enginePtr, State* statePtr); }; classInitialState : public State { public: staticInitialState* Instance() { return &initState; } virtual string whatCanWeDo(); explicitInitialState() {} private: staticInitialStateinitState; };

  6. Chapter 5 – Page 199 classSelectionState : public State { public: staticSelectionState* Instance() { return &selectState; } virtual string whatCanWeDo(); explicitSelectionState() {} private: staticSelectionStateselectState; }; classCheckoutState : public State { public: staticCheckoutState* Instance() { return &checkState; } virtual string whatCanWeDo(); explicitCheckoutState() {} private: staticCheckoutStatecheckState; }; classExitState : public State { public: staticExitState* Instance() { return &exitState; } virtual string whatCanWeDo(); explicitExitState() {} private: staticExitStateexitState; };

  7. Chapter 5 – Page 200 classECommerceContext { public: explicitECommerceContext(); virtual ~ECommerceContext() {} friend class State; voidchangeState(State* pState); stringgetStateCapabilities(); private: State* state; }; ECommerceContext::ECommerceContext() { state = NULL; changeState(InitialState::Instance()); } voidECommerceContext::changeState(State* pState) { if (pState != state) state = pState; } string ECommerceContext::getStateCapabilities() { return state->whatCanWeDo(); }

  8. Chapter 5 – Page 201 string State::whatCanWeDo() { return"Nothing\n"; } void State::ChangeState(ECommerceContext* enginePtr, State* statePtr) { enginePtr->changeState(statePtr); } string InitialState::whatCanWeDo() { return"Capabilities: Select merchandise or exit site.\n\n"; } string SelectionState::whatCanWeDo() { return"Capabilities: Selecting merchandise, go to checkout, or exit site.\n\n"; } string CheckoutState::whatCanWeDo() { return"Capabilities: Pay for merchandise or exit site.\n\n"; } string ExitState::whatCanWeDo() { return"Capabilities: Exit site.\n\n"; } InitialStateInitialState::initState; SelectionStateSelectionState::selectState; CheckoutStateCheckoutState::checkState; ExitStateExitState::exitState;

  9. Chapter 5 – Page 202 void main() { ECommerceContext* myECommerceContext = newECommerceContext(); cout << "Entering Site (Initial State)\n"; cout << myECommerceContext->getStateCapabilities(); cout << "Time to shop!\n\n"; for (int i = 0; i< 3; i++) { myECommerceContext->changeState(SelectionState::Instance()); cout << "Shopping (Selection State)\n"; cout << myECommerceContext->getStateCapabilities(); } cout << "Time to pay!\n\n"; myECommerceContext->changeState(CheckoutState::Instance()); cout << "Paying (Checkout State)\n"; cout << myECommerceContext->getStateCapabilities(); cout << "Time to leave!\n\n"; myECommerceContext->changeState(ExitState::Instance()); cout << "Now in the exit state\n"; cout << myECommerceContext->getStateCapabilities(); deletemyECommerceContext; return; }

  10. Chapter 5 – Page 203

  11. State Pattern Advantages Chapter 5 – Page 204 • The State pattern localizes state-specific behavior in an individual class for each state, and puts all the behavior for that state in a single object, eliminating the necessity for a set of long, look-alike conditional statements scattered through the program’s code. • Using separate objects for separate states makes state transitions explicit rather than using internal data values to define those transitions in one combined object. • Extending the behavior is just a matter of defining additional state classes and modifying the linkages between the states, which translates well to diagrammatic representations of active objects (i.e., state transition diagrams).

More Related