Decorator Pattern(装饰模式)
装饰模式:动态的给一个对象添加一些额外的职责。当然我们也可以通过继承来实现类似的功能,但是随着子类的增多,各种子类的组合会造成子类的急剧膨胀。
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(装饰模式)的更多相关文章
- 第 13 章 装饰模式【Decorator Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...
- 装饰模式(Decorator pattern)
装饰模式(Decorator pattern): 又名包装模式(Wrapper pattern), 它以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式以对客户透明的方式动态的给 ...
- 深入浅出设计模式——装饰模式(Decorator Pattern)
模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制,使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是静 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- .NET设计模式(10):装饰模式(Decorator Pattern)
.NET设计模式(10):装饰模式(Decorator Pattern) 装饰模式(Decorator Pattern) --.NET设计模式系列之十 年月..在....对于..由于使用装饰模 ...
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
- 第8章 装饰模式(Decorator Pattern)
原文 第8章 装饰模式(Decorator Pattern) 概述: 装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. ...
- 设计模式系列之装饰模式(Decorator Pattern)
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装.这种模式创建了一个装饰类,用来包装原 ...
- C#设计模式之八装饰模式(Decorator Pattern)【结构型】
一.引言 今天我们要讲[结构型]设计模式的第三个模式,该模式是[装饰模式],英文名称:Decorator Pattern.我第一次看到这个名称想到的是另外一个词语“装修”,我就说说我对“装修”的理解吧 ...
随机推荐
- Zombie.js Insanely fast, headless full-stack testing using Node.js
(92) Is there a better crawler than Scrapy? - Quora Is there a better crawler than Scrapy?Edit Insan ...
- 利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择
利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择 利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择
- 自定义View编译失败。Binary XML file line #255: Error inflating
02-28 15:17:16.281: DEBUG/AndroidRuntime(391): Shutting down VM 02-28 15:17:16.281: WARN/dalvikvm(39 ...
- Libcurl安装及编译
1.安装curl wget http://curl.haxx.se/download/curl-7.26.0.tar.gz tar -zxvf curl-7.26.0.tar.gz cd curl- ...
- VS2015操作Oracle数据需要做那些设置?
1>在oracle网上下载:ODP.NET 2> 要根据自己的oracle 数据32bit/64bit,选择下载. 3> 根据提示配置tnsnames.ora文件. # alias ...
- Jquery的一些简单使用记录
//平滑滚动到底部 $(".list").scrollTo('100%', '100%', { easing: 'swing' }); //直接滚动至底部(无效果) $('.lis ...
- 未能加载文件或程序集“**, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。试图加载格式不正确的程序。
未能加载文件或程序集“Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项.试图加载格式不正确的程序. 原来, ...
- Winform获取当前程序名称或路径
以下几种方法获取当前程序名称或路径: // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. // 获 ...
- Windows系统中使用WMI获取远程服务器的信息
使用WMI获取远程服务器的状态 我做的项目里边主要包含两个内容: (1)对发布在服务器上的服务(IIS服务.WCF服务)是否可以正常访问: (2)获取服务器上的部分指标:如CPU.内存.磁盘空间信息等 ...
- 两种mysql文件安装方式——win7 32位OS
官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的. 1. .ZIP格式安装 http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345 ...