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. c# 阿拉伯数字转成中文

    调用方法: public string ConvertToChineseNumber(string old) { Chinese ch = new Chinese(); long num = Conv ...

  2. perl 截取 fastq文件

    #!/usr/bin/perl -w use warnings; use strict; input_fastq trim_length}; ; my ($fastq, $trim_length) = ...

  3. VC++ 内存泄露与检测的一种方法

        本文介绍,当VC++或者MFC程序,出现内存泄露时,如何快速定位的方法,这种方法有一定的局限性,在注意事项中会给出的. MFC程序     当MFC程序出现内存泄露时,退出程序时的VS调试输出 ...

  4. asp.net存储过程分页+GridView控件 几百万数据 超快

    存储过程:---亲测275万数据,分页速度N快 ))+' '+@orderid+' from '+@tablename+' '+@tmpOrderid set @sql='select top'+st ...

  5. 内存管理 初始化(六)vmalloc_init 及 ioremap

    是不是我错了,本想这个函数会如网上所说将进行非连续内存管理的初始化,但是对于2.6.34的ARM架构而言,该函数实际完成的业务非常少. 内存管理的初始化读到此处,我感觉原有的认识存在很大缺陷: (1) ...

  6. linux cfs调度器_模型实现

    调度器真实模型的主要成员变量及与抽象模型的对应关系 I.cfs_rq结构体    a) struct sched_entity *curr        指向当前正在执行的可调度实体.调度器的调度单位 ...

  7. workerman定时器使用 php定时任务

    add int \Workerman\Lib\Timer::add(float $time_interval, callable $callback [,$args = array(), bool $ ...

  8. [Arch] 02. Design principle and Software Pattern

    Ref: 软件设计的七大原则 有时间的话,还需进一步深入理解. Figure, 重要的前五个原则 单一职责原则 (Simple responsibility pinciple SRP) 类的设计趋向于 ...

  9. ios的单元測试OCUnit以及更新了之后的XCTestCase

    1.像一般创建项目的步骤一样.创建一个用于測试的项目或者打开一个待測试的项目. (oc是5.0之前所使用的測试,如今用的是XCtestCase,默认会创建一个主的測试类.曾经版本号可能非常多步骤省去) ...

  10. SpringMVC -- 梗概--源码--贰--拦截器:Interceptor

    附:实体类 1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versi ...