标签:style blog http 使用 ar java sp 2014 c
定义:它定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户
结构图:
示例代码:
public interface Strategy { public void algorithnInterface(); } public class ConcreteStrategyA implements Strategy { @Override public void algorithnInterface() { // TODO Auto-generated method stub System.out.println("算法A实现"); } } public class ConcreteStrategyB implements Strategy { @Override public void algorithnInterface() { // TODO Auto-generated method stub System.out.println("算法B实现"); } } public class ConcreteStrategyC implements Strategy { @Override public void algorithnInterface() { // TODO Auto-generated method stub System.out.println("算法C实现"); } } public class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public void contextInterface() { strategy.algorithnInterface(); } }客户端代码:
public class Client { public static void main(String[] args) { Context context = null; context = new Context(new ConcreteStrategyA()); context.contextInterface(); context = new Context(new ConcreteStrategyB()); context.contextInterface(); context = new Context(new ConcreteStrategyC()); context.contextInterface(); } }
运行输出:
算法A实现
算法B实现
算法C实现
标签:style blog http 使用 ar java sp 2014 c
原文地址:http://blog.csdn.net/tiandesheng111/article/details/39802715