装饰者模式

来源:互联网 发布:中点画圆算法流程图 编辑:程序博客网 时间:2024/05/17 10:54
【0】README
0.1)本文部分文字描述转自: “head first设计模式”,旨在学习  装饰者模式 的基础知识;
0.2)装饰者模式的源代码参见:装饰者模式

【1】星巴克荔枝(这里的星巴克仅仅是做个模拟的用途)
1.1)星巴克咖啡订单计费系统类图:

1.2)problem:购买咖啡时,也可以要求加入各种调料,如:蒸奶,豆浆等。星巴克会工具加入的调料来收取不同的费用;而计费方法cost()又在 调料类中,所以就需要 各种调料都要去继承 Beverage 类,这样整个订单系统的类将会很多,而且显得很冗杂。


【2】设计原则: 类应该对扩展开放,对修改关闭(干货——开放关闭原则)
2.1)解释:目标是允许类容易扩展,在不修改现有代码的情况下,就可以搭配新的行为。
2.2)注意事项:在选择需要被扩展的代码部分时要小心,每个地方都采用开放—关闭原则,是一种浪费,也没有必要,还会导致代码变得复杂且难以理解;(干货——每个地方都采用开放—关闭原则,是一种浪费)

【3】认识装饰者模式
3.1)如何设计订单计费系统:我们要以饮料为主体,然后用调料来装饰饮料。
3.2)看个荔枝: 顾客想要摩卡和奶泡深焙咖啡,那么需要做的是(steps):
step1)拿一个深焙咖啡对象;
step2)以摩卡(Mocha)对象装饰它;
step3)以奶泡(Whip)对象装饰它;
step4)调用cost() 方法进行计费,并依赖委托将调料的价钱加上去;
3.3)计费大致流程如下:

step1)调用最外圈装饰者Whip的cost方法;
step2)Whip 调用Mocha的cost方法;
step3)Mocha 调用DarkRoast的cost方法;
step4)DarkRoast返回它的价钱 $0.99;
step5)Mocha在DarkRoast的结果上,加上自己的价钱 $0.20,返回新价钱 $1.19;
step6)Whip 在Mocha 的结果上,加上自己的价钱 $0.10,返回新价钱 $1.29;

【4】整理我们所知道的
4.1)装饰者和被装饰者对象有相同的超类;
4.2)你可以用一个或多个装饰者包装一个对象;
4.3)因为装饰者和被装饰者有同样的基类,所以在任何需要原始对象(被装饰)对象的场合,都可以用装饰对象去替换它;
4.4)装饰者可以在所委托被装饰者的行为之前或之后,加上自己的行为, 以达到特定的目的; (干货——装饰者在被装饰者上添加行为)
4.5)对象可以在任何时候被装饰,所以可以在运行时动态地,不限量地用你喜欢的装饰者来装饰对象;

【5】定义装饰者模式
5.1)定义: 装饰者模式动态地将责任附加到对象上。若要扩展对象功能,装饰者提供了比继承更有弹性的替代方案;
5.2)看看代码示例
Attention)总额为:一个house blend coffee==0.89,而两个Mocha==0.4,一个Whip=0.1,所以总额=1.39

// 调料类(装饰者)public abstract class CondimentDecorator extends Beverage{public abstract String getDesc();}
//具体的装饰者public class Whip extends CondimentDecorator{Beverage beverage;public Whip(Beverage b){this.beverage = b;}@Overridepublic String getDesc() {return beverage.getDesc()+ ", Whip";}@Overridepublic double cost() {return .10 + beverage.cost();}}
// 具体的装饰者public class Mocha extends CondimentDecorator{Beverage beverage;public Mocha(Beverage b){this.beverage = b;}@Overridepublic String getDesc() {return beverage.getDesc()+ ", Mocha";}@Overridepublic double cost() {return .20 + beverage.cost();}}
// 饮料类(被装饰者)public abstract class Beverage {String desc = "unknown beverage";public String getDesc(){return desc;}public abstract double cost();}
// 具体被装饰者public class HouseBlend extends Beverage{public HouseBlend(){desc = "house blend coffee";}@Overridepublic double cost() {return .89;} }
// 具体被装饰者public class Espresso extends Beverage{public Espresso(){desc = "Espresso";}@Overridepublic double cost() {return 1.99;} }

public class Starbucks {public static void main(String[] args) {Beverage beverage = new Espresso();System.out.println(beverage.getDesc() + " $" + beverage.cost());Beverage beverage2 = new HouseBlend();beverage2 = new Mocha(beverage2);beverage2 = new Mocha(beverage2);beverage2 = new Whip(beverage2);System.out.println(beverage2.getDesc() + " $" + beverage2.cost());}}
【6】java IO中应用到的装饰者模式
6.1)看个荔枝:下面是一个典型的对象集合,用装饰者来将功能结合起来,以读取文件数据:

对上图的分析(Analysis):
A1)LineNumberInputStream:是一个装饰者,他加上了计算行数的能力;
A2)BufferedInputStream:是一个具体的装饰者,它加入两种行为:利用缓冲输入来改进性能;用一个readline()方法(用来一次读取一行文本输入数据)来增强功能;
A3)FileInputStream:是被装饰的组件。java IO 程序提供了几个组件,包括FileInputStream, StringBufferInputStream,ByteArrayInputStream。。。等。这些类都提供了最基本的字节读取功能;
6.2) 装饰java.io 类 

6.3)java IO也引出装饰者模式的一个缺点: 利用装饰者模式,常常造成设计中有大量的小类,数量实在太多,可能会造成使用此 API 程序员的困扰。但是,现在你已经了解了装饰者的工作原理,所以当我们使用别人的大量装饰的API时,就可以很容易地判别出他们的装饰者类是如何组织的,以方便用包装方式取得想要的行为;
6.4)编写自己的 java IO 装饰者
public class LowerCaseInputStream extends FilterInputStream {protected LowerCaseInputStream(InputStream in) {super(in);}/** * 下面定义了两个read方法,一个针对字节,一个针对字节数组; */public int read() throws IOException {int c = super.read();return (c == -1 ? c : Character.toLowerCase((char) c));}public int read(byte[] b, int offset, int len) throws IOException {int result = super.read(b, offset, len);for (int i = offset; i < offset + result; i++) {b[i] = (byte) Character.toLowerCase((char) b[i]);}return result;}}
public class InputTest {public static void main(String[] args) {int c;String path = System.getProperty("user.dir") + File.separator + "src"+ File.separator + "com" + File.separator + "designpattern"+ File.separator + "chapter3" + File.separator;try {InputStream in = new LowerCaseInputStream(new BufferedInputStream(new FileInputStream(path + "test.txt")));while ((c = in.read()) >= 0)System.out.print((char) c);in.close();} catch (Exception e) {e.printStackTrace();}}}
【7】装饰者模式——动态地将责任附加到对象上。想要扩展功能,装饰者提供有别于继承的另一种选择;(干货——装饰者模式的定义)



0 0
原创粉丝点击