0.C#设计模式-简单工厂模式

1.C#设计模式--工厂方法模式

2.C#设计模式--抽象工厂模式

3.C#设计模式--单例模式

4.C#设计模式--建造者模式

5.C#设计模式--原型模式

6.C#设计模式--设配器模式

设计模式:

装饰器模式(Decorator Pattern)

简单介绍:

装饰器模式(Decorator Pattern):

假设有一部手机你购买来以后只有手机,然后你需要再给其添加一些装饰,比如保护壳,钢化膜或者手机贴画等,这个就是装饰者模式的思想

装饰器模式主要组成部分:

Component:定义一个对象接口,可以给这些对象动态地添加职责

ConcreteComponent:定义一个对象,可以给这个对象添加一些职责

Decorator:维持一个指向Component的指针,并定义一个与Component接口一致的接口

ConcreteDecorator:负责向ConcreteComponent添加功能

在装饰模式中,Decorator定义了一个装饰接口类。因为Decorator与ConcreteComponent继承同一个接口,所以继承Decorator的类ConcreteDecorator可以使用ConcreteComponent的方法,再在ConcreteDecorator里面加入一些新的方法,也就是装饰,就成为了一个包装好的装饰类。

装饰器模式类图:

装饰者模式C#代码举例:

Component 对象类

     /// <summary>
/// 对象类
/// </summary>
public abstract class Component
{
public abstract void Print();
}

ConcreteComponenet 类 对象的修饰实现类

     public class ConcreteComponent:Component
{
/// <summary>
/// 对象修饰类
/// </summary> public override void Print()
{
Console.WriteLine("我是ConcreteComponent!");
}
}

Decorator类 装饰器类

     /// <summary>
/// 装饰器类
/// </summary>
public class Decorator:Component
{
protected Component _component; public void SetComponent(Component component)
{
this._component = component;
}
public override void Print()
{
if (_component != null)
{
_component.Print();
}
}
}

ConcreteDecoratorA类 装饰子类A

     /// <summary>
/// 装饰子类A
/// </summary>
public class ConcreteDecoratorA: Decorator
{
public override void Print()
{
base.Print();
Console.WriteLine("我是ConcreteDecoratorA!");
}
}

ConcreteDecoratorB 装饰子类B

     /// <summary>
/// 装饰子类B
/// </summary>
public class ConcreteDecoratorB:Decorator
{
public override void Print()
{
base.Print();
Console.WriteLine("我是ConcreteDecoratorB!");
}
}

用户测试类:

     class Client
{
static void Main(string[] args)
{
ConcreteComponent concreteComponent = new ConcreteComponent();
ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA();
ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(); concreteDecoratorA.SetComponent(concreteComponent);
concreteDecoratorA.Print();
Console.WriteLine("--------------------------------");
concreteDecoratorB.SetComponent(concreteDecoratorA); concreteDecoratorB.Print();
Console.Read(); }
}

运行结果:

源代码工程文件下载

装饰者模式实际生活举例

举例说明

假设有一部手机你购买来以后只有手机,然后你需要再给其添加一些装饰,比如保护壳,钢化膜或者手机贴画等

mobilephone 原手机接口

     /// <summary>
/// 手机接口
/// </summary>
public abstract class MobilePhone
{
public abstract void Print();
}

AppleMobilePhone类 手机的修饰类

     /// <summary>
/// 手机的修饰
/// </summary>
public class AppleMobilePhone :MobilePhone
{
public override void Print()
{
Console.WriteLine("我是一部苹果手机!");
}
}

Decorator类 装饰器类

     /// <summary>
/// 装饰器
/// </summary>
public class Decorator:MobilePhone
{
protected MobilePhone mobliePhone;
public void SetMobilePhone(MobilePhone phone)
{
this.mobliePhone = phone;
} public override void Print()
{
if (mobliePhone != null)
{
mobliePhone.Print();
}
}
}

MobilePhoneShell类 手机壳修饰类

     /// <summary>
/// 手机壳修饰类
/// </summary>
public class MobilePhoneShell:Decorator
{
public override void Print()
{
base.Print();
Console.WriteLine("我是一个手机保护壳!");
}
}

MobilePhoneStickers类 手机贴画修饰类

     /// <summary>
/// 手机贴画修饰类
/// </summary>
public class MobilePhoneStickers:Decorator
{
public override void Print()
{
base.Print();
Console.WriteLine("我是一个手机贴画!");
}
}

用户测试类

     class Client
{
static void Main(string[] args)
{
Console.WriteLine("----------------手机-----------------");
AppleMobilePhone appleMobilePhone = new AppleMobilePhone();
appleMobilePhone.Print(); Console.WriteLine("---------加了手机壳的手机----------------");
MobilePhoneShell mobilePhoneShell = new MobilePhoneShell();
mobilePhoneShell.SetMobilePhone(appleMobilePhone);
mobilePhoneShell.Print(); Console.WriteLine("----------加了手机贴画的手机---------------");
MobilePhoneStickers mobilePhoneStickers = new MobilePhoneStickers();
mobilePhoneStickers.SetMobilePhone(appleMobilePhone);
mobilePhoneStickers.Print();
Console.WriteLine("----------既加了手机壳又加了手机贴画的手机--------"); mobilePhoneStickers.SetMobilePhone(mobilePhoneShell);
mobilePhoneStickers.Print(); Console.Read(); }
}

运行结果

源代码工程文件下载地址

C#设计模式--装饰器模式的更多相关文章

  1. JAVA设计模式--装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...

  2. 从ES6重新认识JavaScript设计模式: 装饰器模式

    1 什么是装饰器模式 向一个现有的对象添加新的功能,同时又不改变其结构的设计模式被称为装饰器模式(Decorator Pattern),它是作为现有的类的一个包装(Wrapper). 可以将装饰器理解 ...

  3. C#设计模式-装饰器模式(Decorator Pattern)

    引言 当我们完成一个软件产品开发后就需要对其进行各种测试,适配快速迭代下质量的保障.当有一个完善的产品的对象后,如果我们想要给他添加一个测试功能,那么我们可以用一个新的类去装饰它来实现对原有对象职责的 ...

  4. 设计模式-装饰器模式(Decrator Model)

    文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/6057666.html 目录 1.概述 2.目的 3.结构组成 4.实现 5.总结 1.概 ...

  5. php设计模式 装饰器模式

    装饰器模式,可以动态地添加修改类的功能. 一个类提供了一项功能,如果要修改并添加额外的功能,传统的编程模式需要写一个子类继承它,并重新实现类的方法.使用装饰器模式,仅需要在运行时添加一个装饰器对象即可 ...

  6. 说说设计模式~装饰器模式(Decorator)

    返回目录 装饰器模式,也叫又叫装饰者模式,顾名思义,将一个对象进行包裹,包装,让它变成一个比较满意的对象,这种模式在我们平时项目开发中,经常会用到,事实上,它是处理问题的一种技巧,也很好的扩展了程序, ...

  7. 说说设计模式~装饰器模式(Decorator)~多功能消息组件的实现

    返回目录 为何要设计多功能消息组件 之前写过一篇装饰器模式的文章,感觉不够深入,这次的例子是实现项目中遇到的,所以把它拿出来,再写写,之前也写过消息组件的文章,主要采用了策略模式实现的,即每个项目可以 ...

  8. Java设计模式--装饰器模式到Java IO 流

    装饰器模式 抽象构件角色:给出一个抽象接口,以规范准备接受附加责任的对象. 具体构件角色:定义准备接受附加责任的对象. 抽象装饰角色:持有一个构件对象的实例,并对应一个与抽象构件接口一致的接口. 具体 ...

  9. Groovy 设计模式 -- 装饰器模式

    http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern 装饰器模式, 起到美化原始对象的作用. 一个被 ...

随机推荐

  1. jQuery使用动态渲染表单功能完成ajax文件下载

    原文链接:http://www.poluoluo.com/jzxy/201301/195126.html 封装的通用js函数代码: // Ajax 文件下载 jQuery.download = fun ...

  2. SpringMVC文件上传基础

    maven依赖 <!--文件上传所需--> <dependency> <groupId>commons-fileupload</groupId> < ...

  3. TensorFlow:tf.train.Saver()模型保存与恢复

    1.保存 将训练好的模型参数保存起来,以便以后进行验证或测试.tf里面提供模型保存的是tf.train.Saver()模块. 模型保存,先要创建一个Saver对象:如 saver=tf.train.S ...

  4. iOS:获取 NSDate 的年

    NSDate *currentDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComp ...

  5. 一则Gedit死机引起的错误之解决

    一次,虚拟机中CentOS不明原因死机了,终端打开不能操作,SecureCRT和从SecureCRT中运行的托关于本机Windows下的Xming下的gedit也死掉了,无奈只能强制关机.重新启动后其 ...

  6. 安卓开发笔记——打造万能适配器(Adapter)

    为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需 ...

  7. Android学习之——GridView

    背景知识 GridView在Android开发中和ListView一样经常被使用.如我们经常使用的快图浏览,里面就有将图片的布局改为网格(即GridView)的选项.还有约X神器——陌陌的搜索界也是用 ...

  8. 使用springmvc,jsp,结合网页文本编辑器kindEditor实现基本博客编辑功能

    kindEditor官网:http://kindeditor.net/demo.php 个人实践: 为了在自己的项目中引入一个类似用户写博客的功能,在网上找到了kindeditor,真心又好又易用. ...

  9. Python打包-py2exe

    上篇文章讲了pyinstaller,可以打包成包含Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX等操作系统下的可执行文件,如果只针对Windows ...

  10. 《倾国倾城》全套源代码:client+服务端+资源,歧视复制帖子

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...