The strategic model of behavior model

Posted by mezise on Sun, 24 May 2020 11:46:50 +0200

1 General

Strategy pattern is a kind of behavior pattern with low complexity. When there are many ways to complete a task, you can consider using strategy pattern.

2. Strategic mode

In the strategy mode, we need to define a family of algorithms and put them into independent classes, each of which represents a method to complete the task. For example, calculator, we can define the basic addition, subtraction, multiplication, and divide four strategies. For different numbers of two numbers that are entered, we will get different results. For example, online shopping settlement, we can define a series of payment strategies, Alipay payment, WeChat payment, credit card payment, value card payment... Customers can choose corresponding according to their own preferences. Method of payment.

The strategy pattern decouples the implementation and definition of the algorithm. The new strategy does not need to modify the existing logic and conforms to the opening and closing principle.

3 cases

Use a case to illustrate. For office workers, there are many ways to go to work, including driving, cycling and public transportation. Different people have different choices, even for the same person, today's choices and tomorrow's choices will be different. If the way to work is maintained in the class of office workers, the logic will be more complex. Let's see how to simplify the code logic by separating the way to work through the policy mode:

public interface TransportationStrategy {
    void execute();
}

public class BikeStrategy implements TransportationStrategy {
    private String bike;
    BikeStrategy(String bike) {
        this.bike = bike;
    };
    [@Override](https://my.oschina.net/u/1162528)
    public void execute() {
        System.out.println("Riding the '" + bike + "'bike...");
    }
}

public class BusStrategy implements TransportationStrategy {
    private String trafficCard;
    BusStrategy(String trafficCard) {
        this.trafficCard = trafficCard;
    };
    [@Override](https://my.oschina.net/u/1162528)
    public void execute() {
        System.out.println("Taking the bus with traffic card '" + trafficCard + "'...");
    }
}

public class CarStrategy implements TransportationStrategy {
    private String car;
    CarStrategy(String car) {
        this.car = car;
    };
    [@Override](https://my.oschina.net/u/1162528)
    public void execute() {
        System.out.println("Driving the '" + car + "'car...");
    }
}

public class Worker {
    private String name;
    Worker(String name) {
        this.name = name;
    };
    public void goToWork(TransportationStrategy strategy) {
        strategy.execute();
        System.out.println(name + " is on the way to office.");
    }
}

public class Test {
    public static void main(String[] args) {
        TransportationStrategy BusStrategy = new BusStrategy("NO.9382-2345");
        Worker link = new Worker("Link");
        link.goToWork(BusStrategy);

        TransportationStrategy bikeStrategy = new BikeStrategy("Giant");
        Worker mario = new Worker("Mario");
        mario.goToWork(bikeStrategy);

        TransportationStrategy carStrategy = new BusStrategy("Tesla");
        Worker yoshi = new Worker("Yoshi");
        yoshi.goToWork(carStrategy);
    }
}

Output:

Taking the bus with traffic card 'NO.9382-2345'...
Link is on the way to office.
Riding the 'Giant'bike...
Mario is on the way to office.
Taking the bus with traffic card 'Tesla'...
Yoshi is on the way to office.

By defining an abstract layer of transportation strategy, the code structure is clear and the coupling between components is low. Select different strategy classes to complete the corresponding functions.

Our most familiar example should be the one in JDK Collections.sort() Method, this method can sort the List by different algorithms according to the different comparators passed in, and Comparator is the strategy.

4 Summary

The complexity of the system can be reduced by using the strategy mode reasonably. When a class contains multiple behaviors and represents multiple if branches, you can consider using the policy pattern to make each behavior a policy class.

The github address of the example in this paper

Topics: Programming calculator JDK github