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).在“装饰模式”中很好的提现了开放 ...
随机推荐
- 【.NET】对文件的对称加密
using System;using System.IO;using System.Security.Cryptography;namespace ConsoleApp_SymmetricalEncr ...
- thinkphp数据库添加表单提交的数据
$data['catename'] = I('catename'); 获取表单的数据 $cate=D('cate'); 实例化cat ...
- Centos更新yum packet源
在使用Centos时,常常会遇到使用yum安装某些系统依赖包,特别是第三方软件库(如openstack软件库)时,无法找到包源.因此,需要将Centos的yum源进行更新,扩展,以便可以通过yum的方 ...
- win7如何设置某个软件不弹出用户账户控制
手动修改注册表: 在 HKEY_CURRENT_USERS\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers 键下面 ...
- 无法运行maven项目
tomcat Server Location 选择 User Tomcat installation 设置CATALINA_HOME环境变量(tomcat start.bat启动不了)1.CATALI ...
- .net IL 指令速查
名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...
- Vue.js学习 Item6 -- Class 与 样式绑定
数据绑定一个常见需求是操作元素的 class 列表和它的内联样式.因为它们都是 attribute,我们可以用 v-bind 处理它们:只需要计算出表达式最终的字符串.不过,字符串拼接麻烦又易错.因此 ...
- 什么是Ajax无刷新技术?
浏览器实例化一个Ajax对象,这个对象发送一个HTTP请求,并且携带一定的参数,传输到后台.后台服务器接收这些参数,同时过滤一下传过来的参数,做出逻辑判断.如果需要数据库操作参与,就要取出数据,格式化 ...
- 利用java读写Excel文件
一.读取Excel文件内容 java 代码 public static String readExcel(File file){ StringBuffer sb = new StringBuffer( ...
- AsyncTask的简单使用
package com.zzw.life; import android.app.Activity; import android.os.AsyncTask; import android.os.Bu ...