What are strategic patterns

Encapsulate a set of algorithms into separate classes with a common interface that can be converted to each other.

Structure of a class diagram





【Strategy】

  • 【 Abstract Strategy 】Strategy: Usually implemented by interfaces/abstract classes, gives the methods required by the specific Strategy.
  • ConcreteStrategy: concrete implementation strategy algorithm and steps.
  • Context: Holds a reference to a Strategy. The policy is encapsulated twice, avoiding direct use of the policy.


For chestnut time

It is said that Zhuge Liang knows astronomy and geography, and uses weapons like god, especially fire ability, burning Bo Wang slope, burning Chibi. Here are some of the most frequently used tactics

  • The fire is the art of
  • Wuzhuang method
  • Grass boat borrow arrow
  • empty

Abstract strategy
public interface IStrategy {// Give the interface required by the policy

    void warMethod();
}
Copy the code
The specific strategy
public class FireStrategy implements IStrategy {// The implementation strategy
    @Override
    public void warMethod() {
        System.out.println("[Fire] Whoever burns the mountain will go to jail."); }}Copy the code
public class BaGuaStrategy implements IStrategy {// The implementation strategy
    @Override
    public void warMethod() {
        System.out.println("[Bagua Array] can be a hundred thousand elite soldiers with various changes."); }}Copy the code
public class CaoChuanStrategy implements IStrategy {// The implementation strategy
    @Override
    public void warMethod() {
        System.out.println("[Straw boat borrowing arrows] is a trick of doubt."); }}Copy the code
public class EmptyCityStrategy implements IStrategy {// The implementation strategy
    @Override
    public void warMethod() {
        System.out.println("[Empty City plan] The city is so empty, the memory is so fierce."); }}Copy the code


Wrapper class

This general holds the reference of strategy, that is, whatever strategy Zhuge Liang gives him, he can use it without affecting other functions. After secondary processing by the general, the strategy is more powerful. The general should be Jiang Wei.

public class General {
    private IStrategy mStrategy;

    public General(IStrategy strategy) {
        this.mStrategy = strategy;
    }

    public void Yell(){
        System.out.println("General: [All generals obey orders and set up battle lines]"); mStrategy.warMethod(); }}Copy the code



Practical results

Let’s take a look at the general’s ability to absorb and comprehend. Zhuge Liang threw four new tactics to the general in one breath.

        General general;
        general = new General(new FireStrategy());
        general.Yell();
        System.out.println("\n------------------------------------\n");

        general = new General(new BaGuaStrategy());
        general.Yell();
        System.out.println("\n------------------------------------\n");

        general = new General(new CaoChuanStrategy());
        general.Yell();
        System.out.println("\n------------------------------------\n");

        general = new General(new EmptyCityStrategy());
        general.Yell();
        System.out.println("\n------------------------------------");Copy the code





conclusion

Advantages of policy templates

  • The various strategies switch freely before, and different strategies can play different roles. It’s easier to extend the original code.
  • Avoid using multiple judgments, which are difficult to maintain.

Disadvantages of policy templates

  • It’s hard to maintain if you have a lot of policy classes, and you have to know which scenario to use which strategy.