Decorator(装饰)模式

1、核心意图:
定义
装饰模式以对客户透明的方式 动态的给一个 对象附加上更多的功能。并且,客户端并不会觉得对象在装饰前和装饰后有什么不同。

作用
装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展。

2、装饰模式的构成:
抽象构件角色(Component):给出一个抽象接口,以规范准备接收附加责任的对象。
具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
装饰角色(Decorator):持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口
具体装饰角色(Concrete Decorator):负责给构件对象“贴上”附加的责任。
客户端(Client):通过Component接口控制装饰部件的对象

3、代码实现:
Component.java
Java代码 复制代码 收藏代码
  1. public interface Component
  2. {
  3. public void doSomething();
  4. }
public interface Component
{
	public void doSomething();
}


ConcreteComponent.java
Java代码 复制代码 收藏代码
  1. public class ConcreteComponent implements Component
  2. {
  3. @Override
  4. public void doSomething()
  5. {
  6. System.out.println("功能A");
  7. }
  8. }
public class ConcreteComponent implements Component
{
	@Override
	public void doSomething()
	{
		System.out.println("功能A");
	}
}


Decorator.java
Java代码 复制代码 收藏代码
  1. public class Decorator implements Component
  2. {
  3. private Component component;
  4. public Decorator(Component component)
  5. {
  6. this.component = component;
  7. }
  8. @Override
  9. public void doSomething()
  10. {
  11. component.doSomething();
  12. }
  13. }
public class Decorator implements Component
{
	private Component component;

	public Decorator(Component component)
	{
		this.component = component;
	}
	
	@Override
	public void doSomething()
	{
		component.doSomething();
	}
}


ConcreteDecorator.java
Java代码 复制代码 收藏代码
  1. public class ConcreteDecorator extends Decorator
  2. {
  3. public ConcreteDecorator(Component component)
  4. {
  5. super(component);
  6. }
  7. @Override
  8. public void doSomething()
  9. {
  10. super.doSomething();
  11. this.doAnotherthing();
  12. }
  13. public void doAnotherthing()
  14. {
  15. System.out.println("功能B");
  16. }
  17. }
public class ConcreteDecorator extends Decorator
{
	public ConcreteDecorator(Component component)
	{
		super(component);
	}
	
	@Override
	public void doSomething()
	{
		super.doSomething();
		this.doAnotherthing();
	}
	
	public void doAnotherthing()
	{
		System.out.println("功能B");
	}
}


ConcreteDecorator2.java 附加装饰类
Java代码 复制代码 收藏代码
  1. public class ConcreteDecorator2 extends Decorator
  2. {
  3. public ConcreteDecorator2(Component component)
  4. {
  5. super(component);
  6. }
  7. @Override
  8. public void doSomething()
  9. {
  10. super.doSomething();
  11. this.doAnotherthing();
  12. }
  13. public void doAnotherthing()
  14. {
  15. System.out.println("功能C!");
  16. }
  17. }
public class ConcreteDecorator2 extends Decorator
{
	public ConcreteDecorator2(Component component)
	{
		super(component);
	}

	@Override
	public void doSomething()
	{
		super.doSomething();
		this.doAnotherthing();
	}
	
	public void doAnotherthing()
	{
		System.out.println("功能C!");
	}
}


Client.java
Java代码 复制代码 收藏代码
  1. public class Client
  2. {
  3. public static void main(String[] args)
  4. {
  5. Component component = new ConcreteDecorator2(new ConcreteDecorator(new ConcreteComponent()));
  6. component.doSomething();
  7. }
  8. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值