模板模式和策略模式的区别【转】
来源:互联网 发布:C语言的算术表达式 编辑:程序博客网 时间:2024/05/16 01:43
1.策略模式
1.1 策略模式
策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。
策略模式属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有
共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到
客户端的情况下发生变化。
1.2 策略模式的组成
(1)抽象策略角色: 策略类,通常由一个接口或者抽象类实现。(2)具体策略角色:包装了相关的算法和行为。
类图如下:
1.3 使用场景
假设:现在要设计一个贩卖各类书籍的电子商务网站的购物车系统。一个最简单的情况就是把所有货品的单价乘上数量,但是实际情况肯定比这要复杂。
比如,本网站可能对所有的高级会员提供每本20%的促销折扣;对中级会员提供每本
10%的促销折扣;对初级会员没有折扣。
根据描述,折扣是根据以下的几个算法中的一个进行的:
算法一:对初级会员没有折扣。
算法二:对中级会员提供10%的促销折扣。
算法三:对高级会员提供20%的促销折扣。
实现代码如下:
- /**
- * @Title:商品抽象折扣类
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午03:24:53
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public interface MemberStrategy {
- public double discountPrice(double bookPrice);
- }
- /**
- * @Title:初级会员--折扣类
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午03:53:59
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public class JuniorMemberStrategy implements MemberStrategy{
- public double discountPrice(double bookPrice) {
- return bookPrice;
- }
- }
- /**
- * @Title:中级会员--折扣类
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午03:58:01
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public class InterMediateMemberStrategy implements MemberStrategy{
- @Override
- public double discountPrice(double bookPrice) {
- return bookPrice*0.9;
- }
- }
- /**
- * @Title:高级会员--折扣类
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午03:59:30
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public class AdvancedMemberStrategy implements MemberStrategy{
- @Override
- public double discountPrice(double bookPrice) {
- return bookPrice*0.8;
- }
- }
- /**
- * @Title:图书价格类
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午04:15:57
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public class BookPrice {
- private MemberStrategy strategy;
- private double price;
- /**
- * @param strategy
- */
- public BookPrice(MemberStrategy strategy,double price) {
- super();
- this.strategy = strategy;
- this.price=price;
- }
- /**
- *
- * getRealBookPrice(获取折扣后的价格)
- * @return double
- * @createDate 2013-3-12 下午04:30:26
- * @since 1.0.0
- */
- public double getRealBookPrice() {
- return strategy.discountPrice(this.price);
- }
- public MemberStrategy getStrategy() {
- return strategy;
- }
- public void setStrategy(MemberStrategy strategy) {
- this.strategy = strategy;
- }
- }
- /**
- * @Title:图书打折后的价格--测试类
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午04:17:23
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public class TestBookPrice {
- public static void main(String[] args) {
- MemberStrategy strategy=new AdvancedMemberStrategy();
- BookPrice bookPrice = new BookPrice(strategy,99.0);
- System.out.println("图书价格是:"+bookPrice.getRealBookPrice());
- }
- }
2 模版方法模式
2.1 模版方法模式
定义一个算法的骨架,而将一些实现步骤延迟到子类中。把不变的行为搬到超类,去除子类中重复的代码来体现他的优势。
类图如下:
2.2 应用场景
(1)一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现(2)各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。
首先识别现有代码中的不同之处,并且将不同之处分离为新的操作。最后,用一个
调用这些新的操作的模板方法来替换这些不同的代码。
实现代码如下:
- /**
- * @Title:模版方法抽象类--定义一个业务的骨架,而将一些实现步骤延迟到子类中
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午02:10:38
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public abstract class AbstractTemplate {
- /**
- *
- * executeMain(需要在子类实现)
- * @createDate 2013-3-12 下午02:12:43
- * @since 1.0.0
- */
- public abstract void executeMain();
- public void execute() {
- beforeAction();
- executeMain();
- afterAction();
- }
- public void beforeAction() {
- System.out.println("before Action...");
- }
- public void afterAction() {
- System.out.println("after Action...");
- }
- }
- /**
- * @Title:模版方法子类--实现其中可变的部分
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午02:13:09
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public class TemplateImpl extends AbstractTemplate{
- public void executeMain() {
- System.out.println("核心操作处理...实现方式一");
- }
- }
- /**
- * @Title:模版方法测试类
- * @Description:TODO
- * @Company: Orclight's Studio
- * @author: shuzl 2013-3-12 下午02:14:18
- * @motto: Never put off what you can do today until Tomorrow
- * @version 1.0.0
- */
- public class TestTemplate {
- public static void main(String[] args) {
- AbstractTemplate temp = new TemplateImpl();
- temp.execute();
- }
- }
参考文章:
1.策略模式_百度百科
2.《JAVA与模式》26天系列—第16天—策略模式
3.Java之模板方法模式
0 0
- 模板模式和策略模式的区别
- 模板模式和策略模式的区别
- 模板模式和策略模式的区别
- 模板模式和策略模式的区别
- 模板模式和策略模式的区别
- 模板模式和策略模式的区别
- 模板模式和策略模式的区别
- 模板模式和策略模式的区别
- 模板模式和策略模式的区别【转】
- java模式之策略模式&模板模式 以及策略模式与模板模式的区别
- 模板模式 和 策略模式的区别, builder pattern构造者模式,BeanDefinitionBuilder
- 模板和策略模式
- 模板模式和策略模式
- 工厂模式和策略模式的区别
- 代理模式和策略模式的区别
- 策略模式和工厂模式的区别
- 策略模式和工厂模式的区别
- 工厂模式和策略模式的区别
- LeetCode || Majority Element
- Android Broadcast、content porvider和AIDL的区别和场景
- 搭建Eclipse PHP开发环境
- 【linux】【实验楼学习】【01】学习路线、快捷键、基础介绍
- 我的一些简单的shell脚本实例
- 模板模式和策略模式的区别【转】
- Sliding Window Maximum
- 第k小数
- 使用JS-in-JS解释器陷阱执行来捕获恶意的网站脚本
- android 自定义控件属性步骤
- qt on android之GPS信号的获取
- 杭电oj 1002
- C/C++校招笔试面试经典题目总结六
- CENTOS6.3 配置防火墙,开启80端口、3306端口