平台开发之策略模式

it2024-11-28  18

用鸭子来举例

测试类

package com.troila.design_patterns.strategy_pattern; /** * @description: * @author: ZhiWen * @create: 2020-10-21 **/ public class TestDuck { public static void main(String[] args) { RedheadDuck redheadDuck = new RedheadDuck(); redheadDuck.isDuck(); redheadDuck.swim(); redheadDuck.display(); //动态设置鸭子的行为 redheadDuck.setFlyBehavior(new FlyWithWings()); redheadDuck.setQuackBehavior(new Quack()); redheadDuck.performFly(); redheadDuck.performQuack(); } }

抽象类

package com.troila.design_patterns.strategy_pattern; /** * @description:抽象超类,公共方法 * @author: ZhiWen * @create: 2020-10-21 **/ public abstract class Duck { private FlyBehavior flyBehavior; private QuackBehavior quackBehavior; /** * 动态设置行为参数 * @param f */ public void setFlyBehavior(FlyBehavior f) { flyBehavior = f; } public void setQuackBehavior(QuackBehavior q) { quackBehavior = q; } /** * 通用方法 * @return */ public Boolean isDuck() { System.out.println("Duck.Class=====>我是一只鸭!"); return true; } /** * 通用方法 * @return */ public Boolean swim() { System.out.println("Duck.Class=====>鸭子游泳!"); return true; } /** * 子类必须实现自己的逻辑 * @return */ public abstract Boolean display(); /** * 通用飞 具体使用子实现的飞的方式 * @return */ public Boolean performFly() { flyBehavior.fly(); return true; } /** * 通用的叫 具体使用子实现的叫的方式 * @return */ public Boolean performQuack() { quackBehavior.quack(); return true; } }

子类

package com.troila.design_patterns.strategy_pattern; /** * @description: 子类 红头鸭 * @author: ZhiWen * @create: 2020-10-21 **/ public class RedheadDuck extends Duck { /** * 子类必须实现的抽象方法 * @return */ @Override public Boolean display() { System.out.println("RedheadDuck.Class=====>我是红头鸭"); return true; } }

接口一

package com.troila.design_patterns.strategy_pattern; /** * @description:通用飞行为 * @author: ZhiWen * @create: 2020-10-21 **/ public interface FlyBehavior { /** * 子实现类必须实现自己飞的逻辑 */ void fly(); }

实现类

package com.troila.design_patterns.strategy_pattern; /** * @description:飞的一种方式 * @author: ZhiWen * @create: 2020-10-21 **/ public class FlyWithWings implements FlyBehavior{ @Override public void fly() { System.out.println("FlyWithWings.Class=====>我用翅膀飞"); } } package com.troila.design_patterns.strategy_pattern; /** * @description: 空实现覆盖抽象方法 * @author: ZhiWen * @create: 2020-10-21 **/ public class FlyNoWay implements FlyBehavior{ @Override public void fly() { System.out.println("FlyNoWay.Class=====>我根本不会飞"); } }

接口二

package com.troila.design_patterns.strategy_pattern; /** * @description:通用叫行为 * @author: ZhiWen * @create: 2020-10-21 **/ public interface QuackBehavior { /** * 子实现类必须实现自己叫的逻辑 */ void quack(); }

实现类

package com.troila.design_patterns.strategy_pattern; /** * @description:一种叫的方式 * @author: ZhiWen * @create: 2020-10-21 **/ public class Quack implements QuackBehavior{ @Override public void quack() { System.out.println("Quack.Class=====>嘎嘎叫"); } } package com.troila.design_patterns.strategy_pattern; /** * @description:一种叫的方式 * @author: ZhiWen * @create: 2020-10-21 **/ public class Squeak implements QuackBehavior{ @Override public void quack() { System.out.println("Squeak.Class=====>吱吱叫"); } } package com.troila.design_patterns.strategy_pattern; /** * @description:不会叫,空实现覆盖抽象方法 * @author: ZhiWen * @create: 2020-10-21 **/ public class MuteQuack implements QuackBehavior{ @Override public void quack() { System.out.println("MuteQuack.Class=====>我不会叫"); } }
最新回复(0)