• 设计模式-策略模式


    策略模式:

    定义了算法族,分别封装起来,让它们之间可以相互替换,此模式的变化独立于算法的使用者。

    /**
     * 策略模式
     */
    public class Stragety {
        public static void main(String[] args) {
            Zombie normalZombie = new NormalZombie();
            normalZombie.display();
            normalZombie.move();
            normalZombie.attack();
    
            normalZombie.setAttackable(new HitAttack());
            normalZombie.attack();
            //------------------------------------------------
            Zombie flagZombie = new FlagZombie(new RunMove(), new HitAttack());
            flagZombie.display();
            flagZombie.move();
            flagZombie.attack();
        }
    }
    
    interface Moveable{
        void move();
    }
    
    interface  Attackable{
        void attack();
    }
    
    class StepByStepMove implements Moveable{
        @Override
        public void move() {
            System.out.println("一步一步的移动");
        }
    }
    
    class  BiteAttack implements Attackable{
        @Override
        public void attack() {
            System.out.println("咬。。。");
        }
    }
    
    class RunMove implements Moveable{
        @Override
        public void move() {
            System.out.println("快速移动");
        }
    }
    
    class  HitAttack implements Attackable{
        @Override
        public void attack() {
            System.out.println("打。。。");
        }
    }
    
    
    
    abstract class Zombie{
        Moveable moveable;
        Attackable attackable;
    
        public Zombie(Moveable moveable, Attackable attackable) {
            this.moveable = moveable;
            this.attackable = attackable;
        }
    
        abstract public void display();
        abstract public void move();
        abstract public void attack();
    
        public Moveable getMoveable() {
            return moveable;
        }
    
        public void setMoveable(Moveable moveable) {
            this.moveable = moveable;
        }
    
        public Attackable getAttackable() {
            return attackable;
        }
    
        public void setAttackable(Attackable attackable) {
            this.attackable = attackable;
        }
    }
    
    class NormalZombie extends Zombie{
        //设置默认的移动和攻击方式
        public NormalZombie() {
            super(new StepByStepMove(),new BiteAttack());
        }
    
        public NormalZombie(Moveable moveable, Attackable attackable) {
            super(moveable, attackable);
        }
    
        @Override
        public void display() {
            System.out.println("我是普通僵尸。。。");
        }
    
        @Override
        public void move() {
            moveable.move();
        }
    
        @Override
        public void attack() {
            attackable.attack();
        }
    }
    
    class FlagZombie extends Zombie{
        public FlagZombie() {
            super(new StepByStepMove(),new BiteAttack());
        }
    
        public FlagZombie(Moveable moveable, Attackable attackable) {
            super(moveable, attackable);
        }
    
        @Override
        public void display() {
            System.out.println("我是骑手僵尸。。。");
        }
    
        @Override
        public void move() {
            moveable.move();
        }
    
        @Override
        public void attack() {
            attackable.attack();
        }
    }
  • 相关阅读:
    云架构系统如何做性能分析?| 实战干货
    1024 程序员日,聊聊升职加薪与职业发展!
    测试面试题集锦(三)| 计算机网络和数据库篇(附答案)
    在线沙龙 | 前端测试技术创新与实践
    测试开发系列课程学员打卡听课细则
    这 5 款实用性能测试工具,你会如何选择?
    618 年中大促!Python 自动化测试训练营立减 1000 元!送接口测试实战课!
    美人
    栀子花开
    朋友别哭
  • 原文地址:https://www.cnblogs.com/chenfx/p/14791829.html
Copyright © 2020-2023  润新知