• Strategy


    策略模式: 定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
    public
    interface FlyBehavior { public void fly(); }
    public class FlyWithWings implements FlyBehavior{
    
        @Override
        public void fly() {
            System.out.println("i'm flying");
            
        }
    
    }
    
    
    public class FlyNoWay implements FlyBehavior{
    
        @Override
        public void fly() {
            System.out.println(" cannot fly");
            
        }
    
    }
    public interface QuackBehavior {
    
        public void quack();
    }
    public class Quack implements QuackBehavior{
    
        @Override
        public void quack() {
            System.out.println("quack");
            
        }
    
    }
    public class MuteQuack implements QuackBehavior{
    
        @Override
        public void quack() {
            System.out.println("<<silence>>");
            
        }
    
    }
    public class Squeak implements QuackBehavior{
    
        @Override
        public void quack() {
            System.out.println("squeak");
            
        }
    
    }
    public abstract class Duck {
    
        FlyBehavior flyBehavior;
        QuackBehavior quackBehavior;
        public Duck(){}
        
        public abstract void display();
        
        public void performFly(){
            flyBehavior.fly();
        }
        
        public void performQuack(){
            quackBehavior.quack();
        }
        
        public void swim(){
            System.out.println("All ducks float, even decoys!");
        }
    
        public FlyBehavior getFlyBehavior() {
            return flyBehavior;
        }
    
        public void setFlyBehavior(FlyBehavior flyBehavior) {
            this.flyBehavior = flyBehavior;
        }
    
        public QuackBehavior getQuackBehavior() {
            return quackBehavior;
        }
    
        public void setQuackBehavior(QuackBehavior quackBehavior) {
            this.quackBehavior = quackBehavior;
        }
        
        
    }
    public class MallardDuck extends Duck{
    
        public MallardDuck(){
            flyBehavior = new FlyWithWings();
            quackBehavior = new Quack();
        }
    
        @Override
        public void display() {
            System.out.println("I am  a mallard duck");
            
        }
    
    }
    public class MiniDuckSimulator {
    
        public static void main(String[] args){
            Duck mallard = new  MallardDuck();
            mallard.performFly();
            mallard.performQuack();
            
            mallard.setFlyBehavior(new FlyNoWay());
            mallard.performFly();
        }
    }
    public class MiniDuckSimulator {
    
        public static void main(String[] args){
            Duck mallard = new  MallardDuck();
            mallard.performFly();
            mallard.performQuack();
            
            mallard.setFlyBehavior(new FlyNoWay());
            mallard.performFly();
        }
    }
  • 相关阅读:
    使用数组实现简单线性表功能
    解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析
    Entity Framework with NOLOCK
    64位CentOS 6.0下搭建LAMP环境
    如何正确看待Linq的DistinctBy扩展和ForEach扩展
    jQuery最佳实践
    大话数据结构-树
    hdu2534-Score
    WKE——Webkit精简的纯C接口的浏览器
    WM_ERASEBKGND官方解释(翻译),以及Delphi里所有的使用情况(就是绘制窗口控件背景色,并阻止进一步传递消息)
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/6005801.html
Copyright © 2020-2023  润新知