您的位置:首页 > 其它

装饰模式

2016-03-23 09:00 281 查看
装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例!

/**
*
* @author: muyichun
* @date  : 2016年3月23日08:59:05
* @function: 装饰模式
*/
public class Main{

public static void main(String[] args) {
Sourceable source = new Source();
Sourceable obj = new Decorator(source);
obj.method();

}
}

interface Sourceable{
public void method();
}
class Source implements Sourceable{

@Override
public void method() {
System.out.println("the original method!");
}
}
class Decorator implements Sourceable{
private Sourceable source;

public Decorator(Sourceable source){
super();
this.source = source;
}
@Override
public void method() {
System.out.println("before decorator!");
source.method();
System.out.println("after decorator!");
}

}
——贴上自己喜欢的代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: