Template Method模式和Strategy模式有何异同

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();
}
}


未完待续 ........
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值