Java装饰模式

装饰模式

           装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。

定义:动态给一个对象添加一些额外的职责,使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活。

 

设计初衷:通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,那么势必生成很多子类,增加系统的复杂性,同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,这些功能是编译时就确定了,是静态的。

装饰模式的结构

装饰模式使用原来被装饰的类的一个子类的实例,把客户端的调用委派到被装饰类。装饰模式的关键在于这种扩展是完全透明的。

装饰模式类图如下:



 
在装饰模式中的角色有:


    抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。

    具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。

    装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。

       ●  具体装饰(ConcreteDecorator)角色:负责给构件对象贴上附加的责任。

源代码:

 

/**
 * @author: Col.Mai
 * @date:2014-9-23 下午11:07:34
 * @description :<p>抽象角色构件</P>
 **/
public interface Component {
	
	void sampleOperation();
	
}
 
/**
 * @author: Col.Mai
 * @date:2014-9-23 下午11:08:40
 * @description :<p>具体构件角色</P>
 **/
public class ConcreteComponent implements Component {

	public void sampleOperation() {
		//TODO 写相关业务代码
	}

}
 
/**
 * @author: Col.Mai
 * @date:2014-9-23 下午11:09:58
 * @description :<p>装饰角色</P>
 **/
public class Decorator implements Component {

	private Component component;
	
	public void sampleOperation() {
		// 委派给构件
		component.sampleOperation();
	}
	public  Decorator(Component component){
		this.component = component;
	}
}
 
/**
 * @author: Col.Mai
 * @date:2014-9-23 下午11:13:36
 * @description :<p>具体装饰角色</P>
 **/
public class ConcreteDecorator extends Decorator {
 
	public ConcreteDecorator(Component component) {
		super(component);
	}

	@Override
	public void sampleOperation() {
		super.sampleOperation();
        // 写相关的业务代码
	}
} 
  •   在上面的装饰类里,有一个私有的属性component,其数据类型是构件(Component)
  •   此装饰类实现了构件(Component)接口
  •   接口的方法也值得注意,每一个实现类的方法都委派给父类,但并不单纯地委派,而是有功能增强。

下面举个简单的例子,我记的我小时候看过一个动画片叫《战神金刚》,当时非常喜欢看,放学后就盯着电视连饭都不吃,为此没少挨揍,这个动画片讲的是几个小朋友团队合作组成战神金刚消灭强大的敌人,在组成战神金刚前,他们分别是单独的对象,在组成时他们分别组成身体、头部、腿部、手部从而完成战神金刚(时间久远具体剧情也忘记了,组成部分只是靠回忆猜测,不要在意这些细节,主要是以此为例)。

类图如下:



 代码如下:

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:39:24
 * @description :<p>抽象构件角色(战神金刚)</P>
 **/
public interface Robot {
	/**
	 * 组成某个部位
	 */
	void composition();
}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:49:27
 * @description :<p>具体构件角色</P>
 **/
public class ConcreteRobot implements Robot{

	public void composition() {
        System.out.println("组成战神金刚!");
	}

}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午1:18:48
 * @description :<p>装饰者角色</P>
 **/
public class Decorator implements Robot{
	private Robot robot;
	
	public  Decorator(Robot robot){
		this.robot = robot;
	}
	public void composition() {
		robot.composition();
	}

}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:47:06
 * @description :<p>装饰身体</P>
 **/
public class Decorator4Body extends Decorator {


	public Decorator4Body(Robot robot) {
		super(robot);
	}

	public void composition() {
		 System.out.println("我来组成身体!");
		 super.composition();
	}
}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:47:28
 * @description :<p>装饰手部</P>
 **/
public class Decorator4Hand extends Decorator {


	public Decorator4Hand(Robot robot)  {
		super(robot);	
	}
	public void composition() {
		System.out.println("我来组成手部!");
		super.composition(); 
	}
}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:44:20
 * @description :  <p>  装饰头部   </P>
 **/
public class Decorator4Head extends Decorator {


	public Decorator4Head(Robot robot) {
		super(robot);
	}

	public void composition() {
		System.out.println("我来组成头部!");
		super.composition();
	}
}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:46:27
 * @description :<p>装饰腿部</P>
 **/
public class Decorator4Leg extends Decorator{


	public Decorator4Leg(Robot robot) {
		super(robot);
	}

	public void composition() {
		System.out.println("我来组成腿部!");
		super.composition(); 
	}

}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:55:34
 * @description :<p>xxx功能描述</P>
 **/
public class Client {
	public static void main(String [] args){
		Robot robot = new ConcreteRobot();
		Robot obj = new Decorator4Body(new Decorator4Head(new Decorator4Leg(new Decorator4Hand(robot))));
		obj.composition();
	}
}

 

我来组成身体!
我来组成头部!
我来组成腿部!
我来组成手部!
组成战神金刚!

 

装饰模式的简化

  大多数情况下,装饰模式的实现都要比上面给出的示意性例子要简单。

有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。甚至在只有两个ConcreteDecorator类的情况下,都可以这样做。如下图所示:



 

     还是组成《战神金刚》的例子,看我们简化后如何做。

类图如下:



 源码:

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:39:24
 * @description :<p>抽象构件角色(战神金刚)</P>
 **/
public interface Robot {
	/**
	 * 组成某个部位
	 */
	void composition();
}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:49:27
 * @description :<p>具体构件角色</P>
 **/
public class ConcreteRobot implements Robot{

	public void composition() {
        System.out.println("组成战神金刚!");
	}

}

  

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:47:06
 * @description :<p>装饰身体</P>
 **/
public class Decorator4Body implements Robot {

	private Robot robot;

	public Decorator4Body(Robot robot) {
		this.robot = robot;
	}

	public void composition() {
		 System.out.println("我来组成身体!");
		robot.composition();
		 

	}
}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:47:28
 * @description :<p>装饰手部</P>
 **/
public class Decorator4Hand implements Robot {

	private Robot robot;

	public Decorator4Hand(Robot robot) {
		this.robot = robot;
	}

	public void composition() {
		System.out.println("我来组成手部!");
		robot.composition(); 
	}
}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:44:20
 * @description :  <p>  装饰头部   </P>
 **/
public class Decorator4Head implements Robot {

	private Robot robot;

	public Decorator4Head(Robot robot) {
		this.robot = robot;
	}

	public void composition() {
		System.out.println("我来组成头部!");
		robot.composition();
	}
}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:46:27
 * @description :<p>装饰腿部</P>
 **/
public class Decorator4Leg implements Robot {

	private Robot robot;

	public Decorator4Leg(Robot robot) {
		this.robot = robot;
	}

	public void composition() {
		System.out.println("我来组成腿部!");
		robot.composition(); 
	}

}

 

/**
 * @author: Col.Mai
 * @date:2014-9-26 上午12:55:34
 * @description :<p>xxx功能描述</P>
 **/
public class Client {
	public static void main(String [] args){
		Robot robot = new ConcreteRobot();
		Robot obj = new Decorator4Body(new Decorator4Head(new Decorator4Leg(new Decorator4Hand(robot))));
		obj.composition();
	}
}

 输出

我来组成身体!
我来组成头部!
我来组成腿部!
我来组成手部!
组成战神金刚!

 

<think>好的,用户想了解Java中的装饰器模式,特别是示例和应用场景。首先,我需要回忆装饰器模式的定义和核心概念。装饰器模式属于结构型设计模式,允许动态地给对象添加职责,同时不改变其结构。这通常通过将对象包装在装饰器类中来实现。 用户可能已经有一些面向对象编程的基础,但可能对设计模式不太熟悉。我需要用简单明了的语言解释,并给出具体的Java示例。根据提供的引用,尤其是引用[3]提到Java的IO流库大量使用了装饰器模式,比如BufferedReader装饰InputStreamReader,这可以作为经典例子。 接下来,我需要解释装饰器模式的结构,包括组件接口、具体组件、装饰器基类和具体装饰器。然后,通过一个实际的代码示例来展示如何实现。例如,咖啡订单系统,基础咖啡可以添加牛奶、糖等调料,这样的场景适合用装饰器模式扩展功能,而避免类爆炸问题。 应用场景方面,引用[4]和[5]提到动态扩展功能、避免子类膨胀、运行时添加职责等。需要将这些点具体化,比如IO流、GUI组件、日志增强等。同时,要对比装饰器模式和继承的区别,强调其灵活性。 可能用户会混淆装饰器模式和代理模式,或者装饰器模式与继承的优缺点,所以需要明确说明。此外,用户可能想知道在实际开发中何时选择装饰器模式,所以应用场景部分要具体,并结合Java标准库的例子,如IO流中的各种装饰类。 最后,生成的相关问题要覆盖定义、优缺点、与其他模式的区别以及实际应用,这样用户可以有更全面的了解。需要确保引用正确,比如引用[1]、[3]、[4]、[5]都涉及装饰器模式的应用,要正确对应到回答中的各个部分。</think>### Java 装饰器模式详解 #### 一、模式定义 装饰器模式(Decorator Pattern)是一种结构型设计模式,通过将对象包裹在装饰器类中,**动态地为对象添加新功能**,且不改变原有类结构。这种模式实现了功能扩展的层次化,比继承更灵活[^5]。 #### 二、模式结构 1. **组件接口**(Component):定义核心功能接口(如`Coffee`) 2. **具体组件**(ConcreteComponent):基础实现类(如`SimpleCoffee`) 3. **装饰器基类**(Decorator):继承组件接口,持有组件对象 4. **具体装饰器**(ConcreteDecorator):添加具体扩展功能(如`MilkDecorator`) #### 三、代码示例 ```java // 组件接口 interface Coffee { String getDescription(); double getCost(); } // 具体组件 class SimpleCoffee implements Coffee { public String getDescription() { return "基础咖啡"; } public double getCost() { return 10.0; } } // 装饰器基类 abstract class CoffeeDecorator implements Coffee { protected Coffee decoratedCoffee; public CoffeeDecorator(Coffee coffee) { this.decoratedCoffee = coffee; } } // 具体装饰器1:加牛奶 class MilkDecorator extends CoffeeDecorator { public MilkDecorator(Coffee coffee) { super(coffee); } public String getDescription() { return decoratedCoffee.getDescription() + "+牛奶"; } public double getCost() { return decoratedCoffee.getCost() + 2.0; } } // 具体装饰器2:加糖 class SugarDecorator extends CoffeeDecorator { public SugarDecorator(Coffee coffee) { super(coffee); } public String getDescription() { return decoratedCoffee.getDescription() + "+糖"; } public double getCost() { return decoratedCoffee.getCost() + 1.0; } } // 使用示例 public class Main { public static void main(String[] args) { Coffee order = new SimpleCoffee(); order = new MilkDecorator(order); // 加牛奶 order = new SugarDecorator(order); // 加糖 System.out.println(order.getDescription() + " 总价:" + order.getCost()); // 输出:基础咖啡+牛奶+糖 总价:13.0 } } ``` #### 四、应用场景 1. **动态扩展功能**:需要运行时添加或撤销功能时(如Java IO流中的缓冲/加密装饰)[^3] 2. **避免子类爆炸**:当通过继承会导致大量子类时(如不同组合的咖啡调料)[^4] 3. **核心功能与扩展分离**:保持核心类简洁,通过装饰器叠加功能(如日志增强、事务处理)[^5] 4. **标准库典型应用**: - `BufferedInputStream` 装饰 `FileInputStream` - `GZIPOutputStream` 装饰 `FileOutputStream` - `BufferedReader` 装饰 `InputStreamReader`[^1] #### 五、模式优势 | 优点 | 说明 | |-------|-------| | **灵活扩展** | 比继承更灵活,支持运行时动态组合功能 | | **单一职责** | 每个装饰器只关注特定功能扩展 | | **开闭原则** | 不修改原有代码即可扩展功能 | | **透明包装** | 装饰后的对象仍可被当作原类型使用 | $$ \text{装饰器模式复杂度公式:} \quad O(n) = \sum_{k=1}^{m} C_k \quad (m=\text{装饰层数}) $$ §§ 相关问题 §§ 1. 装饰器模式与继承的主要区别是什么? 2. Java IO流中如何体现装饰器模式的层级结构? 3. 装饰器模式会造成哪些性能损耗? 4. 装饰器模式和代理模式的核心区别? 5. 如何防止装饰器被多次嵌套导致系统复杂化? : Java标准库中装饰器模式在IO系统的应用 : Java IO流的装饰器实现 : 装饰器模式的应用场景说明 : 装饰器模式的核心特性分析
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值