State and Strategy Design Pattern Presentation

JimM45 36 views 20 slides Jun 24, 2024
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

It is a presentation slide on state and strategy design pattern. It includes theory, examlpe and cpp implementation of the given examples.


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

UML Diagram DeliveryState +getState(): void +trackLocation(): void +cancelDelivery(): void PendingState +getStatus(): void +cancelDelivery(): void ShippedState +getStatus(): void +trackLocation(): void DeliveredState +getStatus(): void Delivery +Delivery: void +setState(DeliveryState *newState) +showCurrentStatus(): void +trackCurrentLocation(): void +cancelDelivery(): void interface context concrete states

Implementation in C++

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 ; } };

Context class Delivery { private : DeliveryState *currentState; public : Delivery(){ currentState = new PendingState(); } void setState (DeliveryState *newState) { delete currentState; currentState = newState; } void showCurrentStatus () { currentState->getStatus(); } . . . . . . . void trackCurrentLocation () { currentState->trackLocation(); } void cancelDelivery () { currentState->cancelDelivery(); delete currentState; currentState = nullptr ; } };

Strategy Design Pattern

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

UML Diagram PaymentStrategy + processPayment(): void CardPayment + processPayment(): void MobilePayment + processPayment(): void BankPayment + processPayment(): void paymentProcessor Strategy: paymentStrategy +setPaymentMethod(): void +processPayment():void Strategy Interface Context Concrete Strategies

Implementation in C++

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.

THANK YOU
Tags