Skip to content

Define a family of algorithms, put each of them in a separate class, and make their objects interchangeable

Notifications You must be signed in to change notification settings

design-pattern-list/strategy-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Strategy Pattern

Type

The Strategy Pattern is a behavioral pattern.

Description

The strategy design pattern gives you the ability to define a family of algorithms, put each of them in a separate class, and make their objects interchangeable.

Problem

class ShoppingCart {
    
    public void payUsingPayPal(String username, String password) {
        // Implementation
    }
    
    public void payUsingBancontact(String pinCode) {
        // Implementation
    }

   // ...
}

Solution

interface PaymentStrategy {
    void pay(double amount);
}

class PayPalPaymentStrategy implements PaymentStrategy {
    
    @Override
    public void pay(double amount) {
        // Implementation
    }
}

class ShoppingCart {
    
    // PayPalPaymentStrategy
    // BancontactPaymentStrategy
    // ...

    public void pay(PaymentStrategy strategy) {
        strategy.pay(1234);
    }
}

Use

  1. When you need to have x types of functions that have the same primary goal but behave differently.

Prevent

  1. From creating x types of different functions that have the same goal.

References

  1. https://refactoring.guru/design-patterns/strategy

About

Define a family of algorithms, put each of them in a separate class, and make their objects interchangeable

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages