读书人

设计形式-策略模式

发布时间: 2012-08-25 10:06:20 作者: rapoo

设计模式----策略模式

策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。(原文:The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.)

?

实例

算法接口:Strategy.java

package strategy;public class Main {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubContext context;//算法Acontext = new Context(new ConcreteStrategyA());context.contextInterface();//算法Bcontext = new Context(new ConcreteStrategyB());context.contextInterface();}}
?

读书人网 >软件开发

热点推荐