"Make up lessons" in progress: design mode (11) -- strategy mode in the game

1. Summary of the foregoing

"Make up" in progress: design pattern series

2. Strategy mode in the game

I am a person who likes playing games very much. It's really fun to play games at home on weekends.

Looking back on the past extraordinary years when I went to college, at that time, I was basically playing games by myself, surrounded by several people behind me, one by one playing the role of a dog's head.

After a long time, you can find that people who like to watch others play games often don't play well by themselves, but when they become a dog head strategist, it's a set. Is this the clear view of the bystander?

In those years, the most popular player in the university dormitory was the "League of heroes". In those years, it was the AP swordsman who dominated the world. However, every time we ranked, we met the swordsman of others and our swordsman.

At this time, it is usually the time when the dog commander goes online. You give this xxx to ensure how you cowhide. Alas, who do you beat first? Why do you always chase a meat chop.

If the above scenario is transformed into a program, it is basically like this:

First, define an interface of LOL:

public interface LOL {
    void playMethod();
}

Then two more dogheads will implement this interface. Each doghead has its own playing method:

public class DogStrategistA implements LOL{
    @Override
    public void playMethod() {
        System.out.println("First out attack suit, just front, no advice");
    }
}

public class DogStrategistB implements LOL {
    @Override
    public void playMethod() {
        System.out.println("First out of the defense equipment, can stand before there is output");
    }
}

Then we start a game:

public class LOLGame {
    private LOL lol;
    public LOLGame(LOL lol) {
        this.lol = lol;
    }
    public void play() {
        this.lol.playMethod();
    }
}

Then here is a test class:

public class Test {
    public static void main(String[] args) {
        LOLGame game;
        System.out.println("a person who offers bad advice A Your idea--------------");
        game = new LOLGame(new DogStrategistA());
        game.play();
        System.out.println("a person who offers bad advice B Your idea--------------");
        game = new LOLGame(new DogStrategistB());
        game.play();
    }
}

The final implementation results are as follows:

a person who offers bad advice A Your idea--------------
First out attack suit, just front, no advice
 a person who offers bad advice B Your idea--------------
First out of the defense equipment, can stand before there is output

Does it feel that the above code seems to make no difference in peacetime, but you're not wrong. This is the strategy mode.

3. Strategy mode

3.1 definitions

Strategy Pattern is a relatively simple pattern, also known as policy pattern. It is defined as follows:

Define a family of algorithms,encapsulate each one,and make theminterchangeable. (define a set of algorithms, encapsulate each algorithm and make them interchangeable.)

  • Context: encapsulation role, which serves as a connecting link between the preceding and the following, shields the direct access of high-level modules to policies and algorithms, and encapsulates possible changes.
  • Strategy: Abstract policy role, which is the abstraction of policy and algorithm family. It is usually an interface and defines the methods and attributes that each policy or algorithm must have.
  • ConcreteStrategy: specific policy role.

The general codes are as follows:

public interface Strategy {
    void doSomethinging();
}

public class ConcreteStrategy1 implements Strategy {
    @Override
    public void doSomethinging() {
        System.out.println("Specific strategy 1");
    }
}

public class ConcreteStrategy2 implements Strategy {
    @Override
    public void doSomethinging() {
        System.out.println("Specific strategy 2");
    }
}

public class Context {
    private Strategy strategy;
    public Context(Strategy strategy) {
        this.strategy = strategy;
    }
    public void doAnything() {
        this.strategy.doSomethinging();
    }
}

public class Test {
    public static void main(String[] args) {
        Strategy strategy = new ConcreteStrategy1();
        Context context = new Context(strategy);
        context.doAnything();
    }
}

3.2 advantages

  • The algorithm can switch freely: This is defined by the policy pattern itself. As long as the abstract policy is implemented, it will become a member of the policy family and be encapsulated by encapsulating roles.
  • Avoid using multiple conditional judgment: if there is no policy mode, we can only choose to use multiple conditional judgment statements. Multiple conditional statements are not easy to maintain, and the probability of error is greatly enhanced. After using the policy mode, other modules can decide which strategy to adopt. The access interface provided by the policy family is the encapsulation class, which simplifies the operation and avoids the judgment of conditional statements.
  • Good scalability: This is not even its advantage. It is too easy to add a strategy to the existing system as long as the interface is implemented.

3.3 disadvantages

  • Increase in the number of policy classes: each policy is a class. The possibility of reuse is very small, and the number of classes increases.
  • All strategy classes need to be exposed to the outside world: the upper module must know which strategies are available before deciding which strategy to use. This is contrary to the Demeter rule. I just want to use a strategy. Why should I know this strategy? What's the point of your encapsulation class? This is a disadvantage of the original strategy mode. Fortunately, we can use other modes to correct this defect, such as factory method mode, agent mode or sharing mode.

If the number of specific policies in a policy family in the system exceeds 4, it is necessary to consider using mixed mode to solve the problems of policy class expansion and external exposure.

Your attention to scanning code is the greatest encouragement for Xiaobian to adhere to originality:)

Tags: Design Pattern

Posted by mpf on Sat, 07 May 2022 10:41:09 +0300