---
title: "转载-Java设计模式之Decorator"
author: "RedArmyChen"
date: "2011-08-30T22:32:58Z"
tags: []
categories: ["Web前端"]
slug: "java-decorator"
draft: false
lastmod: "2011-08-30T22:32:58Z"
id: 1161893
url: "https://redarmychen.iteye.com/blog/1161893"
---

今天学员在学习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 进行了装饰