装饰模式:动态的给一个对象添加一些额外的职责。当然我们也可以通过继承来实现类似的功能,但是随着子类的增多,各种子类的组合会造成子类的急剧膨胀。

Requirement:

  假设客户有一个要求,需要打一个report,并且report 的header 和footer 各有3种,然后要求打出所有可能组合的report。

Analysis:

  当然纯粹的通过子类继承也可以实现,但是现在如果header 和footer各有10种呢,那么你就要有100个扩展类。但是通过使用装饰模式,你只要10 header 类+10个footer类(即20个类)就可以轻松搞定。下面我们以header 和footer 各有两个具体实现一下。

Sample Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TestDecoratorPatternSample
{
public abstract class Report
{
public abstract void ReportContent();
} public class Content : Report
{
public override void ReportContent()
{
Console.WriteLine("This is report content.");
}
} public abstract class ReportDecorator : Report
{
private Report report;
public ReportDecorator(Report myReport)
{
report = myReport;
}
public override void ReportContent()
{
report.ReportContent();
}
}
#region Header1
public class Header1 : ReportDecorator
{
public Header1(Report myReport)
: base(myReport)
{
} public override void ReportContent()
{
ReportHeader();
base.ReportContent();
} public void ReportHeader()
{
Console.WriteLine("This is report header1.");
}
} #endregion
#region Header2 public class Header2 : ReportDecorator
{
public Header2(Report myReport)
: base(myReport)
{
} public override void ReportContent()
{
ReportHeader();
base.ReportContent();
} public void ReportHeader()
{
Console.WriteLine("This is report header2.");
}
} #endregion
#region Header3 public class Header3 : ReportDecorator
{
public Header3(Report myReport)
: base(myReport)
{
} public override void ReportContent()
{
ReportHeader();
base.ReportContent();
} public void ReportHeader()
{
Console.WriteLine("This is report header3.");
}
} #endregion
#region Footer1
public class Footer1 : ReportDecorator
{
public Footer1(Report myReport)
: base(myReport)
{
} public override void ReportContent()
{
base.ReportContent();
ReportFooter();
} public void ReportFooter()
{
Console.WriteLine("This is report Footer1.");
}
}
#endregion
#region Footer2 public class Footer2 : ReportDecorator
{
public Footer2(Report myReport)
: base(myReport)
{
} public override void ReportContent()
{
base.ReportContent();
ReportFooter();
} public void ReportFooter()
{
Console.WriteLine("This is report Footer2.");
}
} #endregion
#region Footer3 public class Footer3 : ReportDecorator
{
public Footer3(Report myReport)
: base(myReport)
{
} public override void ReportContent()
{
base.ReportContent();
ReportFooter();
} public void ReportFooter()
{
Console.WriteLine("This is report Footer3.");
}
} #endregion
}

  下面是客户端的调用代码,你需要怎么组合在你调用的时候就怎么组合。

            Content clientReport = new Content();
Report header2 = new Header2(clientReport);
Report footer1 = new Footer1(header2);
footer1.ReportContent();

  

欢迎各位网友拍砖,本人也在不断学习中。。。

Decorator Pattern(装饰模式)的更多相关文章

  1. 第 13 章 装饰模式【Decorator Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...

  2. 装饰模式(Decorator pattern)

    装饰模式(Decorator pattern): 又名包装模式(Wrapper pattern), 它以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式以对客户透明的方式动态的给 ...

  3. 深入浅出设计模式——装饰模式(Decorator Pattern)

    模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制,使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是静 ...

  4. 二十四种设计模式:装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...

  5. .NET设计模式(10):装饰模式(Decorator Pattern)

      .NET设计模式(10):装饰模式(Decorator Pattern)   装饰模式(Decorator Pattern) --.NET设计模式系列之十 年月..在....对于..由于使用装饰模 ...

  6. 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)

    原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...

  7. 第8章 装饰模式(Decorator Pattern)

    原文 第8章 装饰模式(Decorator Pattern) 概述: 装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. ...

  8. 设计模式系列之装饰模式(Decorator Pattern)

    装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装.这种模式创建了一个装饰类,用来包装原 ...

  9. C#设计模式之八装饰模式(Decorator Pattern)【结构型】

    一.引言 今天我们要讲[结构型]设计模式的第三个模式,该模式是[装饰模式],英文名称:Decorator Pattern.我第一次看到这个名称想到的是另外一个词语“装修”,我就说说我对“装修”的理解吧 ...

随机推荐

  1. Android 程序申请权限小知识点

    在Google Play 应用商店,显示至少支持设备的数量时候会用到权限数量.其他地方用处不大. Android系统提供为程序提供了权限申请,即在manifest中使用uses-permission来 ...

  2. JavaScript版排序算法

    JavaScript版排序算法:冒泡排序.快速排序.插入排序.希尔排序(小数据时,希尔排序会比快排快哦) //排序算法 window.onload = function(){ var array = ...

  3. 一些特殊css

    属性 描述            outline  (轮廓)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用. outline:#00FF00 dotted thick; 可以按顺序 ...

  4. JAVA并发,线程工厂及自定义线程池

    package com.xt.thinks21_2; import java.util.concurrent.ExecutorService; import java.util.concurrent. ...

  5. ios post空文件流导致400错误

  6. UVALive 6947 Improvements(DP+树状数组)

    [题目链接] https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sho ...

  7. 关于JQuery中$.data绑定数据原理或逻辑

    问题: JQuery中,对于.data([key],[value])函数,当使用其进行数据绑定时,假设要绑定的数据是“引用数据类型”,也就是对象:那么.data函数绑定的是该对象的副本还是该对象的一个 ...

  8. makefile简单helloworld

    最近要在unix系统上开发c++应用程序,但默认情况下unix编译c++程序需要使用makefile.其实makefile语法还是比较简单,看上去有点像ant.废话不说了,直接上helloworld. ...

  9. js获取智能机浏览器版本信息

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  10. DropDownList为啥总是获取第一项的值???

    小菜: DropDownList控件绑定的数据,在获取数据时总是获取到第一项,很是郁闷,怎么回事,于是就各种想,都没有找到问题的原因. 请看下面的代码 前台代码: <asp:DropDownLi ...