Graphic Design Patterns (11) Strategy Patterns

Keywords: less

I. Application scenarios

Shopping malls adopt different discount strategies for different customers.

Different construction methods are used for different topography and landforms, and different algorithms are selected for different batch data processing.

For commonly used

    if() {

    }else if(){

    }else if(){

    }else{}

Structures can be reconstructed using strategic patterns. When a new mode is added, the original code does not need to be modified, which conforms to the open-close principle.

It can separate algorithm from architecture.

 

II. Specific Realization

  • Implementing Points

1. A Strategy interface in which the method to be implemented is defined.

2. Several specific Strategy classes, implements interface, implement different algorithms in them.

3. A context class Context, which contains Strategy objects, can choose different specific policy classes to handle business.

  • Code implementation

Take supermarket discount strategy as an example, new customers buy less than no discount; new customers buy more than 9 discount; old customers buy less than 9 discount; old customers buy more than 8 discount.

1. Strategy interface, specifying the method to be implemented.

public interface Strategy {
    double getPrice(double originalPrice);
}

2. Different strategies, implements interface

public class NewCustomerFew implements Strategy {

    @Override
    public double getPrice(double originalPrice) {
        System.out.println("No discount for new customers!");
        return originalPrice;
    }
}

class NewCustomerMany implements Strategy {

    @Override
    public double getPrice(double originalPrice) {
        System.out.println("What a 10% discount for new customers!");
        return originalPrice * 0.9;
    }
}

class OldCustomerFew implements Strategy {

    @Override
    public double getPrice(double originalPrice) {
        System.out.println("Old customers buy less than 10% discount!");
        return originalPrice * 0.9;
    }
}

class OldCustomerMany implements Strategy {

    @Override
    public double getPrice(double originalPrice) {
        System.out.println("Old customers buy more than 20% discount!");
        return originalPrice * 0.8;
    }
}

3. Context class, containing policy objects.

public class Context {
    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }
    
    public double getPrice(double originalPrice){
        return this.strategy.getPrice(originalPrice);
    }
}

Test class

public class ClientTest {

    public static void main(String[] args) {
        Context context = new Context(new NewCustomerFew()); //Less New Customers Buy
        System.out.println(context.getPrice(100));
        
        context.setStrategy(new OldCustomerMany());  //Old customers buy too much
        System.out.println(context.getPrice(999));
    }
}

test result

Posted by nickminute on Mon, 07 Oct 2019 06:46:28 -0700