C#设计模式之装饰者模式(Decorator Pattern)
1.概述
装饰者模式,英文名叫做Decorator Pattern。装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
2.特点

/// <summary>
/// 定义Component对象接口
/// </summary>
public abstract class Component
{
public abstract void Operation();//一个抽象的职责
}
/// <summary>
/// 具体对象
/// </summary>
class ConcreteComponent : Component
{
public override void Operation()
{
Console.WriteLine("具体对象的操作");
}
}
//装饰者抽象类
abstract class Decorator : Component
{ protected Component component; public void SetComponent(Component component)
{ this.component = component; } public override void Operation()
{
if (component != null)
{
component.Operation();
}
}
} class ConcreteDecoratorA : Decorator
{ public override void Operation()
{ base.Operation(); //首先运行原Compnent的Operation(),再执行本类的功能,如AddedBehavior,相当于对原Component进行了装饰 Console.WriteLine("具体装饰对象A的操作");
}
}
class ConcreteDecoratorB : Decorator
{ public override void Operation()
{ base.Operation(); //首先运行原Compnent的Operation(),再执行本类的功能,如AddedBehavior,相当于对原Component进行了装饰 Console.WriteLine("具体装饰对象B的操作");
}
}
(2)无抽象接口
public class Car
{
public virtual void Description()
{
Console.Write("基本");
}
} public class ESPDecorator : Car
{
Car car;
public ESPDecorator(Car car)
{
this.car = car;
}
public override void Description()
{
car.Description();
Console.WriteLine("带有ESP功能");
}
}
public class OtherDecorator : Car
{
Car car;
public OtherDecorator(Car car)
{
this.car = car;
}
public override void Description()
{
car.Description();
Console.WriteLine("带有其它功能");
}
}
代码调用
//第一种
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
d1.SetComponent(c);
ConcreteDecoratorB d2 = new ConcreteDecoratorB();
d2.SetComponent(c);
d2.Operation();
//第二种
Car car = new ESPDecorator(new OtherDecorator(new Car()));
car.Description();
Console.Read();
C#设计模式之装饰者模式(Decorator Pattern)的更多相关文章
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
- python 设计模式之装饰器模式 Decorator Pattern
#写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- c#设计模式之装饰器模式(Decorator Pattern)
引子 在面向对象语言中,我们常常会听到这样一句话:组合优于继承.那么该如何去理解这句话呢? 下面我将以游戏装备为模型用简单的代码去展示它 先创建一个装备的抽象类,然后创建刀枪2个具体的业务子类 pub ...
- 【UE4 设计模式】装饰器模式 Decorator Pattern
概述 描述 动态地给一个对象增加一些额外的职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更为灵活.是一种对象结构型模式. 套路 抽象构件(Component) 具体构 ...
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法
装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...
- 设计模式 - 装饰者模式(Decorator Pattern) 具体解释
装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
随机推荐
- js中字符串,数字之间转换的常用方法
var number={ num:, num1:"2你好" }; //将数字转换为字符串 var str=number.num.toString();//十进制 );//二进制 ) ...
- c# winform 关于DataGridView的一些操作(很全,绝对够用)
转自:http://heisetoufa.iteye.com/blog/405317 设置字段名 设置字段值 设定单元格表示 Error图标 设定当前单元格 取得当前单元格内容 取得当前单元格的列 I ...
- linux 文本处理
tr,awk,sed 一:tr 1.大小写转换 cat file | tr [a-z] [A-Z] > new_file(大写 --> 小写) cat file | tr [A-Z] [a ...
- PayPal 开发详解(一):注册PayPal帐号
1.注册paypal帐号 https://www.paypal.com 2.使用刚才注册的paypal帐号登录3.进入开发者中心 4.登录开发者中心 5.登录 查看我们paypal Sandbox测试 ...
- 3月2日 Linked List Cycle
今天星期天,准备好周一的PPT内容,再回来做题,以后考虑周末做一个APP或者微信帐号玩吧. 回到题目, Linked List Cycle,一个检查单项链表是否有环路的问题. 题目周五的时候就简单做过 ...
- 必须会的SQL语句(六)查询
1.基础的查询 1)重命名列 select name as '姓名' from 表名 2)定义常量列 select 是否 ='是' from 表名 3) ...
- MySQL的相关概念介绍
MySQL 为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格"的概念, 一个 ...
- FaceBook微光闪烁---第三方开源--shimmer-android
Android上的微光闪烁shimmer效果,实现的手段不少,其中比较好的是facebook做的开源库:shimmer-android,其在github上的项目主页是:https://github.c ...
- 开源web终端ssh解决方案-gateone简介
好久都没来写博客,最近忙啥去了呢? 一是忙于saltstack的二次开发,二是云计算的学习研究中,所以就一直没写东西,今天给大家介绍个工具. 1. 首先来说一下为什么要 web ssh? 许多人不是说 ...
- ng-summit and $watch() funciton
<div ng-app> <form ng-submit="requestFunding()" ng-controller="StartUpContro ...