一:定义:
Decorator:Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
二:引入
假设现在有一家咖啡店,经营咖啡,茶等饮料,我们来为它设计一个系统。
问题:
- 饮料加糖或牛奶等调料时是要另收费的(每包1元),顾客可以要也可以不要,所以这个固定价格的cost方法不符合要求. 不符合OCP(open for extension,close for modification)
重新设计:
很显然,这种设计也不合适,会导致
- 类数量爆炸性增长(class exploson).
- 也不符合要求,比如有的顾客要两包糖等情况没有考虑到.
引入Decorator设计模式:
public abstract class Beverage ...{
private String discription;
public String getDiscription() ...{
return discription;
}
public void setDiscription(String discription) ...{
this.discription = discription;
}
public abstract double cost() ;
}
public class Coffee extends Beverage...{
public Coffee()
...{
setDiscription("coffee");
}
public double cost()
...{
return 10;
}
}
public abstract class CondimentDecorator extends Beverage...{
public abstract String getDiscription() ;
}
public class CondimentMilk extends CondimentDecorator...{
private Beverage beverage;
public CondimentMilk(Beverage beverage)
...{
this.beverage=beverage;
}
public double cost()
...{
return beverage.cost()+1;
}
public String getDiscription()
...{
return beverage.getDiscription()+",milk";
}
}
public class Client ...{
public static void main(String args[])
...{
Beverage coffee=new Coffee();
System.out.println(coffee.getDiscription()+":"+coffee.cost());
System.out.println(new CondimentMilk(coffee).getDiscription()+":"+new CondimentMilk(coffee).cost());
System.out.println(new CondimentSugar(new CondimentMilk(coffee)).getDiscription()+":"+new CondimentSugar( new CondimentMilk(coffee)).cost());
System.out.println(new CondimentSugar(new CondimentSugar(new CondimentMilk(coffee))).getDiscription()+":"+new CondimentSugar(new CondimentSugar( new CondimentMilk(coffee))).cost());
}
}
三:结构
四:实际应用
- JAVA I/O 系统是decorator模式的典型应用
我们自己来写一个InputStream decorator,用来使所有的输出字符变成小写.
public class LowerCaseInputStream extends FilterInputStream ...{
protected LowerCaseInputStream(InputStream in) ...{
super(in);
}
public int read() throws IOException
...{
int c=super.read();
return (c==-1?c:Character.toLowerCase(c));
}
}
public class IoTest ...{
public static void main(String args[]) ...{
int c;
String filePath = "f:/temp/designPatten.txt";
try ...{
InputStream in = new LowerCaseInputStream(new BufferedInputStream(new FileInputStream(
filePath)));
while(( c=in.read())!=-1)
...{
System.out.print((char)c);
}
in.close();
} catch (FileNotFoundException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
}
} 五:适用情形
Use Decorator
- to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
- for responsibilities that can be withdrawn.
- when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing.
参考文献:
1:阎宏,《Java与模式》,电子工业出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY
分享到:
相关推荐
c++设计模式-结构型模式-装饰器模式;QT工程;...装饰器(Decorator)模式的定义:指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。
C#面向对象设计模式纵横谈(10):Decorator 装饰模式(结构型模式) (Level 300)
C#面向对象设计模式纵横谈(10):Decorator 装饰模式(结构型模式)
在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要... 这是第10节:结构型模式Decorator装饰模式
5、装饰模式(Decorator) 用意:动态地给一个对象增加其它职责,比继承实现灵活。
4.4 Decorator(装饰)—对象结构型模式 115 4.5 FACADE(外观)—对象结构型模式 121 4.6 Flyweight(享元)—对象结构型模式 128 4.7 Proxy(代理)—对象结构型模式 137 4.8 结构型模式的讨论 144 4.8.1 Adapter ...
4.4 DECORATOR(装饰)——对象结构型模式 4.5 FACADE(外观)——对象结构型模式 4.6 FLYWEIGHT(享元)——对象结构型模式 4.7 PROXY(代理)——对象结构型模式 4.8 结构型模式的讨论 第五章 行为模式 5.1 CHAIN ...
NULL 博文链接:https://jacky-dai.iteye.com/blog/1132229
装饰者模式(Decorator Pattern)是一种结构型设计模式,它的定义是在不改变原有对象结构的基础上,动态地给该对象增加一些职责(即增加其额外功能)。这种模式允许向一个现有的对象添加新的功能,同时又不改变其...
结构型模式 Adapter-------->适配器模式 Bridge-------->桥接模式 Composite-------->组合模式 Decorator-------->装饰模式 Facade-------->外观模式 FlyWeight-------->享元模式 Proxy--------&...
装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令...
七个结构型模式 七个结构型模式 适配器模式-Adapter Pattern 不兼容结构的协调——适配器模式(一) 不兼容结构的协调——适配器模式(二) 不兼容结构的协调——适配器模式(三) 不兼容结构的协调——适配器模式...
C#面向对象设计模式纵横谈(10):Decorator 装饰模式(结构型模式) C#面向对象设计模式纵横谈(11):Facade 外观模式(结构型模式) C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式) C#面向对象设计...
4.8 结构型模式的讨论 144 4.8.1 Adapter与Bridge 144 4.8.2 Composite、Decorator与Proxy 145 第5章 行为模式 147 5.1 CHAIN OF RESPONSIBIL ITY(职责链) —对象行为型模式 147 5.2 COMMAND(命令)—对象行为型 ...
在书中,作者们将这二十三种设计模式分成了三类,分别是创建型模式、结构型模式和行为型模式。 创建型模式包含了: 工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ...
在书中,作者们将这二十三种设计模式分成了三类,分别是创建型模式、结构型模式和行为型模式。 创建型模式包含了: 工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ...
在书中,作者们将这二十三种设计模式分成了三类,分别是创建型模式、结构型模式和行为型模式。 创建型模式包含了: 工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ...