Head First Design patterns笔记-Decorator Patterns (从”用不同技能武装自己”看装饰模式)...

本文通过一个生动的例子介绍了装饰模式的概念及其实现方式。装饰模式允许在不改变对象结构的前提下动态地给对象添加新的职责。

定义Decorator patterns attach additional responsibilities abilities(我自己加的) to an object dynamically, decorators provide a flexible alternative to subclassing for extending functionality.

背景介绍:学习是人类与生俱来的一种能力。当然像哭泣、微笑、吃饭、睡觉等能力人一来到这个世界就会。当你学习了一种技能你就会多一种行为能力。用不同的技能把自己武装起来,你就会成为一个多才多艺的人,^_^。就我自己而言,吃喝拉撒睡这些就不用再提了,小学四年纪的时候学会了游泳(在同伴当中应该算是学的很晚的),大约六年级的时候开始摸篮球,算起球龄来也差不多15年了,不能算高手中的低手至少也应该算是低手中的高手了(大学的时候也是靠球技迷倒一片的,^_^。稍微自恋一下)。大学修的计算机科学与技术,毕业来北京混饭吃做了一年多的开发。后来走投无路做了一年测试,最后还是脱离了枯燥的测试工作重新做开发。算起来这几项能力我也算都具备了,现在除了做好自己的开发工作,我也会协助同事做一些测试的工作,工作之余我会参加一些游泳,篮球的比赛活动,业余时间教我老婆这几项技能,^_^。这篇文章的例子正是模拟人与技能之间的这种关系,我的一个朋友一直做开发,现在正在学习游泳,基本不会打球,这个例子把他拉进来吧!^_^。

VS自动生成的类图:


VS自动生成的类图看起来还真不大习惯,下次可能用PD了。^_^。

实例代码:

ContractedBlock.gif ExpandedBlockStart.gif 查看代码
  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.Text;
  4None.gif
  5None.gifnamespace DecoratorDemo
  6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// Both subject and decorator all implment this interface.
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public interface IHuman
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        void Learn();
 13ExpandedSubBlockEnd.gif    }

 14InBlock.gif
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 16InBlock.gif    /// Decorator base class, all concrete decorator classes implement this interface.
 17InBlock.gif    /// We create this class in order to make the hierarchy more clear.
 18ExpandedSubBlockEnd.gif    /// </summary>

 19InBlock.gif    public interface SkillBase : IHuman
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 21InBlock.gif
 22ExpandedSubBlockEnd.gif    }

 23InBlock.gif
 24ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 25InBlock.gif    /// Concrete subject to decorate.
 26ExpandedSubBlockEnd.gif    /// </summary>

 27InBlock.gif    public sealed class ConcreteHuman : IHuman
 28ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 29InBlock.gif        public void Learn()
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            Console.WriteLine("Crying, laughing, eating,etc. are our natural skills!");
 32ExpandedSubBlockEnd.gif        }

 33ExpandedSubBlockEnd.gif    }

 34InBlock.gif
 35InBlock.gif
 36InBlock.gif    public sealed class TestingSkill : SkillBase
 37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 38InBlock.gif        IHuman _human;
 39InBlock.gif        public TestingSkill(IHuman human)
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 41InBlock.gif            _human = human;
 42ExpandedSubBlockEnd.gif        }

 43InBlock.gif
 44InBlock.gif        public void Learn()
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            _human.Learn();
 47InBlock.gif            Console.WriteLine("I have been a tester for more than one year,i know what kinds of skills one tester should have.");
 48ExpandedSubBlockEnd.gif        }

 49InBlock.gif
 50ExpandedSubBlockEnd.gif    }

 51InBlock.gif    public sealed class DevelopingSkill : SkillBase
 52ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 53InBlock.gif        IHuman _human;
 54InBlock.gif        public DevelopingSkill(IHuman human)
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 56InBlock.gif            _human = human;
 57ExpandedSubBlockEnd.gif        }

 58InBlock.gif
 59InBlock.gif        public void Learn()
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 61InBlock.gif            _human.Learn();
 62InBlock.gif            Console.WriteLine("After my graduation, I became a prefessional developer.");
 63ExpandedSubBlockEnd.gif        }

 64ExpandedSubBlockEnd.gif    }

 65InBlock.gif
 66InBlock.gif    public sealed class SwimmingSkill : SkillBase
 67ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 68InBlock.gif        IHuman _human;
 69InBlock.gif        public SwimmingSkill(IHuman human)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            _human = human;
 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif
 74InBlock.gif        public void Learn()
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            _human.Learn();
 77InBlock.gif            Console.WriteLine("I mastered swimming skill when i was in my primary school.");
 78ExpandedSubBlockEnd.gif        }

 79ExpandedSubBlockEnd.gif    }

 80InBlock.gif
 81InBlock.gif    public sealed class BasketballSkill : SkillBase
 82ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 83InBlock.gif        IHuman _human;
 84InBlock.gif        public BasketballSkill(IHuman human)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 86InBlock.gif            _human = human;
 87ExpandedSubBlockEnd.gif        }

 88InBlock.gif
 89InBlock.gif        public void Learn()
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 91InBlock.gif            _human.Learn();
 92InBlock.gif            Console.WriteLine("More than 15 years basketball playing experience.");
 93ExpandedSubBlockEnd.gif        }

 94ExpandedSubBlockEnd.gif    }

 95InBlock.gif
 96ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 97InBlock.gif    /// Testing driver.
 98ExpandedSubBlockEnd.gif    /// </summary>

 99InBlock.gif    class Program
100ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
101InBlock.gif        static void Main(string[] args)
102ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
103InBlock.gif            Console.WriteLine("Do you want to have my information? Take a look at the lines below:");
104InBlock.gif            Console.WriteLine("***********************************************************");
105InBlock.gif            IHuman myself = new ConcreteHuman();
106InBlock.gif            myself = new DevelopingSkill(myself);
107InBlock.gif            myself = new TestingSkill(myself);
108InBlock.gif            myself = new SwimmingSkill(myself);
109InBlock.gif            myself = new BasketballSkill(myself);
110InBlock.gif            myself.Learn();
111InBlock.gif
112InBlock.gif            Console.WriteLine("***********************************************************");
113InBlock.gif            Console.WriteLine(Environment.NewLine);
114InBlock.gif            Console.WriteLine("The following is the information of my friend.");
115InBlock.gif            Console.WriteLine("***********************************************************");
116InBlock.gif           
117InBlock.gif            IHuman myFriend = new ConcreteHuman();
118InBlock.gif            myFriend = new DevelopingSkill(myFriend);
119InBlock.gif            myFriend = new SwimmingSkill(myFriend);
120InBlock.gif            myFriend.Learn();
121InBlock.gif            Console.WriteLine("***********************************************************");
122InBlock.gif
123InBlock.gif            Console.ReadKey();
124ExpandedSubBlockEnd.gif        }

125ExpandedSubBlockEnd.gif    }

126ExpandedBlockEnd.gif}

127None.gif

运行结果图


本例引出的面向对象原则
类可以用来扩展,但是不应该被修改。(classes should be open for extension but closed for modification.)
此外本例也体现了面向接口编程的思想,在decorator的具体实现中应用了一个IHuman对象,而不是应用ConcreteHuman对象正说明了这一点。如果你想扩展subject的功能,那你就实现自己的decorator就可以,无需修改subject内的任何代码。

代码下载:
DecoratorDemo.zip

后续:
装饰模式最最关键的一点是主体(subject)和修饰者(decorator)应该有共同的接口,对于本例而言就是ConcreteHuman 和SkillBase两个类都继承自IHuman这个接口。



 

转载于:https://www.cnblogs.com/netboy/archive/2007/05/10/742255.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值