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("新增加的方法");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值