首页 > 其他 > 详细

装饰模式

时间:2018-08-27 16:53:47      阅读:188      评论:0      收藏:0      [点我收藏+]

本文章,摘抄自:2018黑马程序最新面试题汇总

顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例。

1 public interface ISourceable {
2 
3     public void method();
4 }
1 public class Source implements ISourceable {
2 
3     @Override
4     public void method() {
5         System.out.println("the original method");
6     }
7 
8 }
 1 public class Decorator implements ISourceable {
 2 
 3     private ISourceable source;
 4 
 5     public Decorator(ISourceable source) {
 6         super();
 7         this.source = source;
 8     }
 9 
10     @Override
11     public void method() {
12         System.out.println("before decorator");
13         source.method();
14         System.out.println("after decorator");
15     }
16 
17 }

测试方法:

1 @Test
2     public void test() {
3         ISourceable sourceable = new Source();
4         ISourceable obj = new Decorator(sourceable);
5         obj.method();
6     }

 

装饰模式

原文:https://www.cnblogs.com/ffeiyang/p/9542792.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!