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

    /**
     * @author yuyang
     * @DATE 2019/1/7 0007-9:12
     * 抽象策略角色
     */
    public interface Strategy {
        int calc(int a,int b);
    }
    /**
     * @author yuyang
     * @DATE 2019/1/7 0007-9:12
    *具体策略模式
    */ public class Addcalc implements Strategy { @Override public int calc(int a,int b) { return a+b; } }
    /**
     * @author yuyang
     * @DATE 2019/1/7 0007-9:14
    具体策略角色
    */ public class Subtractcalc implements Strategy{ @Override public int calc(int a, int b) { return a-b; } }
    /**
     * @author yuyang
     * @DATE 2019/1/7 0007-9:15
    环境角色
    */ public class Environment { Strategy strategy; public Environment(Strategy strategy) { this.strategy = strategy; } public int calc(int a,int b){ return strategy.calc(a,b); } }
    /**
     * @author yuyang
     * @DATE 2019/1/7 0007-9:14
    */ public class Text { public static void main(String[] args) { Environment environment = new Environment(new Subtractcalc()); System.out.println(environment.calc(5,5)); } }
  • 相关阅读:
    [NOIP2008] 传纸条
    [NOIP2006] 能量项链
    [poj2393] Yogurt factory
    [poj3069] Saruman's Army
    [NOIP2011] 观光公交
    [NOIP2010] 关押罪犯
    [洛谷2744] 量取牛奶
    [poj3281] Dining
    关于几类STL容器的swap复杂度问题
    折半法
  • 原文地址:https://www.cnblogs.com/yuyangcoder/p/10231440.html
Copyright © 2011-2022 走看看