设计模式之装饰模式篇(Decorator)

本文深入探讨了装饰模式(Decorator)的概念,展示了如何通过此模式动态地为对象添加职责,而不影响其他对象。通过手机功能增强的例子,说明了装饰模式在软件设计中的灵活性与优势。
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提到的装饰模式的缺点,你能体会吗?他们所说的小对象我认为是指的具体装饰角色。这是为一个对象动态添加功能所带来的副作用。
   源码下载: Decorator.rar

转载于:https://www.cnblogs.com/jillzhang/archive/2006/08/27/487353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值