【软考学习】设计模式——装饰模式
来源:互联网 发布:mmap文件阅读器 mac 编辑:程序博客网 时间:2024/06/08 18:04
【背景】
设计模式是非常重要的一块知识,每个设计模式都值得深入了解和学习。
【内容】
结构型设计模式总结:
装饰设计模式总结:
一、定义:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。二、UML结构图:
三、代码实现:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 装饰模式{ 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(); Console.Read(); } } abstract class Component { public abstract void Operation(); } class ConcreteComponent : Component { public override void Operation() { Console.WriteLine("具体对象的操作"); } } abstract class Decorator : Component { protected Component component; public void SetComponent(Component component) { this.component = component; } public override void Operation() { if (component!=null) { component.Operation(); } } } class ConcreteDecoratorA : Decorator { private string addedState; public override void Operation() { base.Operation(); addedState = "New State"; Console.WriteLine("具体装饰对象A的操作"); } } class ConcreteDecoratorB : Decorator { public override void Operation() { base.Operation(); AddedBehavior(); Console.WriteLine("具体装饰对象B的操作"); } private void AddedBehavior() { } }}
阅读全文
0 0
- 【软考学习】设计模式——装饰模式
- 软考设计模式——装饰模式(C++)
- 软考总结——装饰模式
- 【软考学习】设计模式——单例模式
- 【软考学习】设计模式——建造者模式
- 【软考学习】设计模式——原型模式
- 【软考学习】设计模式——适配器模式
- 【软考学习】设计模式——组合模式
- 【软考学习】设计模式——代理模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计思想学习—装饰者模式
- 【软考学习】设计模式——宏观总结
- 【软考学习】设计模式——工厂方法
- 【软考学习】设计模式——抽象工厂
- CSDN图片显示不出解决方法!(新手之坑)
- IT业的风险投资
- 关于VB中的WithEvents关键字理解
- Hive notes
- 蓝桥杯java计算圆的面积
- 【软考学习】设计模式——装饰模式
- 工厂模式之简单工厂、工厂方法、抽象工厂分析对比
- Eclipse最常用快捷键
- python 3 笔记2
- Spring MVC 从实例读源码
- 20171216_C语言学习_MyfirstCode
- Hibernate主键生成celue
- 20171217
- C++ 基础知识