- 浏览: 778338 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
萨琳娜啊:
Java读源码之Netty深入剖析网盘地址:https://p ...
Netty源码学习-FileRegion -
飞天奔月:
写得有趣 ^_^
那一年你定义了一个接口 -
GoldRoger:
第二个方法很好
java-判断一个自然数是否是某个数的平方。当然不能使用开方运算 -
bylijinnan:
<script>alert("close ...
自己动手实现Java Validation -
paul920531:
39行有个bug:"int j=new Random ...
java-蓄水池抽样-要求从N个元素中随机的抽取k个元素,其中N无法确定
声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/
/* 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化 简单理解: 1、将不同的策略提炼出一个共同接口。这是容易的,因为不同的策略,只是算法不同,需要传递的参数(如果需要)和返回值(如果有)都是一样的 2、需要用到策略的类,持有这个接口。在计算时,就可以转调策略接口来实现 大体框架是这样: interface IStrategy { double calculatePrice(double primeCost); } class Context{ private IStrategy strategy; 由于这里是面向接口,所有切换策略很方便 public double caculate(double primeCost) { return strategy.caculatePrice(primeCost); } } …… …… 策略模式的缺点也很易见:策略太多,会造成有多个策略类 */ /** * 根据不同的优惠政策计算价格 * @param primeCost 商品原价 */ interface IStrategy { double calculatePrice(double primeCost); } class NormalCustomerStrategy implements IStrategy { public double calculatePrice(double primeCost) { System.out.println("对于普通用户,不打折,返回原价:"); return primeCost; } } class RegularCustomerStrategy implements IStrategy { public double calculatePrice(double primeCost) { System.out.println("对于老客户,打95折:"); return primeCost * (1 - 0.05); } } //对应策略模式中的Context class Price { private IStrategy strategy; public Price(IStrategy strategy) { this.strategy = strategy; } //如果primeCost是作为类Price的一个field,那么给IStrategy的calculatePrice传递参数时候, //就是传递整个Price对象了 public double getRealPrice(double primeCost) { return this.strategy.calculatePrice(primeCost); } } /* * 作一个扩展: * 老客户在打95折的基础上,再便宜10块钱 * 另外,将商品原价作为Price类的一个field * 有两种实现 */ interface IStrategyB { //传递整个Context对象 double calculatePrice(PriceContext priceContext); } class PriceContext { private IStrategyB strategy; private double primeCost; //将商品原价作为Price类的一个field public PriceContext(IStrategyB strategy, double primeCost) { this.strategy = strategy; this.primeCost = primeCost; } public double getPrimeCost() { return this.primeCost; } public double getRealPrice() { //这里是把整个Context传递进去了 return this.strategy.calculatePrice(this); } } //”老客户打95折“ class RegularCustomerStrategyB implements IStrategyB { public double calculatePrice(PriceContext priceContext) { double primeCost = priceContext.getPrimeCost(); return primeCost * (1 - 0.05); } } //”再便宜10块钱“ //1.extends strategy.要额外减少的数据来自strategy class HappyRegularCustomerStrategyB extends RegularCustomerStrategyB { private double diff; public HappyRegularCustomerStrategyB(double diff) { this.diff = diff; } public double calculatePrice(PriceContext priceContext) { System.out.println("对老客户实行打折后再优惠的策略。差价来自Strategy:"); return super.calculatePrice(priceContext) - diff; } } //2.extends Context.要额外减少的数据来自Price(Context)。这样,所有数据都来自Context,较为统一 class HappyPriceContext extends PriceContext { private double diff; public HappyPriceContext(IStrategyB strategy, double primeCost, double diff) { super(strategy, primeCost); this.diff = diff; } public double getRealPrice() { System.out.println("对老客户实行打折后再优惠的策略。差价来自Context:"); return super.getRealPrice() - diff; } } /** * 书上提到策略模式的一个应用:“容错恢复机制” * 正常情况下是一种策略,出错了又是另一种策略 * 书上给的例子是,记录日志到数据库,若出错了(例如连不上数据库),那先记录到文件中,以后再保存到数据库 */ interface ILogStrategy { void log(String message); } class DatabaseLogStrategy implements ILogStrategy { public void log(String message) { //模拟出错的情况 if (message != null && message.length() > 5) { int i = 5 / 0; } System.out.println("将以下日志信息记录到数据库中:" + message); } } class FileSystemLogStrategy implements ILogStrategy { public void log(String message) { System.out.println("将以下日志信息记录到文件中:" + message); } } //这里策略的选择在Context里实现 class LogContext { //private ILogStrategy strategy; public void log(String message) { ILogStrategy databaseStrategy = new DatabaseLogStrategy(); try { databaseStrategy.log(message); } catch (Exception e) { System.out.print("出错了,将日志信息先保存在文件中:"); ILogStrategy fileStrategy = new FileSystemLogStrategy(); fileStrategy.log(message); } } } //书上最后提到了策略模式和模板模式结合使用的方法:在模板中定义业务算法的“骨架”,具体算法(策略)由子类实现 /** * 这个类是用来测试的 */ public class StrategyPattern { public static void main(String[] args) { //测试打折策略 double primeCost = 1000.0; IStrategy strategy = new NormalCustomerStrategy(); Price price = new Price(strategy); double realPrice = price.getRealPrice(primeCost ); System.out.println(realPrice); strategy = new RegularCustomerStrategy(); price = new Price(strategy); realPrice = price.getRealPrice(primeCost); System.out.println(realPrice); System.out.println(); //测试打折再减差价的策略 double diff = 10.0; HappyRegularCustomerStrategyB happyStrategyB = new HappyRegularCustomerStrategyB(diff); //差价来自Strategy PriceContext priceContext = new PriceContext(happyStrategyB, primeCost); realPrice = priceContext.getRealPrice(); System.out.println(realPrice); RegularCustomerStrategyB strategyB = new RegularCustomerStrategyB(); priceContext = new HappyPriceContext(strategyB, primeCost, diff); //差价来自Context realPrice = priceContext.getRealPrice(); System.out.println(realPrice); System.out.println(); //测试记录日志 LogContext logContext = new LogContext(); logContext.log("hello"); logContext.log("hellooo"); } }
发表评论
-
J2EE设计模式-Intercepting Filter
2013-11-27 16:56 1500Intercepting Filter类似于职责链模式 有两种 ... -
《重构,改善现有代码的设计》第八章 Duplicate Observed Data
2012-12-04 20:34 1466import java.awt.Color; impor ... -
读《研磨设计模式》-代码笔记-状态模式-State
2012-10-07 16:56 1383声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-访问者模式-Visitor
2012-10-06 23:43 1044声明: 本文只为方便我 ... -
读《研磨设计模式》-代码笔记-命令模式-Command
2012-10-06 23:40 1164声明: 本文只为方便我 ... -
读《研磨设计模式》-代码笔记-适配器模式-Adapter
2012-09-26 00:23 1366声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-外观模式-Facade
2012-09-25 23:46 1004声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-解释器模式-Interpret
2012-09-23 23:55 1198声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-备忘录模式-Memento
2012-09-23 00:05 1716声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-享元模式-Flyweight
2012-09-21 21:01 1174声明: 本文只为方便我 ... -
读《研磨设计模式》-代码笔记-抽象工厂模式-Abstract Factory
2012-09-21 20:59 955声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-职责链模式-Chain Of Responsibility
2012-09-15 17:57 1602声明: 本文只为方便我 ... -
读《研磨设计模式》-代码笔记-装饰模式-Decorator
2012-09-15 17:56 1156声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-桥接模式
2012-09-12 18:27 1340声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-工厂方法模式
2012-08-24 16:57 1138声明: 本文只为方便我 ... -
读《研磨设计模式》-代码笔记-简单工厂模式
2012-08-24 16:56 1086声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-组合模式
2012-08-23 18:13 873声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
读《研磨设计模式》-代码笔记-模板方法模式
2012-08-23 18:12 1255声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ... -
式工工工工
2012-08-23 00:09 0package pattern; //=========== ... -
读《研磨设计模式》-代码笔记-原型模式
2012-08-21 19:34 1081声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移 ...
相关推荐
研磨设计模式-配套源代码研磨设计模式-配套源代码
研磨设计模式-配套源代码.rar
研磨设计模式-配套源代码.7z
研磨设计模式第二版-配套源代码 UTF-8格式 研磨设计模式第二版-配套源代码 UTF-8格式
“1.1 设计模式是什么 1.1.1 什么是模式 从字面上理解,模,就是模型、模板的意思;式,就是方式、方法的意思。综合起来,所谓模式就是:可以作为模型或模板的方式或方法。... “研磨设计模式”。 iBooks.
设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。 使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;...
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
1:本源代码是《研磨设计模式》一书的配套源代码 2:每个模式的示例源代码放在一个单独的文件夹下,以该模式的英文名称命名 3:每个模式下分成多个example,按照书的示例顺序分别命名为example1、example2.........
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
单例模式、工厂方法模式、策略模式、命令模式和桥接模式。
第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式(Memento) 第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式...
第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式(Memento) 第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式...
第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式(Memento) 第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式...
研磨设计模式 源代码 书上面的例子都在里面
研磨设计模式的配套源码,请下载,不要想了,解压后,直接放到eclipse下面即可运行!
研磨设计模式全部源代码,个人收集,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式(Memento) 第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式(Decorator) 第23章 职责链模式...
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...