装饰模式

装饰模式
装饰模式

抽象构件角色:
public interface Component {
	public void doSomething();
}
具体构件角色:
public class ConcreateComponent implements Component {
	@Override
	public void doSomething() {
			System.out.println("功能A");
		}
	}
装饰角色1:
public class ConcreateDecorator1 extends Decorator {
	public ConcreateDecorator1(Component component)
	{
		super(component);
	}
	@Override
	public void doSomething() {
		super.doSomething();
		this.doAnotherThing();
	}
	private void doAnotherThing() {
		System.out.println("功能B");
	}
    }

装饰角色2:
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");
	}
}
装饰角色:
public class Decorator implements Component {
	//实现抽象接口,并持有这个方法的引用
	private Component component;
	
	public Decorator(Component component)
	{
		this.component=component;
	}
	@Override
	public void doSomething() {
		component.doSomething();
	}
}