Strategy Pattern /Template Method Pattern
来源:互联网 发布:哇嘎网络连接失败 编辑:程序博客网 时间:2024/05/22 05:33
Program to an interface, not an implementation [针对接口编程,而不要针对实现编程]
The Strategy pattern is based on a few principles:[策略模式基于以下几个原则:]
-
Objects have responsibilities.[对象具有特定的职责]
-
Different, specific implementations of these responsibilities are manifested through the use of polymorphism.[通过使用来多态表示这些职责的不同、特定实现]
-
There is a need to manage several different implementations of what is, conceptually, the same algorithm.[需要对同一算法的几个不同实现进行管理]
UML图:
Code:
package strategy;
/**
* @role __Strategy
*/
public interface Strategy {
//算法接口
void AlgorithmInterface();
}
package strategy;
/**
* Implements an algorithm using the Strategy interface.
*/
public class ConcreteStrategyA implements stategy.Strategy {
//具体算法实现
public void AlgorithmInterface() {
System.out.println("I'm ConcreteStrategyA.AlgorithmInterface();");
}
}
package strategy;
/**
* Implements an algorithm using the Strategy interface.
*/
public class ConcreteStrategyB implements stategy.Strategy {
// 具体算法实现
public void AlgorithmInterface() {
System.out.println("I'm ConcreteStrategyB.AlgorithmInterface();");
}
}
package strategy;
/**
* Implements an algorithm using the Strategy interface.
*/
public class ConcreteStrategyC implements stategy.Strategy {
// 具体算法实现
public void AlgorithmInterface() {
System.out.println("I'm ConcreteStrategyC.AlgorithmInterface();");
}
}
package strategy;
/**
* @role __StrategyContext
*/
public class Context {
/**
* @label Strategy
* @link aggregation
*/
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void ContextInterface() {
strategy.AlgorithmInterface();
}
}
package strategy;
public class Client {
/*#stategy.Context Dependency_Link*/
/* #stategy.Context Dependency_Link */
/* #stategy.Context Dependency_Link */
public static void Main(String[] args) {
// 测试下几个不同的策略算法
Context a = new Context(new ConcreteStrategyA());
a.ContextInterface();
Context b = new Context(new ConcreteStrategyB());
b.ContextInterface();
Context c = new Context(new ConcreteStrategyC());
c.ContextInterface();
}
}
* @role __TemplateContext
*/
* Primitive operation
*/
public abstract void primitiveOperation();
* Primitive operation
*/
public abstract int primitiveOperation1();
* Defines the skeleton of an algorithm. Calls primitive
operations as well as operations defined in AbstractClass
or those in other objects.
*/
public void templateMethod() {
//something happens here...
primitiveOperation();
//...
//later we need another step
primitiveOperation1();
//something more
}
}
* Implements the primitive operations to carry out
* subclass-specific steps of the algorithm.
*/
/* put implementation of particular step of template method here*/
System.out.println("ConcreteClass.PrimitiveOperation1() is called;");
}
/* put implementation of particular step of template method here*/
System.out.println("ConcreteClass.PrimitiveOperation2() is called;");
return 0;
}
}
public static void Main(String[] args) {
// Create instance and call template method
AbstractClass instance = null;
instance = new ConcreteClass();
instance.templateMethod();
}
- Strategy Pattern /Template Method Pattern
- [Design Pattern] Template Method Pattern
- Template Method Pattern
- Template Method Pattern
- Template Method Pattern
- Template Method Pattern
- Template method pattern
- Template Method Pattern
- Design Pattern Template Method
- Template Method Pattern
- Template Method Pattern
- The Template method pattern
- Design Pattern -- Template Method Pattern(一)
- Template Method Pattern(Gof-Behavioral Design Pattern)
- The eighth pattern-------Template Method Pattern
- Design Pattern(1)-Template Method Pattern
- 【Discrete Mathematics】Template Method Pattern
- Design Pattern: Template Method 模式
- 设计Strategy Pattern
- Template method & Strategy
- 1111
- C++的沉迷与爱恋
- 设计网站和Wordpress插件
- Strategy Pattern /Template Method Pattern
- Microsoft Visual Studio 2005 各种评估版下载
- 超级移动筹码分布
- 用Visual C#创建Windows服务程序
- 关于RegSetValueEx函数中的CONST BYTE参数
- 剖析 C++ 引用
- jain-sip 一些类的初步理解
- 『C程序设计』读书笔记
- 25个值得关注的网站