Java设计形式之装饰者模式

Java设计模式之装饰者模式
DECORATOR (Object Structural)
Purpose
    Allows for the dynamic wrapping of objects in order to modify
their existing responsibilities and behaviors.
Use When
    1 Object responsibilities and behaviors should be dynamically
       modifiable.
    2 Concrete implementations should be decoupled from
      responsibilities and behaviors.
    3 Subclassing to achieve modification is impractical or impossible.
    4 Specific functionality should not reside high in the object hierarchy.
    5 A lot of little objects surrounding a concrete implementation is
      acceptable.
Example
    Many businesses set up their mail systems to take advantage of
decorators. When messages are sent from someone in the company
to an external address the mail server decorates the original
message with copyright and confidentiality information. As long
as the message remains internal the information is not attached.
This decoration allows the message itself to remain unchanged
until a runtime decision is made to wrap the message with
additional information.
package javaPattern;

interface Component{
	public void operation();
}
class ConcreteComponent implements Component{
	public void operation(){
		System.out.println("某具体操作方法");
	}
}
public abstract class Decorator implements Component{

	public abstract void operation(); 
	public abstract void addedBehavior();
}
class ConcreteDecorator extends Decorator{

	private String addedState;
	@Override
	public void operation() {
		System.out.println("具体装饰类的某具体方法");
		
	}
	@Override
	public void addedBehavior(){
		System.out.println("新增加的方法");
	}
}