标签:strategy logs stat err ges img 策略 ret size
用户的角度:只需要知道具体的Context,并知道具体的策略类。
实现者的角度:拥有具体的策略类,封装了具体的算法或行为,并抽象一个公共的算法接口,包含在一个Context上下文中。
package com.stategy; /** * 上下文 * @author 黄二狗 * */ public class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy=strategy; } public void contextInterface() { strategy.Algorithmlnterface(); } }
package com.stategy; public interface Strategy { void Algorithmlnterface(); }
package com.stategy; public class ConcreteStrategyA implements Strategy{ @Override public void Algorithmlnterface() { System.out.println("具体的策略A"); } }
package com.stategy; public class ConcreteStrategyB implements Strategy{ @Override public void Algorithmlnterface() { System.out.println("具体的策略B"); } }
package com.stategy; public class ConcreteStrategyC implements Strategy{ @Override public void Algorithmlnterface() { System.out.println("具体的策略C"); } }
package com.stategy; /** * 用户需要知道具体的策略和策略接口还有上下文及其上下文的方法 * @author 黄二狗 * */ public class Client { public static void main(String[] args) { Strategy strategy=new ConcreteStrategyA(); Context context=new Context(strategy); context.contextInterface(); } }
标签:strategy logs stat err ges img 策略 ret size
原文地址:http://www.cnblogs.com/HJL085/p/6607934.html