当前位置: 首页 > news >正文

装饰模式

摘自《大话设计模式》(程杰 著)

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

image

Component 是定义一个对象接口,可以给这些对象动态地添加职责。
ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。
Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的。
ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。

装饰模式是利用 SetComponent 来对对象进行包装的。
这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。

基本代码实现

Component.cs

abstract class Component
{public abstract void Opearation();
}

ConcreteComponent.cs

class ConcreteComponent: Component
{public override void Operation(){Console.WriteLine("具体的操作");}
}

Decorator.cs

abstract class Decorator: Component
{protected Component component;// 设置 Componentpublic void SetComponent(Component component){this.component = component;}// 重写 Operation(), 实际执行的是 Component 的 Operation()public override void Operation(){if (component != null){component.Operation();}}
}

ConcreteDecorator.cs

class ConcreteDecoratorA: Decorator
{// 本类独有功能,用以区别 ConcreteDecoratorBprivate string addedState;public override void Operation(){// 首先执行原 Component 的 Operation()base.Operation();// 再执行本类功能addedState = "New State";Console.WriteLine("具体装饰对象A的操作");}
}class ConcreteDecoratorB : Decorator
{public override void Operation(){// 首先执行原 Component 的 Operation()base.Operation();// 再执行本类功能AddedBehavior();Console.WriteLine("具体装饰对象B的操作");}// 本类独有方法,用来区别 ConcreteDecoratorAprivate void AddedBehavior(){}
}

Program.cs

static void Main(string[] arg)
{ConcreteComponent c = new ConcreteComponent();ConcreteDecoratorA d1 = new ConcreteDecoratorA();ConcreteDecoratorB d2 = new ConcreteDecoratorB();d1.SetComponent(c);d2.SetComponent(d1);d2.Operation();Console.Read();
}

实例:服饰装扮

如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。
同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。

image

Person.cs (ConcreteComponent)

class Person
{public Person(){}private string name;public Person(string name){this.name = name;}public virtual void Show(){Console.WriteLine("装扮的{0}",name);}
}

Finery.cs (Decorator)服饰类

class Finery : Person
{protected Person component;// 打扮public void Decorate(Person component){this.component = component;}public override void Show(){if (component != null){component.Show();}}
}

具体服饰类 ConcreteDecorator

class TShirts: Finery
{public override void Show(){Console.Write("大T恤");base.Show();}
}class BigTrouser : Finery
{public override void Show(){Console.Write("垮裤");base.Show();}
}// 其余类似

客户端代码

Person person = new Person("Harry");Console.WriteLine("\n第一种装扮:");TShirts tshirt = new TShirts();
BigTrouser bigtrouser = new BigTrouser();tshirt.Decorate(person);
bigtrouser.Decorate(tshirt);
bigtrouser.Show();
http://www.gsyq.cn/news/82238.html

相关文章:

  • BIM+GIS深度融合:高速公路数字化底座建设方案
  • 2025年12月精密铝压铸,铝合金压铸,铝压铸厂家推荐:行业权威盘点与品质红榜发布​ - 品牌鉴赏师
  • CH5xx 程序中获取代码大小
  • 2025年12月房屋沉降检测,房屋倾斜检测,房屋质量检测公司推荐:行业权威盘点与品质红榜发布​ - 品牌鉴赏师
  • 上海全屋定制厂家推荐:靠谱之选打造理想家居 - myqiye
  • 2025年长三角包装印刷企业排名:上海万通卡牌印刷工艺怎样? - 工业品牌热点
  • 2025年河南正规叛逆学校排名:资质齐全的叛逆学校有哪些? - 工业推荐榜
  • 大一统视角理解扩散模型Understanding Diffusion Models: A Unified Perspective
  • C++学习笔记 03 枚举
  • TinyMCE + Vue 使用
  • 2025年推荐厚片吸塑制品厂家TOP5:吸塑制品生产厂家解析 - 工业品牌热点
  • C++学习笔记02 static关键字
  • doker批量停用和删除容器命令
  • 国产替代实验室离心机实力厂家推荐:四川蜀科、湖南凯达、美瑞克仪器 - 品牌推荐大师
  • 为什么我们需要使用react提供的ChildrenAPI而不是 JavaScript 的 map?
  • 医用/低速/生物制药/血站/大容量/微量高速/国产离心机哪个品牌好?实力厂家推荐与选购建议指南 - 品牌推荐大师
  • IDEA中创建Spring Boot项目兼容JDK8的解决方案
  • 医用/低速/生物制药/血站/大容量/微量高速/血库/自动/国产离心机优质品牌有哪些?头部企业及高口碑厂家推荐 - 品牌推荐大师
  • 2025全球力传感器品牌价值榜——高精度测量领域十大金牌供应商技术解密 - 品牌推荐大师1
  • 2025年度中国铝合金精密管材厂家推荐:专业的铝合金精密管材 - mypinpai
  • 2025年杭州艺术学校美术专业招生机构TOP5推荐:杭州艺术 - 工业品牌热点
  • 详细介绍:跨端框架对决:React Native vs Flutter深度对比
  • 常熟国强和茂管材有限公司在当地的口碑怎样过?其发张前景如何? - mypinpai
  • 用户推荐的好评超声波换能器厂家有哪些/哪个品牌售后好 - 品牌推荐大师
  • 2025步入式恒温恒湿试验箱十大品牌top发布,技术过硬、服务完善的恒温恒湿试验箱生产企业推荐 - 品牌推荐大师1
  • 用户推荐的好评超声波雾化设备厂家有哪些/哪个品牌售后好 - 品牌推荐大师
  • windriver 第9章:特定芯片组的增强支持
  • 2025年实力强的超级电容制造企业TOP5:国内知名制造商权 - myqiye
  • 负载均衡相关的upstream模块参数
  • 2025年杭州会议室全彩屏五大靠谱货源渠道推荐,包安装维护服 - 工业品牌热点