"Headfirst Design Pattern" Strategy Mode

Design Principles

1. Find out where changes in the application may need to be changed, separate them, and do not put them together with the code that does not change.
2. Program for the interface, not for the implementation
. 3. Use combination more and less inheritance

Strategy mode

The algorithm family is defined and packaged separately so that they can be replaced with each other. This mode makes the algorithm change independent of the customers who use the algorithm
Insert picture description here
Insert picture description here

package Duck;
//超类
//策略模式
public abstract class Duck {
    
    
    FlyBehavior flyBehavior;
    QuackBehavior quackBehavior;

    public Duck() {
    
    
    }
    public abstract void display();
    public void performFly(){
    
    
        flyBehavior.fly();
    }
    public void performQuack(){
    
    
        quackBehavior.quack();
    }
    public void setFlyBehavior(FlyBehavior fb){
    
    
        flyBehavior = fb;
    }
    public void setQuackBehavior(QuackBehavior qb){
    
    
        quackBehavior = qb;
    }
    public void swim(){
    
    
        System.out.println("All ducks float, even decoys!");
    }
}
package Duck;
//绿头鸭
public class MailardDuck extends Duck{
    
    
    public MailardDuck() {
    
    
        flyBehavior = new FlyWithWings();
        quackBehavior = new Quack();
    }

    @Override
    public void display() {
    
    
        System.out.println("I'm a real Mallard duck!");
    }
}
package Duck;
//模型鸭
public class ModelDuck extends Duck{
    
    
    public ModelDuck() {
    
    
        flyBehavior = new FlyNoWay();
        quackBehavior = new Quack();
    }

    @Override
    public void display() {
    
    
        System.out.println("I'm a model duck!");
    }
}

We define two interface classes in the superclass Duck. The purpose is to call different classes that implement interfaces according to different ducks.
For example, in this example, the changed parts of fly() and Quake() are implemented as interfaces, and they are implemented separately Different behaviors,
three different flying modes and three different tweet modes are implemented here.
When defining different ducks, inherit the abstract class Duck, and
assign different values to the FlyBehavior flyBehavior; QuackBehavior quackBehavior; the two interface classes, and finally When the test is called, the corresponding behavior will be called according to the nature of polymorphism

Insert picture description here

package Duck;

public interface FlyBehavior {
    
    
    public void fly();
}
package Duck;

public interface QuackBehavior {
    
    
    public void quack();
}
package Duck;

public class FlyNoWay implements FlyBehavior{
    
    
    @Override
    public void fly() {
    
    
        System.out.println("I'm can't fly!!");
    }
}
package Duck;

public class FlyWithWings implements FlyBehavior{
    
    
    @Override
    public void fly() {
    
    
        System.out.println("I'm flying!!");
    }
}
package Duck;

public class FlyRocketPowered implements FlyBehavior {
    
    
    @Override
    public void fly() {
    
    
        System.out.println("I'm flying with a rocket!!");
    }
}
package Duck;

public class MuteQuack implements QuackBehavior {
    
    
    @Override
    public void quack() {
    
    
        System.out.println("Slience");
    }
}
package Duck;

public class Quack implements QuackBehavior {
    
    
    @Override
    public void quack() {
    
    
        System.out.println("Quack");
    }
}
package Duck;

public class Squeak implements QuackBehavior {
    
    
    @Override
    public void quack() {
    
    
        System.out.println("Squeak");
    }
}

Finally, our test class and output results

package Duck;

public class MiniDuckSimulator {
    
    
    public static void main(String[] args) {
    
    
        Duck mallard = new MailardDuck();
        mallard.performFly();
        mallard.performQuack();
        Duck model = new ModelDuck();
        model.performFly();
        model.setFlyBehavior(new FlyRocketPowered());
        model.performFly();
    }
}

Insert picture description here
Finally, look back at the definition of our strategy design pattern

Strategy mode

The algorithm family is defined and packaged separately so that they can be replaced with each other. This mode makes the algorithm change independent of the customers who use the algorithm
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36694674/article/details/107568158