03、装饰模式(Decorator)
一、概念:
动态的给一个对象添加一些额外的职责,就增加的功能来说,装饰模式比生成子类更为灵活。【DP】
二、通俗的理解:
装饰模式是利用其中的方法为来对对象进行包装的,这样每个包装对象的事项就和如何使用这个对象分离了,
每个对象只关心自己的功能,不需要关心如何添加到对象链中去。
三、模式类图

四、对类图的解释;
Component是定义一个对象接口,可以给这些对象动态的添加职责,ConcreateComponent是定义了一个具体的对象,
也可以给这个对象添加 一些职责。Decorator,装饰抽象类,继承了Componet,从外类来扩展Component类的功能,
但对于Component来说,是无需知道Descorator的存在的。至于ConcreteDescorator就是具体的装饰对象,起到给Component添加职责的功能。
五、类图代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Decorator
{
//Component类; public abstract class Component
{
public abstract void Operation(); //定义的接口中的实现方法
} public class ConcreteComponent : Component
{
public override void Operation()
{
Console.WriteLine("具体对象的实现操作");
}
} public class Decorator : Component
{
protected Component component; public void SetComponent(Component component)
{
//设置Component
this.component = component;
} public override void Operation()
//重写Operation(),实际是执行的是Component的Operation()
{
if (component != null)
{
component.Operation();
}
}
} public class ConcreteDecoratorA : Decorator
{
public override void Operation()
{
base.Operation(); //关键的一步
SubedBehavior();
Console.WriteLine("具体装饰对象A的操作");
}
public void SubedBehavior()
{
//用来区别ConcreateDecoratorA和 ConcreateDecoratorB的区别
}
} public class ConcreteDecoratorB : Decorator
{ public override void Operation()
{
base.Operation();
AddedBehavior();
Console.WriteLine("具体装饰对象B的操作");
}
public void AddedBehavior()
{
//用来区别ConcreateDecoratorA和 ConcreateDecoratorB的区别
}
} //客户端代码的实现;
class Program
{
static void Main(string[] args)
{
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c);
d2.SetComponent(d1);
d2.Operation();
//装饰的方法是:首先用ConcreteComponent实例话对象c,然后用ConcreteDecorateA的实例化对象d1来包装c,在用ConcreteDecoratorB的对象d2来包装d1,最终执行d2的Operation() Console.ReadKey();
}
} }
六、结果显示

七、案例的使用:
在生活中我们时刻个电脑打交道,那么电脑就是我们补课或缺的一部分,那么我们肯定会给电脑进行添加东西,进行装饰了,
有外观上的装饰,同样也有内在的性能改变,这就用到了装饰模式了。对于电脑我们可以以性能的装饰来进行实例的演习。
性能中,我们可以加固态硬盘、内存条等。那么进行演示:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test_01
{ public abstract class Computer
{
public abstract void Show();
} public class HPComputer:Computer
{
public override void Show()
{
Console.WriteLine("以下是对惠普电脑的一系列操作");
}
} public class Decorator:Computer
{
protected Computer computer; public void SetCompueter(Computer computer)
{
this.computer = computer;
} public override void Show()
{
if(computer!=null)
{
computer.Show();
}
}
} public class SSD:Decorator
{
public override void Show()
{
base.Show();
Console.WriteLine("我的电脑加装了固态硬盘");
}
} public class Memory : Decorator
{
public override void Show()
{
base.Show();
Console.WriteLine("我的电脑加装了内存条");
}
} class Program
{
static void Main(string[] args)
{
HPComputer decorator = new HPComputer();
SSD ssd = new SSD();
Memory memory = new Memory(); ssd.SetCompueter(decorator);
memory.SetCompueter(ssd);
memory.Show();
Console.ReadKey();
}
}
}
运行结果

八、结语:到底在什么地方使用装饰模式
当系统需要新功能的时候,是向旧的类中添加新的代码,这些新加的代码通常装饰了原有类的核心职责或主要行为。
装饰模式是提供了一个非常好的解决方案,他把每个要装饰的功能放在单独的类中,并让这个类包装他所要装饰的对象,因此当需要执行特殊操作行为时,
客户代码就可以在运行时根据需要有选择的,按顺序的使用装饰功能 包装对象了【DP】
优点
1、装饰者模式比继承更灵活
2、可以通过一种动态的方式来扩展一个对象的功能,在运行时选择不同的装饰器,从而实现不同的行为。
3、通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合。
缺点
1、会产生很多小对象,增加了系统的复杂性。
2、更加易于出错,排错也很困难,较为烦琐。
03、装饰模式(Decorator)的更多相关文章
- 装饰模式/decorator模式/结构型模式
装饰模式Decorator 定义 为对象动态的增加新的功能,实现要求装饰对象和被装饰对象实现同一接口或抽象类,装饰对象持有被装饰对象的实例. java实现要点 定义一个接口或抽象类,作为被装饰者的抽象 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
- 设计模式 装饰模式(Decorator)
设计模式 装饰模式(Decorator) @author ixenos 装饰模式是什么 1.装饰模式以对客户端透明的方式对象的功能,是继承关系的一个替代方案,但装饰模式可以在不创造更多子类的情况下,对 ...
- 设计模式-装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活
- Netty学习-IO体系架构系统回顾 & 装饰模式Decorator的具体使用
Netty学习-IO体系架构系统回顾 IO和NIO的学习 NIO - 1.4 开始出的 在网络应用框架中,NIO得到了大量的使用,特别是netty里面 前提:对IO及其了解 对IO的总结和回顾 理解J ...
- 设计模式-09装饰模式(Decorator Pattern)
1.模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制:使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是 ...
- 设计模式系列之装饰模式(Decorator Pattern)——扩展系统功能
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 装饰模式 - Decorator 和 外观模式 - Facade
装饰模式 Decorator,不改变接口但动态给对象加入责任,所需功能按顺序串联起来控制,比生成子类灵活. 外观模式 Facade,让接口更简单.为子系统中的一组接口提供一个一致的界面. 参考:
- [工作中的设计模式]装饰模式decorator
一.模式解析 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式的要点主要是: 1.需要对已有对象扩展新的功能,又不希望改变原有对 ...
随机推荐
- CSS 选择器大全
在CSS中,选择器是用于选择要设置样式的元素的模式. 选择器 例子 描述 .class .intro 选择class=“intro”的所有元素 #id #firstname 选择id=“firstna ...
- flux架构的详细介绍和使用!
结构分为四个 视图 view动作 action派发器 dispatcher数据商店 store 流程: 用户操作视图 视图(view)发送动作(action)到派发器(dispatcher) 由派发器 ...
- apicloud开发app
1.apicloud官网 2.注册登录 3.开发控制台 4.创建应用 5.代码=>svn拉取代码,账号:注册账号的邮箱,密码:获取分支密码中的密码 6.编辑器下载对应的插件或者直接使用apicl ...
- 春秋-SQLi题
这道题挺好的 学到的知识 sprintf()构成的sql注入漏洞 题目环境今天做的时候坏了 留下这几篇博客学习 https://blog.csdn.net/nzjdsds/article/detail ...
- Linux磁盘信息查询及删除文件操作
查询磁盘容量 $df -hl 删除文件固定行数 (1)删除第一行 $sed -i '1d' a.txt (2)删除指定行数 $sed -i '1,100d' a.txt 删除末尾行 $sed -i ' ...
- Spring Cloud Netflix之Euraka Server注册中心
Spring Cloud简介 Spring Cloud是基于Spring Boot的一套实现微服务架构的生态组件.生态组件中包含Spring Cloud NetFlix,Spring Cloud Fe ...
- Python 爬虫之 Beautifulsoup4,爬网站图片
安装: pip3 install beautifulsoup4 pip install beautifulsoup4 Beautifulsoup4 解析器使用 lxml,原因为,解析速度快,容错能力强 ...
- STM32F4 串口IAP程序要点
1. IAP(bootloader)程序 1.1 内部Flash地址分配 /* Start of the Flash address */ #define STM32_FLASH_BASE 0x080 ...
- Ubuntu 18.04通过命令禁用/开启触控板
Ubuntu下经常遇到无法用快捷键关闭触控板的情况,博主的电脑安装Ubuntu18.04后便出现了该问题. 解决办法: 首先查看输入设备的id,命令行输入: xinput ,插鼠标与不插鼠标时,Tou ...
- linux(05) 编译安装py3
一.编译安装python3 https://www.cnblogs.com/pyyu/p/9015317.html 1.下载python3的源码 cd /opt yum install wget -y ...