zoukankan      html  css  js  c++  java
  • 设计模式-策略模式

    策略模式:

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

    /**
     * 策略模式
     */
    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();
        }
    }
  • 相关阅读:
    python一些简单操作
    MySQL事务
    c#替换word中的文本并导出(示例)
    SQLServer将某个字段的多行记录合并返回一行
    SQL根据某字段查询不重复记录
    SQLServer逗号分割字符串返回多行
    c# js日期工具
    onchange,onfocus ,oninput事件
    compositionstart事件与compositionend事件
    图片处理问题
  • 原文地址:https://www.cnblogs.com/chenfx/p/14791829.html
Copyright © 2011-2022 走看看