最近看了blog牛人张逸写的设计之道,在文中设计模式的例子中运用了Decorator模式.怎么看怎么不理解(忘记说了,本人智商Te低),于是一个晚上没有睡着觉,在网上搜索Decorator的讲解.看了Terrylee的设计模式.看了lovecherry的无废话设计模式,也拜读了wxj1020<head First>设计模式系列(在此特别感谢各位,特献上最崇高的敬意).经过720个分钟的努力终于对decorator模式有一点点理解,为了加深对她的记忆.特此记录下来(也是怕忘记了.智商低的人忘记的特快).
    Decorator模式
    1:意图:动态的为一些对象添加一些职责(何为动态?晕死,我的理解就是,不通过继承而增加对象功能!).
    2:结构图(不是俺画的)
    NET设计模式之一:装饰模式(Decorator Pattern)
    3
实例(别讲那些高深的理论的东西,实践出真知,抓紧看一下吧)
          项目背景:一个图书馆有两种刊物可以外借书籍(Books)和音像(Videos),开始都免费.看下类图吧
                               NET设计模式之一:装饰模式(Decorator Pattern)
       都是很简单的关系.继承.代码也很简单
       LibraryItem.cs
 1NET设计模式之一:装饰模式(Decorator Pattern)using System;
 2NET设计模式之一:装饰模式(Decorator Pattern)using System.Collections.Generic;
 3NET设计模式之一:装饰模式(Decorator Pattern)using System.Text;
 4NET设计模式之一:装饰模式(Decorator Pattern)
 5NET设计模式之一:装饰模式(Decorator Pattern)namespace Decorator
 6
        Books.cs
 1NET设计模式之一:装饰模式(Decorator Pattern)using System;
 2NET设计模式之一:装饰模式(Decorator Pattern)using System.Collections.Generic;
 3NET设计模式之一:装饰模式(Decorator Pattern)using System.Text;
 4NET设计模式之一:装饰模式(Decorator Pattern)
 5NET设计模式之一:装饰模式(Decorator Pattern)namespace Decorator
 6
        Vedios.cs
 1NET设计模式之一:装饰模式(Decorator Pattern)using System;
 2NET设计模式之一:装饰模式(Decorator Pattern)using System.Collections.Generic;
 3NET设计模式之一:装饰模式(Decorator Pattern)using System.Text;
 4NET设计模式之一:装饰模式(Decorator Pattern)
 5NET设计模式之一:装饰模式(Decorator Pattern)namespace Decorator
 6
    实现以上的功能.我们的设计还可以胜任.突然某天,图书馆老板变成"黄世仁"了.晕死.看书要书费.不过为了能顺利的进行不收费到收费的过度,他制定了详细的计划女生借书要钱,男生借Videos要钱.说不定改天对男生女生都收钱.
.完了.本来想通过自家继承的子类来实现,发现这样要增加3个子类,如果要是有其他的变化,子类又要多了.咋办?呵呵.聪明的你想到了.Decorator帮你实现(现代人就是轻松,老一辈啥都给你准备好了).看一下Decorator类图吧
NET设计模式之一:装饰模式(Decorator Pattern)

看看代码吧
 1NET设计模式之一:装饰模式(Decorator Pattern)using System;
 2NET设计模式之一:装饰模式(Decorator Pattern)using System.Collections.Generic;
 3NET设计模式之一:装饰模式(Decorator Pattern)using System.Text;
 4NET设计模式之一:装饰模式(Decorator Pattern)
 5NET设计模式之一:装饰模式(Decorator Pattern)namespace Decorator
 6

相关文章:

  • 2021-11-11
  • 2021-11-28
  • 2021-07-19
  • 2021-08-14
  • 2021-07-18
  • 2021-10-03
猜你喜欢
  • 2021-06-02
  • 2021-05-19
  • 2021-11-17
相关资源
相似解决方案