Shang Silicon Valley design pattern learning (23) - [strategy pattern]

Gateway = = > Java design pattern of station B

❤❤❤ Thanks to Shang Silicon Valley ❤❤❤

Recently, I began to plan to learn design patterns. Come on!!!

Scenario introduction

Questions about ducks

There are various kinds of ducks = = > such as wild duck, Peking duck, water duck, toy duck and Chong duck
Ducks have various behaviors, such as running, flying

Traditional ways to solve problems

Duck abstract class duck

//Ducks;
public abstract class Duck {
    public Duck() {

    }
    //Duck information;
    public abstract void show();

    //Ducks talk;
    public void talk(){
        System.out.println("Duck Talk");
    };
    //Duck running;
    public void run(){
        System.out.println("Duck running");
    };
    //Duck flight;
    public  void fly(){
        System.out.println("Ducks fly and fly");
    }
}

Toy duck

//Toy duck;
public class ToyDuck extends Duck{


    @Override
    public void show() {
        System.out.println("This is a toy duck,Unfortunately, he can't fly,Can't run");
    }

    @Override
    public void run() {
        System.out.println("Toy ducks can't run");
    }

    @Override
    public void fly() {
        System.out.println("Toy ducks can't fly");
    }
}

WaterDuck

//Water duck;
public class WaterDuck extends Duck {

    @Override
    public void show() {
        System.out.println("This is a water duck,He can't fly");
    }

    @Override
    public void fly() {
        System.out.println("Water ducks can't fly");
    }
}

Wild duck

//wild duck;
public class WildDuck extends Duck{
    @Override
    public void show() {
        System.out.println("This is a wild duck,It can fly,Can run,Can speak.");
    }
}

Impersonate Client

//Simulation client;
public class Client {
    public static void main(String[] args) {
        Duck duck = new ToyDuck();
        duck.show();
        duck.talk();
        duck.run();
        duck.fly();
    }
}
/*
This is a toy duck. Unfortunately, he can't fly and run
 Duck Talk
 Toy ducks can't run
 Toy ducks can't fly
*/

Notice a problem. If this problem is designed in such a traditional way, the bottom implementation will be changed because different kinds of ducks may behave differently
If other properties are added to the upper duck abstract class, the lower duck may have to change

Strategy mode

Define algorithm clusters and package them separately, so that they can be replaced with each other, so that the changes of algorithms are independent of the customers using algorithms

schematic diagram

(1) Separate the original changed code from the unchanged code;
(2) It defines the policy interface, that is, programming for the interface
(3) Try not to use inheritance; Instead, change to combination or aggregation
(4) Comply with the opening and closing principle

Go back to the beginning of duck classification and behavior; Solve problems with policy mode

Talk strategy interface TalkStrategy

/**
 * @author by CSDN@Xiaozhi RE0
 */
//Speech behavior strategy interface;
public interface TalkStrategy {
    void talk();
}

Running strategy interface

/**
 * @author by CSDN@Xiaozhi RE0
 */

//Running behavior interface;
public interface RunStrategy {
    void run();
}

Flight strategy interface

/**
 * @author by CSDN@Xiaozhi RE0
 */

//Flight behavior strategy;
public interface FlyStrategy {
    void fly();
}

The strategy that can speak implements ICanTalk

/**
 * @author by CSDN@Xiaozhi RE0
 */

//The strategy of speaking can be realized concretely;
public class ICanTalk implements TalkStrategy{
    @Override
    public void talk() {
        System.out.println("Can talk");
    }
}

Do not talk policy implementation

/**
 * @author by CSDN@Xiaozhi RE0
 */

//Specific implementation of the strategy of not speaking;
public class DoNotTalk implements TalkStrategy{
    @Override
    public void talk() {
        System.out.println("Unable to speak");
    }
}

The strategy of running can be used to implement ICanRun

/**
 * @author by CSDN@Xiaozhi RE0
 */
//The strategy of running can be realized concretely;
public class ICanRun implements RunStrategy{
    @Override
    public void run() {
        System.out.println("Can run");
    }
}

DoNotRun can not be implemented by running strategy

/**
 * @author by CSDN@Xiaozhi RE0
 */

//The strategy of not running can be realized in detail;
public class DoNotRun implements RunStrategy{
    @Override
    public void run() {
        System.out.println("Can't run");
    }
}

The strategy of flying can realize ICanFly

/**
 * @author by CSDN@Xiaozhi RE0
 */

//Specific implementation of flight strategy;
public class ICanFly implements FlyStrategy{
    @Override
    public void fly() {
        System.out.println("Can fly");
    }
}

Implementation of DoNotFly by non flying strategy

/**
 * @author by CSDN@Xiaozhi RE0
 */
//Specific implementation of the strategy of no flight;
public class DoNotFly implements FlyStrategy{
    @Override
    public void fly() {
        System.out.println("Can't fly");
    }
}

Duck abstract class

/**
 * @author by CSDN@Xiaozhi RE0
 */
//Ducks;
public abstract class Duck {
    //Policy interface for aggregating flight behavior;
    FlyStrategy flyStrategy;
    //Policy interface for aggregating running behavior;
    RunStrategy runStrategy;
    //Policy interface for aggregating speech behavior;
    TalkStrategy talkStrategy;

    //Duck information display;
    public abstract void show();

    //The way ducks talk;
    public void talk(){
        if(talkStrategy!=null){
            talkStrategy.talk();
        }
    };
    //Duck running method;
    public void run(){
        if(runStrategy!=null){
            runStrategy.run();
        }
    };
    //Duck flight method;
    public  void fly(){
       if(flyStrategy!=null){
           flyStrategy.fly();
       }
    }

    //Change the realization of flight strategy;
    public void setFlyStrategy(FlyStrategy flyStrategy) {
        this.flyStrategy = flyStrategy;
    }

    //Change the strategy of running;
    public void setRunStrategy(RunStrategy runStrategy) {
        this.runStrategy = runStrategy;
    }

    //Change the strategy of speaking;
    public void setTalkStrategy(TalkStrategy talkStrategy) {
        this.talkStrategy = talkStrategy;
    }
}

Wild duck

/**
 * @author by CSDN@Xiaozhi RE0
 */

//wild duck;
public class WildDuck extends Duck {
    //initialization;
    public WildDuck() {
        show();
        talkStrategy = new ICanTalk();
        runStrategy = new ICanRun();
        flyStrategy = new ICanFly();
    }

    @Override
    public void show() {
        System.out.println("This is a wild duck,It can fly,Can run,Can speak.");
    }
}

WaterDuck

/**
 * @author by CSDN@Xiaozhi RE0
 */

//Water duck;
public class WaterDuck extends Duck {
    //initialization;
    public WaterDuck() {
        show();
        talkStrategy = new ICanTalk();
        runStrategy  = new ICanRun();
        flyStrategy  = new DoNotFly();
    }

    @Override
    public void show() {
        System.out.println("This is a water duck,He can't fly");
    }
}

Toy duck

/**
 * @author by CSDN@Xiaozhi RE0
 */
//Toy duck;
public class ToyDuck extends Duck {
    //initialization;
    public ToyDuck() {
        show();
        talkStrategy = new ICanTalk();
        runStrategy  = new DoNotRun();
        flyStrategy  = new DoNotFly();
    }

    @Override
    public void show() {
        System.out.println("This is a toy duck,Unfortunately, he can't fly,Can't run");
    }
}

Simulate client call;

/**
 * @author by CSDN@Xiaozhi RE0
 */
//Simulation client;
public class Client {
    public static void main(String[] args) {
        Duck toyDuck = new ToyDuck();
        toyDuck.talk();
        toyDuck.run();
        toyDuck.fly();

        System.out.println("Modify the running properties of the toy duck");
        toyDuck.setRunStrategy(new ICanRun());
        toyDuck.run();
    }
}
/*
This is a toy duck. Unfortunately, he can't fly and run
 Can talk
 Can't run
 Can't fly
 Modify the running properties of the toy duck
 Can run
*/

Keywords: Design Pattern

Added by avianrand on Fri, 08 Oct 2021 07:44:10 +0300