---
title: "[3]设计模式——装饰器模式"
author: "tsface"
date: "2014-03-30T17:52:30Z"
tags: []
categories: ["编程语言"]
slug: "3"
draft: false
lastmod: "2014-03-30T17:54:48Z"
id: 2038547
url: "https://tsface.iteye.com/blog/2038547"
---

装饰器模式

 

       ***装饰器模式（Decorator），动态的给一个对象添加一些额外的职责，就增加功能来说，装饰器模式比生成子类更加灵活。***

 

      类图：

 

![](http://dl2.iteye.com/upload/attachment/0095/5250/3db5b0e6-43ec-3875-80c8-dfe95cdf5c60.jpg)

 

 

 

      *Component：定义一个组件接口，可以给这些对象动态的添加职责*

      *ConcreteComponent：定义了一个具体的组件*

      *Decorator：装饰接口，持有一个对组件的引用，从外类来扩展组件的功能。*

      *ConcreteDecoratorA：具体的装饰对象，对组件进行装饰：*

 

      

     Component接口：

      

```
/*
 * @(#)Component.java 2014-3-30
 */
package com.decorator;

/**
 * 定义一个对象接口，可以给这些对象动态的添加职责
 * @author liyan
 * @version 2014-3-30
 * @since 1.0
 */
public interface Component
{
	public void opreation();
}
```

 

    ConcreteComponent：

    

```
/*
 * @(#)ConcreteComponent.java 2014-3-30
 */
package com.decorator;

/**
 * 具体的对象
 * @author liyan
 * @version 2014-3-30
 * @since 1.0
 * @see
 */
public class ConcreteComponent implements Component
{

	@Override
	public void opreation()
	{
		System.out.println("具体的实现对象");
	}
	
}
```

 

     Decorator：

 

     

```
/*
 * @(#)Decorator.java 2014-3-30
 */
package com.decorator;

/**
 * 装饰器接口，维持一个组件对象的引用
 * @author liyan
 * @version 2014-3-30
 * @since 1.0
 * @see
 */
public class Decorator implements Component
{
	protected Component component;
	
	public Decorator(Component component)
	{
		this.component = component;
	}
	
	@Override
	public void opreation()
	{
		component.opreation();
		
	}
	
}
```

 

 

    装饰器A对象：

```
/*
 * @(#)ConcreteDecoratorA.java 2014-3-30
 */
package com.decorator;

/**
 * 描述当前类的作用
 * @author liyan
 * @version 2014-3-30
 * @since 1.0
 * @see
 */
public class ConcreteDecoratorA extends Decorator
{

	public ConcreteDecoratorA(Component component)
	{
		super(component);
	}

	@Override
	public void opreation()
	{
		System.out.println("ConcreteDecoratorA 装饰了当期组件");
		// TODO Auto-generated method stub
		super.opreation();
	}
	
}
```

 

 

       装饰器B对象：

 

 

```
package com.decorator;

public class ConcreteDecoratorB extends Decorator
{
	
	public ConcreteDecoratorB(Component component)
	{
		super(component);
	}

	@Override
	public void opreation()
	{
		System.out.println("B装饰器的实现 装饰了组件对象");
		super.opreation();
	}
	
}
```

 

   实现测试：

   

```
/*
 * @(#)Test.java 2014-3-30
 */
package com.decorator;

/**
 * 描述当前类的作用
 * @author liyan
 * @version 2014-3-30
 * @since 1.0
 * @see
 */
public class Test
{
	public static void main(String[] args)
	{
		Component component = new ConcreteComponent();
		
		//采用装饰器对组件进行“装饰”
		Decorator decoratorA = new ConcreteDecoratorA(component);
		Decorator decoratorB = new ConcreteDecoratorB(decoratorA);
		
		//调用A装饰器的装饰过程
		decoratorA.opreation();
		
		//同理调用B的
		decoratorB.opreation();
	}
}
```

 

 

    结果：

     

```
ConcreteDecoratorA 装饰了当期组件
具体的实现对象
B装饰器的实现 装饰了组件对象
ConcreteDecoratorA 装饰了当期组件
具体的实现对象
```

 

 

       **系统什么时候用装饰器：**

       当系统需要新增功能的时候，是向旧的类中添加新的代码。这些新增加的代码通常装饰了原有类的核心职责或者主要行为，在主类中加入新的字段，新的方法和新的逻辑，从而增加了主类的复杂度。而这些新增的东西仅仅是为了满足一些只有在特定情况下才会执行的特殊行为需求。**装饰器**为上述需求提供了一个较好的解决方案，*它把每个要装饰的功能放在单独的类中，并让这些类包含它所要装饰的对象，因此，当需要执行特殊行为的时候，客户端代码可以在运行时候根据需要有选择的，按照顺序的使用装饰包装类（组件）。*

 2014/03/30 tsface 南京

hanily@msn.com