Design Patterns---Strategy Pattern

The strategy mode, as the name suggests, is a mode in which different strategies are used to solve problems. Sun Wukong has seventy-two changes, and Sun Tzu has thirty-six strategies. The seventy-two changes and every strategy and strategy in the thirty-six strategies are all different. is a strategy. It is necessary for the parties to choose what becomes and which strategy to use according to the specific situation.

 

1. Strategy pattern class diagram



 


2. The composition of the strategy pattern:

Abstract strategy base class, strategy implementation class, caller.

There are abstract algorithms in the base class, and the implementation class implements specific and algorithmic behaviors. The caller class holds a reference to the strategy interface and can flexibly invoke strategies of different implementation classes.

 

 

3. design example

For example, when traveling abroad, there are different ways to travel.

Policy abstract base class

/**
 * Strategy abstract base class, with abstract behavior interface
 */
public interface TravelStrategy {
    public void travel();
}

 

Implementation class: cycling trip

public class BikeTravel implements TravelStrategy {
    @Override
    public void travel() {
        System.out.println("Travel by Bike.");
    }
}

 

Realization Two: Train Travel

public class RailWayTravel implements TravelStrategy {
    @Override
    public void travel() {
        System.out.println("Travel by RailWay.");
    }
}

 

Realization Three: Hiking

public class WalkTravel implements TravelStrategy {
    @Override
    public void travel() {
        System.out.println("Travel by Walk.");
    }
}

 

Caller class: Passenger

/**
 * Policy calling class, holding a reference to the policy
 */
public class Person {

    private Long id;
    private String name;

    private TravelStrategy strategy;

    public void setStrategy(TravelStrategy strategy) {
        this.strategy = strategy;
    }

    public void travel() {
        strategy.travel();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

Test class:

public class InvokeClient {
    public static void main(String[] args) {
        Person person = new Person();
        person.setStrategy(new WalkTravel());
        person.travel();

        person.setStrategy(new BikeTravel());
        person.travel();

        person.setStrategy(new RailWayTravel());
        person.travel();
    }
}

 

Using the strategy mode, you can better make more suitable coping strategies according to different situations.

 

advantage:

The encapsulation algorithm reduces the coupling between the algorithm's users and the algorithm, and does a good job of decoupling.

It embodies the design principle of being open to extension and closed to modification, so that the program has better maintainability and extensibility.

Programming for the abstract, rather than for the concrete, makes the system flexible.

 

shortcoming:

The caller needs to know each strategy in order to use the strategy correctly, and the caller needs to master the usage scenarios of each strategy.

The encapsulation strategy class is made for each change, which increases the number of classes in the system and increases the complexity of the system.

  

scenes to be used:

1. Replace many if..else.. conditional judgments.

2. The algorithm needs to be switched freely.

3. Multiple classes differ only in certain behaviors or algorithms.

 

user target audience:

One object can use multiple strategies, or different objects can use different strategies, depending on the situation.

 

Precautions:

When there are too many strategies, it will increase the complexity of the system, and the problem of strategy expansion needs to be considered.

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563776&siteId=291194637