结构
意图 动态地给一个对象添加一些额外的职责。就增加功能来说,D e c o r a t o r 模式相比生成子类更为灵活。
适用性
  • 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
  • 处理那些可以撤消的职责。
  • 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
 using System;

     abstract class Component
{
public abstract void Draw();
} class ConcreteComponent : Component
{
private string strName;
public ConcreteComponent(string s)
{
strName = s;
} public override void Draw()
{
Console.WriteLine("ConcreteComponent - {0}", strName);
}
} abstract class Decorator : Component
{
protected Component ActualComponent; public void SetComponent(Component c)
{
ActualComponent = c;
}
public override void Draw()
{
if (ActualComponent != null)
ActualComponent.Draw();
}
} class ConcreteDecorator : Decorator
{
private string strDecoratorName;
public ConcreteDecorator (string str)
{
// how decoration occurs is localized inside this decorator
// For this demo, we simply print a decorator name
strDecoratorName = str;
}
public override void Draw()
{
CustomDecoration();
base.Draw();
}
void CustomDecoration()
{
Console.WriteLine("In ConcreteDecorator: decoration goes here");
Console.WriteLine("{0}", strDecoratorName);
}
} /// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
Component Setup()
{
ConcreteComponent c = new ConcreteComponent("This is the real component"); ConcreteDecorator d = new ConcreteDecorator("This is a decorator for the component"); d.SetComponent(c); return d;
} public static int Main(string[] args)
{
Client client = new Client();
Component c = client.Setup(); // The code below will work equally well with the real component,
// or a decorator for the component c.Draw(); return ;
}
}

装饰模式

结构型设计模式之装饰模式(Decorator)的更多相关文章

  1. 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)

    原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...

  2. 享元模式 FlyWeight 结构型 设计模式(十五)

    享元模式(FlyWeight)  “享”取“共享”之意,“元”取“单元”之意. 意图 运用共享技术,有效的支持大量细粒度的对象. 意图解析 面向对象的程序设计中,一切皆是对象,这也就意味着系统的运行将 ...

  3. 代理模式 PROXY Surrogate 结构型 设计模式(十四)

    代理模式 PROXY 别名Surrogate 意图 为其他的对象提供一种代理以控制对这个对象的访问. 代理模式含义比较清晰,就是中间人,中介公司,经纪人... 在计算机程序中,代理就表示一个客户端不想 ...

  4. 桥接模式 桥梁模式 bridge 结构型 设计模式(十二)

      桥接模式Bridge   Bridge 意为桥梁,桥接模式的作用就像桥梁一样,用于把两件事物连接起来   意图 将抽象部分与他的实现部分进行分离,使得他们都可以独立的发展.  意图解析 依赖倒置原 ...

  5. 组合模式 合成模式 COMPOSITE 结构型 设计模式(十一)

    组合模式(合成模式 COMPOSITE) 意图 将对象组合成树形结构以表示“部分-整体”的层次结构. Composite使得用户对单个对象和组合对象的使用具有一致性.   树形结构介绍 为了便于理解, ...

  6. 12、Decorator 装饰器 模式 装饰起来美美哒 结构型设计模式

    1.Decorator模式 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式(Decorator Pattern)允许向一个现 ...

  7. 设计模式之装饰模式(Decorator)摘录

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/fengbingchun/article/details/29237955 23种GOF设计模式一般分 ...

  8. 设计模式 笔记 装饰模式 Decorator

    //---------------------------15/04/17---------------------------- //Decorator 装饰模式----对象结构型模式 /* 1:意 ...

  9. 设计模式-09装饰模式(Decorator Pattern)

    1.模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制:使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是 ...

随机推荐

  1. html5的canvas绘制线条,moveTo和lineTo详解

    今天在看html5,里面新增的属性有一个canvas,它相当于一个画布你可以用js在里面画你想要的效果!我在w3c的手册里面看到用moveTo和lineTo绘制线条讲的不是很清楚,尤其是moveTo和 ...

  2. 图解HTTP总结(4)——返回结果的HTTP状态码

    HTTP状态码负责表示客户端HTTP请求的返回结果.标记服务器端的处理是否正常.通知出现的错误等工作. 状态码的类别 2XX 成功 200 OK 表示从客户端发来的请求在服务器端被正常处理了. 在响应 ...

  3. bash:/usr/bin/mogod/:connot execute binary:exec fotmat error

    前两天博主在安装mogodb的时候出现以下错误,很是郁闷,明明按照教程里面做的,怎么到最后 执行命令的时候出错了呢,以下为错误execute binary:exec fotmat error" ...

  4. Uva12230Crossing Rivers 数学

    Uva12230Crossing Rivers 问题: You live in a village but work in another village. You decided to follow ...

  5. [CodeForces940E]Cashback(set+DP)

    Description Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is ...

  6. Permute Digits 915C

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...

  7. 十四、pymysql模块

    一.安装的两种方法 第一种 #安装 pip3 install pymysql 第二种 二.链接,执行sql,关闭(游标) import pymysql user= input('用户名:>> ...

  8. Spring---配置文件概述

    概述 Spring 的配置文件是用于指导 Spring 工厂进行Bean的生产.依赖关系注入及 Bean 实例分发的“图纸”,它是一个或多个标准的XML文档,J2EE 程序员必须学会并灵活应用这份“图 ...

  9. Atom使用插件精选(FE)

    [转]原文https://zhuanlan.zhihu.com/p/24753739?refer=AlenQi Atom琳琅满目的插件中,为前端coder推荐一些实用的插件. sync-setting ...

  10. c#集合的使用

    //添加单个元素用Add方法 ArrayList list = new ArrayList(); list.Add(true); list.Add(); list.Add("小陈" ...