博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式之装饰模式篇(Decorator)
阅读量:7168 次
发布时间:2019-06-29

本文共 3189 字,大约阅读时间需要 10 分钟。

1.引言
如果我们对自己所居住的房间不满意的时候,我们往往是通过装修的方式来使我们的房间变的漂亮起来。而不是重新建一个房间。而且经过装修的屋子仍让是原来的屋子,本质不会变,但它的确变漂亮了,满足了我们的美的需求。从投入来看,装修比重新建设显然要便宜的多! 在软件设计里面,此类情形的设计既可以采用装饰模式(Decorator)来完成。
2.概念与模式

装饰模式(Decorator)也叫包装器模式(Wrapper)。GOF在《设计模式》一书中给出的定义为:动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。

装饰模式的实现类关系图为:

1)        抽象构件角色(Component):定义一个抽象接口,以规范准备接收附加责任的对象。

 

 

2)        具体构件角色(Concrete Component):这是被装饰者,定义一个将要被装饰增加功能的类。

 

 

3)        装饰角色(Decorator):持有一个构件对象的实例,并定义了抽象构件定义的接口。

 

 

4)        具体装饰角色(Concrete Decorator):负责给构件添加增加的功能。

3.应用实例
一部手机,在其接受到来电的时候,会发出声音来提醒主人。而现在我们需要为该手机添加一项功能,在接收来电的时候,产生震动,而令一部更加高级,不仅发声,而且振动,而且有灯光闪烁。
用装饰模式来解决这个问题的类关系图为:
相关文件为:

ContractedBlock.gif
ExpandedBlockStart.gif
Component
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Decorator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public interface IPhone
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
10InBlock.gif        /// 接收电话
11ExpandedSubBlockEnd.gif        /// </summary>
12InBlock.gif        void ReceiveCall();
13ExpandedSubBlockEnd.gif    }
14ExpandedBlockEnd.gif}
15None.gif
ContractedBlock.gif
ExpandedBlockStart.gif
Concrete Component
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Decorator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class BasePhone:IPhone
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public void ReceiveCall()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            Console.WriteLine("Ringdot.gif..");
12ExpandedSubBlockEnd.gif        }
13ExpandedSubBlockEnd.gif    }
14ExpandedBlockEnd.gif}
15None.gif
ContractedBlock.gif
ExpandedBlockStart.gif
Decorator
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Decorator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public abstract class PhoneDecorator:IPhone
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        IPhone phone = null;
10InBlock.gif        public PhoneDecorator(IPhone phone)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            if (phone != null)
13ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
14InBlock.gif                this.phone = phone;
15ExpandedSubBlockEnd.gif            }
16InBlock.gif            else
17ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
18InBlock.gif                this.phone = new BasePhone();
19ExpandedSubBlockEnd.gif            }
20ExpandedSubBlockEnd.gif        }
21InBlock.gif        public virtual void ReceiveCall()
22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
23InBlock.gif            phone.ReceiveCall();
24ExpandedSubBlockEnd.gif        }
25ExpandedSubBlockEnd.gif    }
26ExpandedBlockEnd.gif}
27None.gif
ContractedBlock.gif
ExpandedBlockStart.gif
Concrete Decorator
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Decorator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class JarPhone:PhoneDecorator
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public JarPhone(BasePhone phone)
10InBlock.gif            : base(phone)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12ExpandedSubBlockEnd.gif        }
13InBlock.gif        public override void ReceiveCall()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            base.ReceiveCall();
16InBlock.gif            Console.WriteLine("Jardot.gif");
17ExpandedSubBlockEnd.gif        }
18ExpandedSubBlockEnd.gif    }
19ExpandedBlockEnd.gif}
20None.gif
21None.gif
22None.gif
23None.gifusing System;
24None.gifusing System.Collections.Generic;
25None.gifusing System.Text;
26None.gif
27None.gifnamespace Decorator
28ExpandedBlockStart.gifContractedBlock.gifdot.gif{
29InBlock.gif    public class ComplexPhone:PhoneDecorator
30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
31InBlock.gif        public ComplexPhone(JarPhone phone)
32InBlock.gif            : base(phone)
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34ExpandedSubBlockEnd.gif        }
35InBlock.gif        public override void ReceiveCall()
36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
37InBlock.gif            base.ReceiveCall();
38InBlock.gif            Console.WriteLine("Winkdot.gif");
39ExpandedSubBlockEnd.gif        }
40ExpandedSubBlockEnd.gif    }
41ExpandedBlockEnd.gif}
42None.gif
ContractedBlock.gif
ExpandedBlockStart.gif
客户端调用
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Decorator
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    class Program
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        static void Main(string[] args)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            BasePhone phone = new BasePhone();
12InBlock.gif            phone.ReceiveCall();
13InBlock.gif            Console.WriteLine("---------------------");
14InBlock.gif            JarPhone p2 = new JarPhone(phone);
15InBlock.gif            p2.ReceiveCall();
16InBlock.gif            Console.WriteLine("---------------------");
17InBlock.gif            ComplexPhone p3 = new ComplexPhone(p2);           
18InBlock.gif            p3.ReceiveCall();
19InBlock.gif            Console.Read();
20ExpandedSubBlockEnd.gif        }
21ExpandedSubBlockEnd.gif    }
22ExpandedBlockEnd.gif}
23None.gif
运行结果:

四、应用环境 

       GOF书中给出了以下使用情况: 
1)        在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。 

2)        处理那些可以撤消的职责。  

3)        当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。  

来分析下手机的使用是属于哪种情况。首先实现了比静态继承更加灵活的方式,动态的增加功能。试想为手机的所有实现类通过继承来增加一个功能,意味着要添加不少的功能类似的子类,这明显是不太合适的。 

而且,这就避免了高层的类具有太多的特征。 

五、透明和半透明  

       对于面向接口编程,应该尽量使客户程序不知道具体的类型,而应该对一个接口操作。这样就要求装饰角色和具体装饰角色要满足Liskov替换原则。

六、其它 采用Decorator模式进行系统设计往往会产生许多看上去类似的小对象,这些对象仅仅在他们相互连接的方式上有所不同,而不是它们的类或是它们的属性值有所不同。尽管对于那些了解这些系统的人来说,很容易对它们进行定制,但是很难学习这些系统,排错也很困难。这是GOF提到的装饰模式的缺点,你能体会吗?他们所说的小对象我认为是指的具体装饰角色。这是为一个对象动态添加功能所带来的副作用。
   源码下载: 

转载地址:http://dhmwm.baihongyu.com/

你可能感兴趣的文章
漫画 :Apache Nginx80 端口争夺战
查看>>
Chrome 67 for Android发布
查看>>
Windows Server 2016-查询并导出过去某时间段创建AD用户
查看>>
Python制作数据分析工具
查看>>
MySQL5.7 error log时间显示问题
查看>>
<init-param>和<context-param>两个标签的区别
查看>>
有关list集合转换为map集合
查看>>
A记录 MX记录 CNAME记录 TXT记录 SRV记录
查看>>
“最美叔叔”谢尚威向“最美女教师”张丽莉致意
查看>>
Activiti(二)在官方实例上运行一个流程
查看>>
Redis集群创建
查看>>
如何在Amazon AWS上设置一台Linux服务器
查看>>
Python读取修改ini配置文件[ConfigParser]
查看>>
Linux虚拟化技术—CentOS7.4下KVM虚拟化一 安装配置及基本操作
查看>>
我的高质量软件发布心得
查看>>
DecimalFormat 类基本使用
查看>>
es6 Set和map数据结构
查看>>
数字键盘三
查看>>
12个值得关注的顶级JS库
查看>>
线程安全的CopyOnWriteArrayList介绍
查看>>