It is a presentation slide on state and strategy design pattern. It includes theory, examlpe and cpp implementation of the given examples.
Size: 1.58 MB
Language: en
Added: Jun 24, 2024
Slides: 20 pages
Slide Content
State & Strategy Design Pattern
State Design Pattern What is State Pattern The State Design Pattern is a behavioral design pattern that allows an object to change its behavior when its internal state changes. It changes the behaviour of the object at runtime depending on the state
Why Important Clear State Management : Enables clear representation and management of different states. Simplifies Transition Logic : Reduces complex conditional statements for state transitions. Improves Maintainability : Separates concerns, making maintenance and modifications easier. Promotes Clean Code : Facilitates writing clean, modular, and organized code.
State : Provide the interface/abstract class for all concrete states. Concrete State : Implements the State interface. Provides specific behavior associated with a particular state. Context : Maintains a reference to the current state. Provides an interface to interact with state and allow state transitions. Components
Status: Pending Location: Not available Status: Shipped Location: In transit Status: Delivered Location: At Destination PendingState ShippedState DeliveredState Delivery Management System
State Interface // State interface class DeliveryState { public : virtual void getStatus () = ; virtual void trackLocation () { cout << "Tracking is not available at this state << endl ; } virtual void cancelDelivery () { cout << "Delivery can’t be canceled at this state" << endl ; } };
Concrete States // PendingState class PendingState : public DeliveryState { public : void getStatus () override { cout << "Pending" << endl ; } void cancelDelivery () override { cout << "Cancelled" << endl ; } }; // ShippedState class ShippedState : public DeliveryState { public : void getStatus () override { cout << "Shipped" << endl ; } void trackLocation () override { cout << "In transit" << endl ; } }; // DeliveredState class DeliveredState : public DeliveryState { public : void getStatus () override { cout << "Delivered" << endl ; } void trackLocation () override { cout << "AtDestination" << endl ; } };
The Strategy design pattern is a behavioral design pattern in software development that defines a family of algorithms,encapsulates each of them individually and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. What is Strategy?
Components of Strategy Design Pattern Context: Contains a reference to a strategy object. May define an interface that lets strategy accessing its data. Strategy: Defines an interface or an abstract class to all supported algorithms . Context class uses this interface to call the algorithm defined by a concrete strategy. Concrete Strategy: Each concrete strategy implements an algorithm. Each concrete strategy provides a specific behavior.
Real Life Example Card Payment Mobile Payment Bank Payment
Strategy Interface class paymentStrategy { public : virtual void processPayment ( int amount) = ; }; class CardPayment : public paymentStrategy { void processPayment( int amount) { cout<< "processing card payment of amount" <<amount<<endl; } }; class MobilePayment : public paymentStrategy { void processPayment( int amount) { cout<< "processing mobile payment of amount" <<amount<<endl; } }; class BankPayment : public paymentStrategy { void processPayment( int amount) { cout<< "processing bank payment of amount" <<amount<<endl; } }; Concrete Strategies
Context class PaymentProcessor { private : paymentStrategy *strategy; public : void setPaymentMethod (paymentStrategy *strategy) { this ->strategy = strategy; } void processPayment ( int amount) { if (strategy){ strategy->processPayment(amount); } else cout << "select payment method first" << endl ; } };
Differences Between State & Strategy Aspect State Pattern Strategy Pattern Purpose Allows an object to alter its behavior when its internal state changes. Create distinct implementations to accomplish a single operation. Components Context, State Interface, Concrete States. Context, Strategy Interface, Concrete Strategies. Transition Order of state transition is well-defined. The client can freely choose and switch strategies. Dependency States are dependent on each other. Strategies are not dependent on each other.