结构型设计模式之装饰模式(Decorator)
| 结构 | ![]() |
| 意图 | 动态地给一个对象添加一些额外的职责。就增加功能来说,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)的更多相关文章
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
- 享元模式 FlyWeight 结构型 设计模式(十五)
享元模式(FlyWeight) “享”取“共享”之意,“元”取“单元”之意. 意图 运用共享技术,有效的支持大量细粒度的对象. 意图解析 面向对象的程序设计中,一切皆是对象,这也就意味着系统的运行将 ...
- 代理模式 PROXY Surrogate 结构型 设计模式(十四)
代理模式 PROXY 别名Surrogate 意图 为其他的对象提供一种代理以控制对这个对象的访问. 代理模式含义比较清晰,就是中间人,中介公司,经纪人... 在计算机程序中,代理就表示一个客户端不想 ...
- 桥接模式 桥梁模式 bridge 结构型 设计模式(十二)
桥接模式Bridge Bridge 意为桥梁,桥接模式的作用就像桥梁一样,用于把两件事物连接起来 意图 将抽象部分与他的实现部分进行分离,使得他们都可以独立的发展. 意图解析 依赖倒置原 ...
- 组合模式 合成模式 COMPOSITE 结构型 设计模式(十一)
组合模式(合成模式 COMPOSITE) 意图 将对象组合成树形结构以表示“部分-整体”的层次结构. Composite使得用户对单个对象和组合对象的使用具有一致性. 树形结构介绍 为了便于理解, ...
- 12、Decorator 装饰器 模式 装饰起来美美哒 结构型设计模式
1.Decorator模式 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式(Decorator Pattern)允许向一个现 ...
- 设计模式之装饰模式(Decorator)摘录
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/fengbingchun/article/details/29237955 23种GOF设计模式一般分 ...
- 设计模式 笔记 装饰模式 Decorator
//---------------------------15/04/17---------------------------- //Decorator 装饰模式----对象结构型模式 /* 1:意 ...
- 设计模式-09装饰模式(Decorator Pattern)
1.模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制:使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是 ...
随机推荐
- anaconda+jupyter notebook 安装配置
安装Anaconda 从清华大学开源软件镜像站选择合适自己的版本 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda ...
- Python学习笔记:logging(日志处理)
在一个软件中,日志是可以说必不可少的一个组成部分,通常会在定位客户问题或者记录软件使用情况等场景中会用到.logging模板块是Python的一个内置标准库,用于实现对日志的控制输出,对于平常的日志输 ...
- 数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图
二叉查找树(Binary Search Tree) 是一种树形的存储数据的结构 如图所示,它具有的特点是: 1.具有一个根节点 2.每个节点可能有0.1.2个分支 3.对于某个节点,他的左分支小于自身 ...
- 不同级域名中的 Cookie 共享
HTTP 响应头中 Set-Cookie 行未指定 domain 时则设置访问的域名 seliote.com 可以设置 seliote.com(也可以写成 .seliote.com 意思一样) 与 w ...
- hive-pom.xml
4.0.0 <groupId>com.cenzhongman</groupId> <artifactId>hive</artifactId> <v ...
- TouTiao开源项目 分析笔记9 实现一个问答主页面
1.根据API返回创建几个基础的Bean 1.1.WendaArticleDataBean类 API返回的数据如下: /** * cell_type : 36 * extra : {"wen ...
- 9.4python开发之virtualenv与virtualenvwrapper
在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...
- 统计大写字母个数&压缩和去重(过滤)
找出给定字符串中大写字符(即'A'-'Z')的个数 接口说明 原型:int CalcCapital(String str); 返回值:int 知识点 字符串 运行时间限制 10M 内存限制 128 输 ...
- 怎么设置才能让外网ip可以访问mysql数据库[转]
转自: http://www.hongyanliren.com/89.html 使用mysql中,很多人都会遇到这样的问题:在vps服务器或者云服务器上安装了mysql后,使用其他工具在外网ip之下根 ...
- Python 绘制棋盘
import turtle pen = turtle.Pen() pen.speed(10) width = 30 # 格子宽度 count = 18 # 横向纵向格子数 o = width * co ...
