设计模式--装饰模式(Decorator)
2009-05-09 17:39
316 查看
装饰模式
概述
[/code]
概述
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
适用性
1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。 2.处理那些可以撤消的职责。 3.当不能采用生成子类的方法进行扩充时。
参与者
1.Component 定义一个对象接口,可以给这些对象动态地添加职责。 2.ConcreteComponent 定义一个对象,可以给这个对象添加一些职责。 3.Decorator 维持一个指向Component对象的指针,并定义一个与Component接口一致的接口。 4.ConcreteDecorator 向组件添加职责。
Example
Component public interface Person { void eat(); } ConcreteComponent public class Man implements Person { public void eat() { System.out.println("男人在吃"); } } Decorator public abstract class Decorator implements Person { protected Person person; public void setPerson(Person person) { this.person = person; } public void eat() { person.eat(); } } ConcreteDecorator public class ManDecoratorA extends Decorator { public void eat() { super.eat(); reEat(); System.out.println("ManDecoratorA类"); } public void reEat() { System.out.println("再吃一顿饭"); } } public class ManDecoratorB extends Decorator { public void eat() { super.eat(); System.out.println("==============="); System.out.println("ManDecoratorB类"); } } Test public class Test { public static void main(String[] args) { Man man = new Man(); ManDecoratorA md1 = new ManDecoratorA(); ManDecoratorB md2 = new ManDecoratorB(); md1.setPerson(man); md2.setPerson(md1); md2.eat(); } } result 男人在吃 再吃一顿饭 ManDecoratorA类 =============== ManDecoratorB类
[/code]
相关文章推荐
- 设计模式 (十)装饰模式(Decorator)
- Java设计模式之再从[暗黑破坏神"装备镶嵌宝石系统"]分析装饰(Decorator)模式
- [转] 设计模式---装饰模式(Decorator)
- C#设计模式之 :Decorator模式(装饰模式)
- 设计模式--装饰模式(Decorator)
- java设计模式之装饰器模式(Decorator)
- 设计模式读书笔记之装饰器模式(Decorator)
- 设计模式之装饰模式(Decorator)
- SPRING与设计模式---装饰模式(Decorator)(包装器模式(Wrapper))
- 基于东北F4的设计模式情景剧——第一幕 装饰模式(Decorator Pattern)
- 设计模式_Decorator_装饰模式
- 设计模式-结构型-装饰模式(Decorator)
- 设计模式之美:Decorator(装饰)
- java 设计模式学习笔记九 decorator装饰模式
- 设计模式之装饰模式(Decorator)
- 设计模式c++实现(一):装饰(Decorator)模式
- 设计模式(八)装饰器模式Decorator(结构型)
- C#面向对象设计模式第十讲:Decorator 装饰模式(结构型模式)
- C#设计模式之装饰模式(Decorator Pattern)
- 设计模式(9)——Decorator(装饰)模式