装饰模式Decorator

本文详细介绍了装饰模式的概念及其在软件设计中的应用。通过示例代码展示了如何动态地为对象添加职责,以及装饰模式与继承之间的区别。并通过具体实例演示了装饰模式的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 定义
动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式比生成子类更为灵活。

 

2. 结构图



Component:组件对象的接口,可以给这些对象动态地添加职责。
ConcreteComponent:具体的组件对象,实现组件对象接口,通常就是被装饰器装饰的原始对象,也就是可以给这个对象添加职责。
Decorator:所有装饰器的抽象父类,需要定义一个与组件接口一致的接口,并持有一个Component对象,其实就是持有一个被装饰的对象。注意这个被装饰的对象不一定是最原始的那个对象,也可能是被其他装饰器装饰过后的对象,反正都是实现的同一个接口,也就是同一类型。
ConcreteDecorator:实际的装饰器对象,实现具体要向被装饰器对象添加的功能。

 

3. 本质
装饰模式的本质:动态组合。

 

4. Code Demo

Component.java

package org.fool.decorator;

public interface Component {
	public void doSomething();
}

 

ConcreteComponent.java

package org.fool.decorator;

public class ConcreteComponent implements Component {
	@Override
	public void doSomething() {
		System.out.println("功能A");
	}

}

 

Decorator.java

package org.fool.decorator;

public class Decorator implements Component {
	private Component component;

	public Decorator(Component component) {
		this.component = component;
	}

	@Override
	public void doSomething() {
		component.doSomething();
	}
}

 

ConcreteDecorator1.java

package org.fool.decorator;

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

	@Override
	public void doSomething() {
		super.doSomething();

		this.doAnotherThing();
	}

	private void doAnotherThing() {
		System.out.println("功能B");
	}

}

 

ConcreteDecorator2.java

package org.fool.decorator;

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

	@Override
	public void doSomething() {
		super.doSomething();

		this.doAnotherThing();
	}

	private void doAnotherThing() {
		System.out.println("功能C");
	}
}

 

Client.java

package org.fool.decorator;

public class Client {
	public static void main(String[] args) {
		/*
		 * //节点流 Component component = new ConcreteComponent();
		 * 
		 * //过滤流 Component component2 = new ConcreteDecorator1(component);
		 * 
		 * component2.doSomething();
		 * 
		 * System.out.println("------------");
		 * 
		 * //过滤流 Component component3 = new ConcreteDecorator2(component2);
		 * 
		 * component3.doSomething();
		 */

		Component component = new ConcreteDecorator1(new ConcreteDecorator2(new ConcreteComponent()));

		component.doSomething();
	}
}

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值