IO和Decorator模式
2013-11-20 16:14
113 查看
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!刚才写了好长的blog,竟然网页有问题丢了,还找不回了!!!!!!!
气!
那就直接粘贴疯狂java联盟对Decorator模式的介绍!
装饰模式
[b]概述[/b]
[b]适用性[/b]
[b]参与者[/b]
[b]类图[/b]
[b]例子[/b]
Component
ConcreteComponent
Decorator
ConcreteDecorator
Test
result
气!
那就直接粘贴疯狂java联盟对Decorator模式的介绍!
装饰模式
[b]概述[/b]
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
[b]适用性[/b]
1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。 2.处理那些可以撤消的职责。 3.当不能采用生成子类的方法进行扩充时。
[b]参与者[/b]
1.Component 定义一个对象接口,可以给这些对象动态地添加职责。 2.ConcreteComponent 定义一个对象,可以给这个对象添加一些职责。 3.Decorator 维持一个指向Component对象的指针,并定义一个与Component接口一致的接口。 4.ConcreteDecorator 向组件添加职责。
[b]类图[/b]
[b]例子[/b]
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类
相关文章推荐
- (IO)Decorator模式
- Java IO (1) 装饰模式(Decorator)
- 装饰器模式(Decorator) java IO
- java.io.*中的设计模式之 装饰(Decorator)模式
- 从Decorator模式和Adapter来 java IO(一)
- Java IO 包中的Decorator模式
- java io中的Decorator模式
- JAVA IO 包中的Decorator模式
- 装饰设计模式(Decorator)-IO
- Java IO中的装饰器模式(Decorator)和适配器模式(Adapter)
- 用io包讲解Decorator模式
- 第7讲 IO/输入与输出-Decorator涉及模式
- JAVA IO-适配器模式(Adapter)&装饰模式(Decorator)
- 设计模式-装饰模式(Decorator)在jDK中IO系统的应用
- Decorator模式在JAVA中的IO应用
- Java IO 包中的Decorator模式
- Decorator模式在java.io包中的应用
- java的IO模型与Decorator模式
- Java IO 包中的Decorator模式
- Java IO 包中的Decorator模式读后的想法