1、核心意图:
定义
装饰模式以对客户透明的方式 动态的给一个 对象附加上更多的功能。并且,客户端并不会觉得对象在装饰前和装饰后有什么不同。
作用
装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展。
2、装饰模式的构成:
– 抽象构件角色(Component):给出一个抽象接口,以规范准备接收附加责任的对象。
– 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
– 装饰角色(Decorator):持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口
– 具体装饰角色(Concrete Decorator):负责给构件对象“贴上”附加的责任。
– 客户端(Client):通过Component接口控制装饰部件的对象
3、代码实现:
Component.java
ConcreteComponent.java
Decorator.java
ConcreteDecorator.java
ConcreteDecorator2.java 附加装饰类
Client.java
定义
装饰模式以对客户透明的方式 动态的给一个 对象附加上更多的功能。并且,客户端并不会觉得对象在装饰前和装饰后有什么不同。
作用
装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展。
2、装饰模式的构成:
– 抽象构件角色(Component):给出一个抽象接口,以规范准备接收附加责任的对象。
– 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
– 装饰角色(Decorator):持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口
– 具体装饰角色(Concrete Decorator):负责给构件对象“贴上”附加的责任。
– 客户端(Client):通过Component接口控制装饰部件的对象
3、代码实现:
Component.java
- public interface Component
- {
- public void doSomething();
- }
public interface Component
{
public void doSomething();
}
ConcreteComponent.java
- public class ConcreteComponent implements Component
- {
- @Override
- public void doSomething()
- {
- System.out.println("功能A");
- }
- }
public class ConcreteComponent implements Component
{
@Override
public void doSomething()
{
System.out.println("功能A");
}
}
Decorator.java
- public class Decorator implements Component
- {
- private Component component;
- public Decorator(Component component)
- {
- this.component = component;
- }
- @Override
- public void doSomething()
- {
- component.doSomething();
- }
- }
public class Decorator implements Component
{
private Component component;
public Decorator(Component component)
{
this.component = component;
}
@Override
public void doSomething()
{
component.doSomething();
}
}
ConcreteDecorator.java
- 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");
- }
- }
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 附加装饰类
- 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!");
- }
- }
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
- public class Client
- {
- public static void main(String[] args)
- {
- Component component = new ConcreteDecorator2(new ConcreteDecorator(new ConcreteComponent()));
- component.doSomething();
- }
- }