美文网首页
策略模式

策略模式

作者: 阳光的技术小栈 | 来源:发表于2018-01-22 15:12 被阅读8次

    策略模式定义了算法族(也可以说是一组行为),分别封装起来,让它们之间可以相互交替,此模式让算法的变化独立于使用算法的客户。

    示例—不同鸭子的表现

    设计几种鸭子的类,比如绿头鸭、红头鸭等。鸭子都由不同的样子,有些会飞,有些不会飞(比如橡皮鸭),有些会叫,有些不会叫(比如橡皮鸭)。

    UML图表示

    鸭子的策略模式设计

    代码演示

    1. 由于每个鸭子会飞,但有些会飞,有些不会飞,所以设计一个飞行行为接口,用不同实现表示
    interface FlyBehavior{
        void fly();
    }
    
    class FlyWithWings implements FlyBehavior{
        @Override
        public void fly() {
            System.out.println("I'm flying!!");
        }
    }
    
    class FlyNoWay implements FlyBehavior{
        @Override
        public void fly() {
            System.out.println("I can't fly");
        }
    }
    
    1. 大部分鸭子会叫,也有不会叫的鸭子,而且叫的声音也不同。也采用接口+实现的方式
    interface QuackBehavior{
        void quack();
    }
    
    class Quack implements QuackBehavior{
        @Override
        public void quack() {
            System.out.println("Quack");
        }
    }
    
    class MuteQuack implements QuackBehavior{
        @Override
        public void quack() {
            System.out.println("<< Silence >>");
        }
    }
    
    class Squeak implements QuackBehavior{
        @Override
        public void quack() {
            System.out.println("Squeak");
        }
    }
    
    1. 设计鸭子的基类,总结鸭子的行为有:飞、叫、游泳、显示的外观。
    public abstract class Duck {
        private FlyBehavior flyBehavior;
        private QuackBehavior quackBehavior;
    
        public void swim(){
            System.out.println("I can swim");
        }
    
        public abstract void display();
    
        public void performFly(){
            flyBehavior.fly();
        }
    
        public void performQuack(){
            quackBehavior.quack();
        }
    
        public void setFlyBehavior(FlyBehavior flyBehavior){
            this.flyBehavior = flyBehavior;
        }
    
        public void setQuackBehavior(QuackBehavior quackBehavior){
            this.quackBehavior = quackBehavior;
        }
    }
    
    1. 四种鸭子的实现:绿头鸭、红头鸭、橡皮鸭、诱饵鸭。
    class MallardDuck extends Duck{
    
        MallardDuck(){
            setFlyBehavior(new FlyWithWings());
            setQuackBehavior(new Quack());
        }
    
        @Override
        public void display() {
            System.out.println("I'm a green head duck");
        }
    }
    
    class RedHeadDuck extends Duck{
    
        RedHeadDuck(){
            setFlyBehavior(new FlyWithWings());
            setQuackBehavior(new Quack());
        }
    
        @Override
        public void display() {
            System.out.println("I'm a red head duck");
        }
    }
    
    class RubberDuck extends Duck{
    
        RubberDuck(){
            setFlyBehavior(new FlyNoWay());
            setQuackBehavior(new MuteQuack());
        }
    
        @Override
        public void display() {
            System.out.println("I'm a model duck");
        }
    }
    
    class DecoyDuck extends Duck{
    
        DecoyDuck(){
            setFlyBehavior(new FlyNoWay());
            setQuackBehavior(new Squeak());
        }
    
        @Override
        public void display() {
            System.out.println("I'm a decoy duck");
        }
    }
    
    1. 添加测试代码
     public static void main(String[] args) {
            MallardDuck mallardDuck = new MallardDuck();
            mallardDuck.swim();
            mallardDuck.display();
            mallardDuck.performFly();
            mallardDuck.performQuack();
    
            System.out.println("~~~~~~~~~~~~~~~~~~~");
    
            RedHeadDuck redHeadDuck = new RedHeadDuck();
            redHeadDuck.swim();
            redHeadDuck.display();
            redHeadDuck.performFly();
            redHeadDuck.performQuack();
    
            System.out.println("~~~~~~~~~~~~~~~~~~~");
    
            RubberDuck rubberDuck = new RubberDuck();
            rubberDuck.swim();
            rubberDuck.display();
            rubberDuck.performFly();
            rubberDuck.performQuack();
    
            System.out.println("~~~~~~~~~~~~~~~~~~~");
    
            DecoyDuck decoyDuck = new DecoyDuck();
            decoyDuck.swim();
            decoyDuck.display();
            decoyDuck.performFly();
            decoyDuck.performQuack();
        }
    
    1. 测试结果
    I can swim
    I'm a green head duck
    I'm flying!!
    Quack
    ~~~~~~~~~~~~~~~~~~~
    I can swim
    I'm a red head duck
    I'm flying!!
    Quack
    ~~~~~~~~~~~~~~~~~~~
    I can swim
    I'm a model duck
    I can't fly
    << Silence >>
    ~~~~~~~~~~~~~~~~~~~
    I can swim
    I'm a decoy duck
    I can't fly
    Squeak
    

    相关文章

      网友评论

          本文标题:策略模式

          本文链接:https://www.haomeiwen.com/subject/axqrixtx.html