第六章 装饰模式
2016-03-30 23:19
295 查看
装饰模式将类的装饰功能从类中分离出来,只保留了类的核心职责,装饰逻辑(顺序)由调用者选择分配。
/** * Created by hero on 16-3-30. */ public abstract class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Person() { } public Person(String name) { this.name = name; } public abstract void show(); } /** * Created by hero on 16-3-30. */ public class Finery extends Person { protected Person person; public void Decorate(Person person) { this.person = person; } @Override public void show() { if (this.person != null) { person.show(); } } } /** * Created by hero on 16-3-30. */ public class TShirt extends Finery { public void show() { super.show(); System.out.println("T-shirt"); } } /** * Created by hero on 16-3-30. */ public class Trouser extends Finery { public void show() { super.show(); System.out.println("Trouser"); } } /** * Created by hero on 16-3-30. */ public class ConcretePerson extends Person { public ConcretePerson(String name) { setName(name); } @Override public void show() { System.out.println(getName()); } } public class Main { public static void main(String[] args) { ConcretePerson person = new ConcretePerson("小明"); TShirt tShirt = new TShirt(); Trouser trouser = new Trouser(); tShirt.Decorate(person); trouser.Decorate(tShirt); trouser.show(); } }
相关文章推荐
- Spring MVC PageNotFound.noHandlerFound No mapping found for HTTP request with URI
- [Offer收割]编程练习赛3 - 题目3 : 智力竞赛
- DOSBox下编译和运行NASM代码
- hdu4307 Matrix
- 将Windows文件路径转换为java中可识别的文件路径
- 张国祥老师在宁波讲授《如何当好部门经理》实战课程取得圆满成功
- Java泛型深入理解
- zyUpload---照片上传并显示效果
- 内联函数和宏定义的总结
- Unity连接MySQL数据库
- Tarjan算法详解
- 关于alter table move
- flow的简单使用
- C/C++中的预编译指令
- 软件工程_东师站_第四周作业
- python使用httplib2访问REST服务的例子
- 1.3-puppet安装
- 视频播放器(二)——播放列表
- Qt实现记事本的打开和保存功能(第一个实用的Qt小程序)
- Nginx下的https配置