Java设计模式之装饰者模式

DECORATOR (Object Structural)
[color=red]Purpose[/color]
Allows for the dynamic wrapping of objects in order to modify
their existing responsibilities and behaviors.
[color=red]Use When[/color]
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.
[color=red]Example[/color]
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("新增加的方法");
}
}
发布了40 篇原创文章 · 获赞 0 · 访问量 836
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章