大话设计模式六:装饰模式(穿什么有那么重要吗)
来源:互联网 发布:淘宝数据魔方多少钱 编辑:程序博客网 时间:2024/04/29 10:39
装饰模式
动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
装饰模式是为已有功能动态地添加更多功能的一种方式。
装饰模式把每个要装饰的功能放在单独的类中,并让这个类包含它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地,按顺序地使用装饰功能包装对象了。
装饰模式的优点是:
1.把类中的装饰功能从类中搬移去除,这样可以简化原有的类。
2.有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。
装饰器相当于一个累加的过程,c累加到d1,再把d1累加到d2,类似于d2(d1(c))
package Decorator;public class MainClass {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPerson xc = new Person("jack");System.out.println("First decoration");Sneakers sneakers = new Sneakers();BigTrouser bigTrouser = new BigTrouser();TShirts tShirts = new TShirts();sneakers.Decorate(xc);bigTrouser.Decorate(sneakers);tShirts.Decorate(bigTrouser);tShirts.Show();}}//ConcreteComponent classclass Person {public Person() {}private String name;public Person(String name) {this.name = name;}public void Show() {System.out.println("装扮的"+this.name);}}//Decoratorclass Finery extends Person {protected Person component;public void Decorate(Person component) {this.component = component;}@Overridepublic void Show() {// TODO Auto-generated method stubif (component != null) {component.Show();}}}//ConcreteDecoratorclass TShirts extends Finery {@Overridepublic void Show(){System.out.println("T-shirt");super.Show();}}class BigTrouser extends Finery {@Overridepublic void Show() {System.out.println("big trouser");super.Show();}}class Sneakers extends Finery {@Overridepublic void Show() {System.out.println("sneaker");super.Show();}}class Suit extends Finery {@Overridepublic void Show() {System.out.println("suit");super.Show();}}class Tie extends Finery {@Overridepublic void Show() {System.out.println("Tie");super.Show();}}class LeatherShoes extends Finery {@Overridepublic void Show() {System.out.println("leatherShoes");super.Show();}}
- 大话设计模式六:装饰模式(穿什么有那么重要吗)
- 《大话设计模式》之--第6章 穿什么有这么重要?----装饰模式
- 《大话设计模式》之--第6章 穿什么有这么重要?----装饰模式
- 第6章 穿什么有这么重要?-装饰模式 大话设计模式
- 《大话设计模式》--穿什么有这么重要?--装饰模式<Decorator>(6)
- 大话设计模式-第06章 穿什么有这么重要?--装饰模式
- 《大话设计模式》——读后感 (6)穿什么有这么重要?——装饰模式
- 【设计模式】穿什么有这么重要? --- 装饰模式
- 《大话设计模式》——读后感 (3)穿什么有这么重要?——装饰模式之理论实例(1)
- 《大话设计模式》——读后感 (3)穿什么有这么重要?——装饰模式之理解实例(2)
- 第六章 穿什么有这么重要-装饰模式(读书笔记)
- 第六章 穿什么有这么重要---装饰模式(读书笔记)
- 装饰模式-Python版---穿什么有这么重要?
- 穿什么有这么重要吗?————装饰模式
- 第六章 穿什么有这么重要吗?—装饰模式
- 大话设计模式(四)装饰模式
- 大话设计模式-装饰模式
- 大话设计模式-装饰模式
- Apache Commons fileUpload实现文件上传
- Effective STL
- C++内存回收
- lucene 简单的例子
- 优化系统服务提升Win7系统运行速度
- 大话设计模式六:装饰模式(穿什么有那么重要吗)
- JAVA 配置
- 在RedHat中如何确定CPU的个数
- 印刷电路板设计及其相关问题介绍
- 2013年9月9日、面向对象(包)|多线程|/调用run和调用start有什么区别|例举
- 如何在ASPX中将word文件输出到客户端浏览器
- KDB支持单步调试功能(ARM架构)
- 苹果真机模拟遇到的问题以及解决办法
- Python学习笔记1:Python起步