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();
        }
    }
  • 相关阅读:
    重大技术需求系统八
    2020年下半年软考真题及答案解析
    周总结五
    重大技术需求系统七
    TextWatcher 编辑框监听器
    Android四大基本组件介绍与生命周期
    JAVA String,StringBuffer与StringBuilder的区别??
    iOS开发:保持程序在后台长时间运行
    宏定义的布局约束
    随便说一些
  • 原文地址:https://www.cnblogs.com/chenfx/p/14791829.html
Copyright © 2011-2022 走看看