• 设计模式-策略模式


    策略模式:

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

    /**
     * 策略模式
     */
    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();
        }
    }
  • 相关阅读:
    软考相关试题
    qt中的toUtf8, toLatin1, Local8bit, toUcs4(转)
    qt的中文乱码问题
    《左耳听风》-ARTS-打卡记录-第八周
    杂题
    图论
    基础数据结构
    整除
    同余
    常用数学
  • 原文地址:https://www.cnblogs.com/chenfx/p/14791829.html
Copyright © 2020-2023  润新知