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.我第一次看到这个名称想到的是另外一个词语“装修”,我就说说我对“装修”的理解吧 ...
随机推荐
- 网络技术教程笔记(20)ISDN
广域网与接入网技术 广域网与接入网技术 常见接入技术--ISDN 综合业务数字网(Integrated Services Digital Network,ISDN)由电话综合数字网IDN演化而成,能够 ...
- 树莓派高级GPIO库,wiringpi2 for python使用笔记(一)安装
网上的教程,一般Python用RPi.GPIO来控制树莓派的GPIO,而C/C++一般用wringpi库来操作GPIO,RPi.GPIO过于简单,很多高级功能不支持,比如i2c/SPI库等,也缺乏高精 ...
- nodejs中EventEmitter
在模块events中,定义了一个EventEmitter类,可以使用var EventEmitter = require('events');访问它.基本上所有发送事件的对象都是继承自EventEmi ...
- 在PADS LAYOUT中如何隐藏不需要的鼠线?
如下图示,将net GPR_0的鼠线隐藏. 鼠标右键,选择网络----选择你要隐藏的网络------右键选择view nets----点击对话框右边View List里你所选的网络-----在右下角t ...
- ECC(Error Checking and Correction)校验和纠错
ECC的全称是 Error Checking and Correction or Error correction Coding,是一种用于差错检测和修正的算法.上一节的BBM中我们提到过,NAND闪 ...
- Noip2013调试技巧
关于调试技巧,个人觉得还是很重要的,于是把自己之前写过的总结拿出来,修修补补再复习一下. F7 单步跟踪法 这是大家都最常用的调试方法,可以一步一步去跟踪程序的运行方向,以及各种变量的变化情况,当发现 ...
- 基于xml文件实现系统属性配置管理
文章标题:基于xml文件实现系统属性配置管理 . 文章地址: http://blog.csdn.net/5iasp/article/details/11774501 作者: javaboy2012 E ...
- [置顶] ※数据结构※→☆线性表结构(list)☆============单向链表结构(list single)(二)
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始:链表是使用指针进行构造的列表:又称为结点列表,因为链表是由一个个结点组装起来的:其中每个结点都有指 ...
- hdu4491 Windmill Animation(计算几何)
Windmill Animation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Linux下smi/mdio总线驱动
Linux下smi/mdio总线驱动 韩大卫@吉林师范大学 MII(媒体独立接口), 是IEEE802.3定义的以太网行业标准接口, smi是mii中的标准管理接口, 有两跟管脚, mdio 和mdc ...