装饰者模式 | Decorator Pattern
来源:互联网 发布:java socket拒绝ip连接 编辑:程序博客网 时间:2024/05/27 19:27
装饰者模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
装饰模式的特点:
- 装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
- 装饰对象包含一个真实对象的引用(reference)
- 装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
- 装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。
适用性:
以下情况使用Decorator模式
- 需要扩展一个类的功能,或给一个类添加附加职责。
- 需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
- 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
- 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
java IO 流是典型的装饰模式。
示例:
//定义被装饰者 public interface Human { public void wearClothes(); public void walkToWhere(); } //定义装饰者 public abstract class Decorator implements Human { private Human human; public Decorator(Human human) { this.human = human; } public void wearClothes() { human.wearClothes(); } public void walkToWhere() { human.walkToWhere(); } } //下面定义三种装饰,这是第一个,第二个第三个功能依次细化,即装饰者的功能越来越多 public class Decorator_zero extends Decorator { public Decorator_zero(Human human) { super(human); } public void goHome() { System.out.println("进房子。。"); } public void findMap() { System.out.println("书房找找Map。。"); } @Override public void wearClothes() { // TODO Auto-generated method stub super.wearClothes(); goHome(); } @Override public void walkToWhere() { // TODO Auto-generated method stub super.walkToWhere(); findMap(); } } public class Decorator_first extends Decorator { public Decorator_first(Human human) { super(human); } public void goClothespress() { System.out.println("去衣柜找找看。。"); } public void findPlaceOnMap() { System.out.println("在Map上找找。。"); } @Override public void wearClothes() { // TODO Auto-generated method stub super.wearClothes(); goClothespress(); } @Override public void walkToWhere() { // TODO Auto-generated method stub super.walkToWhere(); findPlaceOnMap(); } } public class Decorator_two extends Decorator { public Decorator_two(Human human) { super(human); } public void findClothes() { System.out.println("找到一件D&G。。"); } public void findTheTarget() { System.out.println("在Map上找到神秘花园和城堡。。"); } @Override public void wearClothes() { // TODO Auto-generated method stub super.wearClothes(); findClothes(); } @Override public void walkToWhere() { // TODO Auto-generated method stub super.walkToWhere(); findTheTarget(); } } //定义被装饰者,被装饰者初始状态有些自己的装饰 public class Person implements Human { @Override public void wearClothes() { // TODO Auto-generated method stub System.out.println("穿什么呢。。"); } @Override public void walkToWhere() { // TODO Auto-generated method stub System.out.println("去哪里呢。。"); } } //测试类,看一下你就会发现,跟java的I/O操作有多么相似 public class Test { public static void main(String[] args) { Human person = new Person(); Decorator decorator = new Decorator_two(new Decorator_first( new Decorator_zero(person))); decorator.wearClothes(); decorator.walkToWhere(); } }
阅读全文
0 0
- Decorator Pattern (装饰者模式)
- 装饰者模式(Decorator Pattern)
- 装饰者模式(decorator pattern)
- 装饰者模式(Decorator Pattern)
- 装饰者模式(Decorator Pattern)
- Decorator Pattern 装饰者模式
- 装饰者模式(Decorator Pattern)
- 装饰者模式 Decorator Pattern
- Decorator Pattern(装饰者模式)
- 装饰者模式-Decorator Pattern
- 装饰者模式 | Decorator Pattern
- 装饰模式Decorator Pattern
- 装饰模式(Decorator Pattern)
- 装饰模式(Decorator Pattern)
- 装饰模式--Decorator pattern
- 装饰模式 Decorator Pattern
- 装饰模式【DECORATOR PATTERN 】
- 装饰模式【Decorator Pattern】
- Pandas
- 内存管理
- 程序员必读书单
- set集合容器
- 使用 Rest-Assured 测试 REST API
- 装饰者模式 | Decorator Pattern
- 希尔排序
- Unity自定义UI组件(十一) 雷达图、属性图
- 打造完美的 Ubuntu16.04 开发环境
- Nginx错误ngx_slab_alloc() failed: no memory in cache keys zone "nginx_uwsgi_cache"解决方法
- Mybatis Oracle 批量导入
- Java+opencv3.2.0之中值滤波
- [算法与数据结构]
- C++虚函数和纯虚函数的区别