C#设计模式系列:装饰模式(Decorator)
1. 装饰模式简介
装饰模式动态地给一个对象添加额外的职责。例如一幅画有没有画框都可以挂在墙上,画就是被装饰者。但是通常都是有画框的。在挂在墙上之前,画可以被蒙上玻璃,装到框子里,所以在画上加一层画框,并把它们组合成一个整体——有框的画。这样随着不断有新的装饰的加入,就给商品不断地打上包装,变成一个功能更让人满意的商品。这种不断打包装的过程就是装饰。
1.1 定义
装饰模式提供了一种给类增加功能的方法。它通过动态地组合对象,可以给原有的类添加新的代码,而无须修改现有代码。因此引入bug或产生意外副作用的机会将大幅度减少。
1.2 使用频率
中等
2. 装饰模式结构图
2.1 结构图
2.2 参与者
装饰模式参与者:
◊ Component:定义一个对象接口,可以给这些对象动态地添加职责
◊ ConcreteComponent:定义一个对象,可以给这个对象添加一些职责
◊ Decorator:维持一个指向Component的指针,并定义一个与Component接口一致的接口
◊ ConcreteDecorator:负责向ConcreteComponent添加功能
在装饰模式中,Decorator定义了一个装饰接口类。因为Decorator与ConcreteComponent继承同一个接口,所以继承Decorator的类ConcreteDecorator可以使用ConcreteComponent的方法,再在ConcreteDecorator里面加入一些新的方法,也就是装饰,就成为了一个包装好的装饰类。
3、装饰模式结构实现
Component.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.DecoratorPattern.Structural
{
public abstract class Component
{
public abstract void Operation();
}
}
ConcreteComponent.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.DecoratorPattern.Structural
{
public class ConcreteComponent : Component
{
public override void Operation()
{
Console.WriteLine("ConcreteComponent.Operation()");
}
}
}
Decorator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.DecoratorPattern.Structural
{
public abstract class Decorator : Component
{
protected Component component; public void SetComponent(Component component)
{
this.component = component;
} public override void Operation()
{
if (component != null)
{
component.Operation();
}
}
}
}
ConcreteDecoratorA.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.DecoratorPattern.Structural
{
public class ConcreteDecoratorA : Decorator
{
public override void Operation()
{
base.Operation();
Console.WriteLine("ConcreteDecoratorA.Operation()");
}
}
}
ConcreteDecoratorB.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.DecoratorPattern.Structural
{
public class ConcreteDecoratorB : Decorator
{
public override void Operation()
{
base.Operation();
AddedBehavior();
Console.WriteLine("ConcreteDecoratorB.Operation()");
} void AddedBehavior()
{
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using DesignPatterns.DecoratorPattern.Structural; namespace DesignPatterns.DecoratorPattern
{
class Program
{
static void Main(string[] args)
{
// Create ConcreteComponent and two Decorators
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Link decorators
d1.SetComponent(c);
d2.SetComponent(d1); d2.Operation();
}
}
}
运行输出:
ConcreteComponent.Operation()
ConcreteDecoratorA.Operation()
ConcreteDecoratorB.Operation()
请按任意键继续. . .
4、装饰模式应用分析
装饰模式适用情形:
◊ 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责
◊ 处理那些可以撤销的职责
装饰模式的特点:
◊ 比静态类更灵活。使用装饰模式可以很容易地向对象添加职责的方式。可以用添加和分离的方法,对装饰在运行时添加和删除职责。相比之下,继承机制要求为每个添加的职责创建一个新的子类。这会产生很多新的类,并会增加系统的复杂度。
◊ 使用装饰模式可以很容易地重复添加一个特性,而两次继承特性类则极容易出错。
◊ 为了避免处理顶层的类有太多的特征。装饰模式下,你可以定义一个简单的类,并用装饰类给它逐渐地添加功能。这样可以从简单的部件组合出复杂的功能,具有低依赖性和地复杂性。
◊ 有许多小对象。采用装饰模式进行系统设计往往会产生许多看上去类似的小对象,尽管对于了解这些系统的人来说,很容易进行定制,但是很难学习这些系统,排错很恶化呢困难。
C#设计模式系列:装饰模式(Decorator)的更多相关文章
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- 设计模式-09装饰模式(Decorator Pattern)
1.模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制:使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是 ...
- 设计模式之装饰模式(Decorator)摘录
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/fengbingchun/article/details/29237955 23种GOF设计模式一般分 ...
- 设计模式 笔记 装饰模式 Decorator
//---------------------------15/04/17---------------------------- //Decorator 装饰模式----对象结构型模式 /* 1:意 ...
- 结构型设计模式之装饰模式(Decorator)
结构 意图 动态地给一个对象添加一些额外的职责.就增加功能来说,D e c o r a t o r 模式相比生成子类更为灵活. 适用性 在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责. ...
- 设计模式之装饰模式(Decorator)
装饰模式原理:给对象增加特性,这种特性是一种累加的效果 代码如下 #include <iostream> #include <string> #include <list ...
- 【设计模式】—— 装饰模式Decorator
前言:[模式总览]——————————by xingoo 模式意图 在不改变原来类的情况下,进行扩展. 动态的给对象增加一个业务功能,就功能来说,比生成子类更方便. 应用场景 1 在不生成子类的情况下 ...
- Netty学习-IO体系架构系统回顾 & 装饰模式Decorator的具体使用
Netty学习-IO体系架构系统回顾 IO和NIO的学习 NIO - 1.4 开始出的 在网络应用框架中,NIO得到了大量的使用,特别是netty里面 前提:对IO及其了解 对IO的总结和回顾 理解J ...
- 设计模式系列之装饰模式(Decorator Pattern)——扩展系统功能
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
随机推荐
- linux安装oracle11g
准备oracle安装文件 Oracle11gR2包含两个文件linux_11gR2_database_1of2.zip和linux_11gR2_database_2of2.zip,将这两个文件通过SS ...
- ITree诞生啦!
经过一个月的码码码,一个面向OIer的ITree终于来辣! ... (似乎把OI遗弃在了某个角落了........... 一个月里,从只会py到写出ITree,真是不容易呢(其实就是两个多礼拜而已= ...
- lua 字符串 正则表达式 转义 特殊字符
string.gsub 函数有三个参数:目标串,模式串,替换串.基本作用是用来查找匹配模式的串,并将使用替换串其替换掉: s = string.gsub("Lua is good" ...
- 第一次IT技术面试经历
一.技术总监面试问题: 1.Hibernate的应用项目例举 2.jsp标签库例举 3.oracle的增删改查 4.关系型数据库的关联关系 5.数据库分页操作 二.技术总监面试问题: 1.for循环中 ...
- dede 调用原图的路径
步骤:1修改include/extend.func.php 添加如下代码: //取原图地址function bigimg($str_pic){$str_houzhi=substr($str_pic,- ...
- nmap
扫描端口 nmap -v -sS -open -iL iplist.txt -no-stylesheet -oX output.xml -p- -P0 -v 详细信息-sS 隐蔽扫描(半开syn).– ...
- hexo建个人博客
已经在腾讯云获得了域名和服务器,想着既然已经这样了,就折腾折腾自己的个人博客主页吧. 考虑再三决定用github pages来实现我的博客.github Pages可以被认为是用户编写的.托管在git ...
- Android分享一款漂亮的折叠书架菜单
一个Android折叠书架菜单,效果极佳,给人的视觉感觉很好,便于使用. FoldingMenu
- 【转】Web测试方法
看到好文章,拿过来给大家分享分享! 一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&am ...
- 用C#语言在Visual Studio 2010里开发一个自定义的PowerShell Cmdlet
1. 打开Visual Studio 2010 2. 新建一个基于Class Library的项目 3. 给项目起个名字然后OK 4. 为项目添加下列Reference System.Manageme ...