Template Method模式很容易理解,就是由基类提供一个模板,将各子类中不变的行为提取到基类中实现,而各子类中可变的行为则由各子类自己重写基类方法实现.
Strategy则是在使用策略模式的应用实例内部维护一个策略实例,针对不同的子类用不同的策略实现.
来看看两者的代码实现:
Template Method模式 -- 基类
package com.dzeay.pattern.template_method;
public class BaseTemplate {
public void doSameThing() {
System.out.println("BaseTemplate.doSameThing");
}
public void doOtherThing() {
System.out.println("BaseTemplate.BaseTemplate");
}
}
Template Method模式 -- 子类A
package com.dzeay.pattern.template_method;
public class AClass extends BaseTemplate {
@Override
public void doOtherThing() {
System.out.println("AClassImpl.doOtherThing");
}
}
Template Method模式 -- 子类B
package com.dzeay.pattern.template_method;
public class BClass extends BaseTemplate {
@Override
public void doOtherThing() {
System.out.println("BClassImpl.doOtherThing");
}
}
Template Method模式 -- 测试类
package com.dzeay.pattern.template_method;
/**
* <pre>
* Template Method(模板方法模式)详解:
* 由基类提供一个模板,将各子类中不变的行为提取到基类中实现,
* 而各子类中可变的行为由各子类自己重写基类方法实现
* </pre>
*
* @author <a href="mailto:dzeay.com@gmail.com">dzeay.com</a>
* @since 2010-11-15
* @version 1.0
*/
public class TestClass {
/**
*
* @param args
*/
public static void main(String[] args) {
BaseTemplate aClass = new AClass();
aClass.doSameThing();
aClass.doOtherThing();
BaseTemplate bClass = new BClass();
bClass.doSameThing();
bClass.doOtherThing();
}
}
Strategy模式 -- 策略接口
package com.dzeay.pattern.strategy;
public interface IStrategy {
public void doOtherThing();
}
Strategy模式 -- 策略A
package com.dzeay.pattern.strategy;
public class AStrategy implements IStrategy {
@Override
public void doOtherThing() {
System.out.println("AStrategy.doOtherThing");
}
}
Strategy模式 -- 策略B
package com.dzeay.pattern.strategy;
public class BStrategy implements IStrategy {
@Override
public void doOtherThing() {
System.out.println("BStrategy.doOtherThing");
}
}
Strategy模式 -- 应用
package com.dzeay.pattern.strategy;
public class Context {
private IStrategy strategy;
public Context() {
}
public Context(IStrategy strategy) {
this.strategy = strategy;
}
public void doOtherThing() {
this.strategy.doOtherThing();
}
public void setStrategy(IStrategy strategy) {
this.strategy = strategy;
}
}
Strategy模式 -- 测试类
package com.dzeay.pattern.strategy;
/**
* <pre>
* Strategy(策略模式)详解:
* 在使用策略模式的应用实例内部维护一个strategy实例,针对不同的子类用不同的策略实现
* </pre>
*
* @author <a href="mailto:dzeay.com@gmail.com">dzeay.com</a>
* @since 2010-11-15
* @version 1.0
*/
public class TestClass {
/**
* @param args
*/
public static void main(String[] args) {
Context context = new Context();
context.setStrategy(new AStrategy());
context.doOtherThing();
context.setStrategy(new BStrategy());
context.doOtherThing();
context.setStrategy(new IStrategy() {
@Override
public void doOtherThing() {
System.out.println("doOtherThing");
}
});
context.doOtherThing();
}
}
未完待续 ........
分享到:
相关推荐
一、 模板方法(Template Method)模式 204 二、 模版方法模式的结构 204 三、 模板方法模式的示意性代码 205 四、 继承作为复用的工具 207 五、 一个实际应用模板方法的例子 208 六、 模版方法模式中的方法 210 七...
一、 模板方法(Template Method)模式 二、 模版方法模式的结构 三、 模板方法模式的示意性代码 四、 继承作为复用的工具 五、 一个实际应用模板方法的例子 六、 模版方法模式中的方法 七、 重构的原则 设计...
5、模板方法 Template Method (定义一个操作中的算法骨架,将其具体算法延迟到子类中) 6、单例 Singleton (保证应用程序中,某个类只有一个实例存在) 7、代理模式、观察者模式、工厂模式、装饰模式等
3、FACTORY METHOD —请 MM 去麦当劳吃汉堡,不同的 MM 有不同的口味,要每个都记住 、 是一件烦人的事情,我一般采用 Factory Method 模式,带着 MM 到服务员那儿,说“要一个 汉堡”,具体要什么样的汉堡呢,让 ...
模板方法(TemplateMethod)模式:定义一个操作中的算法骨架,而将算法的一些步骤延迟到子类中,使得子类可以不改变该算法结构的情况下重定义该算法的某些特定步骤。 策略(Strategy)模式:定义了一系列算法,并将...
很适合初学软件设计模型,总结的很全,简单通俗易懂 5个创建模式: 1、Abstract Factory 抽象工厂 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们... 10、Template Method 模板方法 11、Visitor 访问者
正因为这点不同,导致建筑的管理模式和软件的管理模式就有很多不同, 有些人认识不到这点,就产生了可以大量使用"软件 蓝领"的想法,因为他羡慕建筑中"民工"的低成本. 要知道软件还有一个与建筑截然相反的责任和用途,那...
Template Method:定义一个操作中的算法骨架,而将这些算法的具体实现的代码延迟到子类中完成。 Visitor:标识一个作用于某对象结构中的各元素的操作,在不改变各元素的类的前提下定义作用于这个元素的新操作。
附件中是java实现全部的设计模式,包含代码和工程(jbuilder工程),值得收藏. 此目录里包括了一书中所有23种设计模式的实现(Java 版)源码 关于代码的几点说明: 1. 代码为根据个人对Design Pattern的学习理解写...
按键跳转以及按Enter以不同参数提交,及其他感应事件 动态控制Page页的Head信息 SubmitOncePage:解决刷新页面造成的数据重复提交问题 SharpRewriter:javascript + xml技术利用#实现url重定向 采用XHTML和CSS设计可...
16、同步和异步有何异同,在什么情况下分别使用他们?举例说明。 如果数据将在线程间共享。例如正在写的数据以后可能被另一个线程读到,或者正在读的数据可能已经被另一个线程写过了,那么这些数据就是共享数据,...
19、同步和异步有何异同,在什么情况下分别使用他们?举例说明。 如果数据将在线程间共享。例如正在写的数据以后可能被另一个线程读到,或者正在读的数据可能已经被另一个线程写过了,那么这些数据就是共享数据,...
Form Template Method 形成Template Method Replace Inheritance with Delegation 用委托代替继承 Replace Delegation with Inheritance 用继承代替委托 Chapter 12:Big Refactorings(by Kent Beck and...
Form Template Method 形成Template Method Replace Inheritance with Delegation 用委托代替继承 Replace Delegation with Inheritance 用继承代替委托 Chapter 12:Big Refactorings(by Kent Beck ...
Form Template Method 形成Template Method Replace Inheritance with Delegation 用委托代替继承 Replace Delegation with Inheritance 用继承代替委托 Chapter 12:Big Refactorings(by Kent Beck and...
Form Template Method 形成Template Method Replace Inheritance with Delegation 用委托代替继承 Replace Delegation with Inheritance 用继承代替委托 Chapter 12:Big Refactorings(by Kent Beck and...
Form Template Method 形成Template Method Replace Inheritance with Delegation 用委托代替继承 Replace Delegation with Inheritance 用继承代替委托 Chapter 12:Big Refactorings(by Kent Beck ...
Form Template Method 形成Template Method Replace Inheritance with Delegation 用委托代替继承 Replace Delegation with Inheritance 用继承代替委托 Chapter 12:Big Refactorings(by Kent Beck ...
template模板方法模式、memento备忘录模式、observer观察者模式、command命令模式、state状态模式、strategy策略模式、mediator调停者模式、interpreter解释器模式、visitor访问者模式、chain of responsibility责任...