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.我第一次看到这个名称想到的是另外一个词语“装修”,我就说说我对“装修”的理解吧 ...
随机推荐
- HTML中小问题
1.a标签不添加href这个属性时,不会出现鼠标变成手型~
- Html 小插件5 百度搜索代码2
网页添加百度搜索框代码大全 ★ 用法:在下面选择合适的样式,复制代码到网页中相应位置粘贴即可. ★ 样式一(200×30)代码: <iframe id="baiduframe" ...
- POJ 2774 Long Long Message(后缀数组)
[题目链接] http://poj.org/problem?id=2774 [题目大意] 求最长公共子串 [题解] 将两个串中间嵌一个字符相连,求一遍后缀数组 如果排名相邻的两个后缀的开端是分属于两个 ...
- Lowest Common Ancestor of a Binary Search Tree (BST)
Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node ...
- Mysql 库、表、字段 字符集
show character set;show create database aloe;show create table book_category;show full columns from ...
- MicroStrategy笔试
1. coding判定二叉树是否是有序二叉树 2. 一个有序数组A(buffer足够大),和一个有序数组B,设计算法,merge两个数组后有序,不使用任何额外的内存空间 3. 100个点灯问题,初始状 ...
- jbpmAPI-5
Part II. jBPM Core 5.1.概述本章介绍了API需要加载过程和执行它们.更多的细节如何定义过程本身,看看在BPMN 2.0章.与流程引擎交互(例如,开始一个过程),你需要建立一个会话 ...
- poj 3422 Kaka's Matrix Travels 费用流
题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...
- [LeetCode]题解(python):069-Sqrt(x)
题目来源: https://leetcode.com/problems/sqrtx/ 题意分析: 实现一个整型的开根. 题目思路: 利用牛顿迭代法可以求解.首先讲数据类型转成浮点数,然后选取初始值为n ...
- MVC-04 视图(2)
三.View如何从Aciton取得数据 从Action取得数据,在ASP.NET MVC可区分成两种方式,一种是“使用弱类型取得数据”,另一种则是“使用强类型取得数据”,两者的差别在于View页面最上 ...