今天学员在学习java的时候提到了Decorator其设计模式,在网找到了这样一个案例,还不错,给学员分享一下!
Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样。因此,装饰器模式具有如下的特征:
它必须具有一个装饰的对象。
它必须拥有与被装饰对象相同的接口。
它可以给被装饰对象添加额外的功能。
用一句话总结就是:保持接口,增强性能。
装饰器通过包装一个装饰对象来扩展其功能,而又不改变其接口,这实际上是基于对象的适配器模式的一种变种。它与对象的适配器模式的异同点如下。
相同点:都拥有一个目标对象。
不同点:适配器模式需要实现另外一个接口,而装饰器模式必须实现该对象的接口。
Sourcable类的源代码如程序 12-22 所示,其定义了一个接口函数 operation() 。
程序12-22 源接口 Sourcable.java
Java代码
1.package pattern.decorator;
2.
3.public interface Sourcable {
4. public void operation();
5.
6.}
package pattern.decorator;
public interface Sourcable {
public void operation();
}
(2 ) Source.java 是 Sourcable.java 的一个实现,其函数 operation() 负责往控制台输出一个字符串:原始类的方法。其源代码如程序 12-23 所示。
程序12-23 源类 Source.java
Java代码
1.package pattern.decorator;
2.
3.public class Source implements Sourcable {
4.
5. public void operation() {
6. System.out.println("原始类的方法");
7. }
8.
9.}
package pattern.decorator;
public class Source implements Sourcable {
public void operation() {
System.out.println("原始类的方法");
}
}
(3 )装饰器类 Decorator1.java 采用了典型的对象适配器模式,它首先拥有一个 Sourcable 对象 source ,该对象通过构造函 数进行初始化。然后它实现了 Sourcable.java 接口,以期保持与 source 同样的接口,并在重写的 operation() 函数中调用 source 的 operation() 函数,在调用前后可以实现自己的输出,这就是装饰器所扩展的功能。其源代码如程序 12-24 所示。
程序12-24 装饰器类 Decorator1.java
Java代码
1.package pattern.decorator;
2.
3.public class Decorator1 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator1(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10.
11. public void operation() {
12. System.out.println("第一个装饰器前");
13. sourcable.operation();
14. System.out.println("第一个装饰器后");
15.
16. }
17.
18.}
package pattern.decorator;
public class Decorator1 implements Sourcable {
private Sourcable sourcable;
public Decorator1(Sourcable sourcable){
super();
this.sourcable=sourcable;
}
public void operation() {
System.out.println("第一个装饰器前");
sourcable.operation();
System.out.println("第一个装饰器后");
}
}
装饰器类Decorator2.java 是另一个装饰器,不同的是它装饰的内容不一样,即输出了不同的字符串。其源代码如程序 12-25 所示。
程序12-25 装饰器类 Decorator2.java
Java代码
1.package pattern.decorator;
2.
3.public class Decorator2 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator2(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10. public void operation() {
11. System.out.println("第二个装饰器前");
12. sourcable.operation();
13. System.out.println("第二个装饰器后");
14.
15. }
16.
17.}
package pattern.decorator;
public class Decorator2 implements Sourcable {
private Sourcable sourcable;
public Decorator2(Sourcable sourcable){
super();
this.sourcable=sourcable;
}
public void operation() {
System.out.println("第二个装饰器前");
sourcable.operation();
System.out.println("第二个装饰器后");
}
}
装饰器类Decorator1.java 是第三个装饰器,不同的是它装饰的内容不一样,即输出了不同的字符串。其源代码如程序 12-26 所示。
程序12-26 装饰器类 Decorator3.java
Java代码
1.package pattern.decorator;
2.
3.public class Decorator3 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator3(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10. public void operation() {
11. System.out.println("第三个装饰器前");
12. sourcable.operation();
13. System.out.println("第三个装饰器后");
14.
15. }
16.
17.}
package pattern.decorator;
public class Decorator3 implements Sourcable {
private Sourcable sourcable;
public Decorator3(Sourcable sourcable){
super();
this.sourcable=sourcable;
}
public void operation() {
System.out.println("第三个装饰器前");
sourcable.operation();
System.out.println("第三个装饰器后");
}
}
这时,我们就可以像使用对象的适配器模式一样来使用这些装饰器,使用不同的装饰器就可以达到不同的装饰效果。如程序12-27 所示,首先需要创建一 个源类对象 source ,然后根据将对象使用 Decorator1 来装饰,并以此使用 Decorator2 、 Decorator3 进行装饰,装饰后的对象 同样具有与 source 同样的接口。
程序12-27 测试类 DecoratorTest.java
Java代码
1.package pattern.decorator;
2.
3.public class DecoratorTest {
4.
5. /**
6. * @param args
7. */
8. public static void main(String[] args) {
9. Sourcable source = new Source();
10.
11. // 装饰类对象
12. Sourcable obj = new Decorator1(new Decorator2(new Decorator3(source)));
13. obj.operation();
14. }
15.
16.}
package pattern.decorator;
public class DecoratorTest {
/**
* @param args
*/
public static void main(String[] args) {
Sourcable source = new Source();
// 装饰类对象
Sourcable obj = new Decorator1(new Decorator2(new Decorator3(source)));
obj.operation();
}
}
运行该程序的输出如下:
第1 个装饰器装饰前
第2 个装饰器装饰前
第3 个装饰器装饰前
原始类的方法
第3 个装饰器装饰后
第2 个装饰器装饰后
第1 个装饰器装饰后
从输出的结果可以看出,原始类对象source 依次被 Decorator1 、 Decorator2 、 Decorator3 进行了装饰
分享到:
相关推荐
java设计模式之Builder&Decorator,java设计模式之Builder&Decorator
设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享元) 行为模式: 设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer 设计模式之Chain of Responsibility(职责链) 设计模式之...
前端开源库-pure-render-decorator纯渲染装饰,使反应组件纯。
NULL 博文链接:https://janeky.iteye.com/blog/472502
奇幻RPG(物品锻造 与 Decorator模式)本文中,我们通过一个常见的给武器(对象)添加宝石(额外的状态和行为)的例子,讨论了Decorator设计模式的实现过程。
C#设计模式之Decorator 装饰模式,pdf+视频教学,实例演示,易学易用~~
- 23种设计模式 - 工厂方法模式(Factory Method) - 抽象工厂模式(Abstract Factory) - 单例模式(Singleton) - 建造者模式(Builder) - 原型模式(Prototype) - 代理模式(Proxy) - 适配器模式(Adapter) - 装饰...
博文链接:https://your.iteye.com/blog/133420
java-io-decorator.pdf
java Decorator设计模式应用,可以简单了解它,初学者入。
20-装饰器使用_Decorator
这是JAVA设计模式中属于结构模式的部分,包括Flyweight(共享模式)、Bridge(桥模式)、Decorator(装饰模式)、Composite(组合模式)、Adapter(适配器模式)、Proxy(代理模式)、Facade (外观模式)的源代码。其中有些模式中...
Angular-rich-logger-decorator.zip,丰富的记录器类型脚本装饰器,便于编码和调试丰富的记录器装饰器,Angularjs于2016年发布,是Angularjs的重写版。它专注于良好的移动开发、模块化和改进的依赖注入。angular的设计...
Angular-apollo-storybook-decorator.zip,使用Apollo客户端包装您的故事书环境,使用GraphQlapolo故事书装饰器为独立的UI测试提供模拟,Angularjs于2016年发布,是Angularjs的重写版。它专注于良好的移动开发、模块化...
设计模式 t07Decorator
基础知识 基础知识设计模式概述 从招式与内功谈起——设计模式概述(一) 从招式与内功谈起——设计模式概述(二) 从招式与内功谈起——设计模式概述(三) 面向对象设计原则 面向对象设计原则之单一职责原则 面向...
学习java 装饰模式(decorator)非常好的例子
Java-从Decorator,Adapter模式看Java/IO 讲得很详细