9.装饰者模式(Decorator Pattern)
- using System;
- namespace ConsoleApplication7
- {
- class Program
- {
- static void Main(string[] args)
- {
- // 我买了个苹果手机
- Phone phone = new ApplePhone();
- // 现在想贴膜了
- Decorator applePhoneWithSticker = new Sticker(phone);
- // 扩展贴膜行为
- applePhoneWithSticker.Print();
- Console.WriteLine("----------------------\n");
- // 现在我想有挂件了
- Decorator applePhoneWithAccessories = new Accessories(phone);
- // 扩展手机挂件行为
- applePhoneWithAccessories.Print();
- Console.WriteLine("----------------------\n");
- // 现在我同时有贴膜和手机挂件了
- Sticker sticker = new Sticker(phone);
- Accessories applePhoneWithAccessoriesAndSticker = new Accessories(sticker);
- applePhoneWithAccessoriesAndSticker.Print();
- Console.ReadLine();
- }
- }
- /// <summary>
- /// 手机抽象类,即装饰者模式中的抽象组件类
- /// </summary>
- public abstract class Phone
- {
- public abstract void Print();
- }
- /// <summary>
- /// 苹果手机,即装饰着模式中的具体组件类
- /// </summary>
- public class ApplePhone : Phone
- {
- /// <summary>
- /// 重写基类方法
- /// </summary>
- public override void Print()
- {
- Console.WriteLine("开始执行具体的对象——苹果手机");
- }
- }
- /// <summary>
- /// 装饰抽象类,要让装饰完全取代抽象组件,所以必须继承自Photo
- /// </summary>
- public abstract class Decorator : Phone
- {
- private Phone phone;
- public Decorator(Phone p)
- {
- this.phone = p;
- }
- public override void Print()
- {
- if (phone != null)
- {
- phone.Print();
- }
- }
- }
- /// <summary>
- /// 贴膜,即具体装饰者
- /// </summary>
- public class Sticker : Decorator
- {
- public Sticker(Phone p)
- : base(p)
- {
- }
- public override void Print()
- {
- base.Print();
- // 添加新的行为
- AddSticker();
- }
- /// <summary>
- /// 新的行为方法
- /// </summary>
- public void AddSticker()
- {
- Console.WriteLine("现在苹果手机有贴膜了");
- }
- }
- /// <summary>
- /// 手机挂件
- /// </summary>
- public class Accessories : Decorator
- {
- public Accessories(Phone p)
- : base(p)
- {
- }
- public override void Print()
- {
- base.Print();
- // 添加新的行为
- AddAccessories();
- }
- /// <summary>
- /// 新的行为方法
- /// </summary>
- public void AddAccessories()
- {
- Console.WriteLine("现在苹果手机有漂亮的挂件了");
- }
- }
- }
9.装饰者模式(Decorator Pattern)的更多相关文章
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法
装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...
- 设计模式 - 装饰者模式(Decorator Pattern) 具体解释
装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
- C#设计模式之装饰者模式(Decorator Pattern)
1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- 七个结构模式之装饰者模式(Decorator Pattern)
定义: 使用组合的方法,动态给一个类增加一些额外的功能,避免因为使用子类继承而导致类继承结构复杂.并且可以保持和被装饰者同一个抽象接口,从而使客户端透明. 结构图: Component:抽象构件类,定 ...
- C#设计模式——装饰者模式(Decorator Pattern)
一.例子在软件开发中,我们往往会想要给某一类对象增加不同的功能.比如要给汽车增加ESP.天窗或者定速巡航.如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCa ...
随机推荐
- 深入解析MySQL分区(Partition)功能
自5.1开始对分区(Partition)有支持 = 水平分区(根据列属性按行分)= 举个简单例子:一个包含十年发票记录的表可以被分区为十个不同的分区,每个分区包含的是其中一年的记录. === 水平分区 ...
- WAF绕过的技巧
研究过国内外的waf.分享一些绝技. 一些大家都了解的技巧如:/*!*/,SELECT[0x09,0x0A-0x0D,0x20,0xA0]xx FROM 不再重新提及. 以下以Mysql为例讲述这些技 ...
- 只能输入汉字js脚本
<html> <head> <meta http-equiv="Content-Type" content="text/html" ...
- 基于.NET的大型Web站点StackOverflow架构分析(转)
Stack Overflow网址:http://stackoverflow.com/ 当前访问量:每月9500PV(每天300多万PV) 当前Alexa排名:149 所用.NET技术:C#.Visua ...
- win7打开网页老是提示下载网页解决办法
方法:我的系统是windows 7 旗舰版, 解决方法可以自己手动去修复,方法是进入命令窗口. 开始--运行--cmd--sfc.exe--sfc/scannow 修复一下!
- ios框架中UIResponder的职责链设计模式应用
今天有空,就把UIResponder的职责链图上传一下 如果不理解职责链设计模式的朋友,请参考:http://www.cnblogs.com/langtianya/p/4060941.html
- Log4j使用教程 log4:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
1.Logger类 通过Logger类的静态方法Logger.getRootLogger得到RootLogger.所有其他的loggers是通过静态方法Logger.getLogger来实例化并获取的 ...
- [整理]Code::Blocks使用遇到的问题
在使用其编写C小程序的过程总会遇到些问题,特整理如下: 1.无法调试 注意的是项目所在的文件路径不能包含中文. 2.头文件接口函数申明引用无效 查看头文件是否处于可编译状态,左侧项目文件列表里是文件名 ...
- Linux大神必备-文本编辑器
导读 我们在 Linux 上不缺乏非常现代化的编辑软件,但是它们都是基于 GUI(图形界面)的编辑软件.正如你所了解的:Linux 真正的魅力在于命令行,当你正在用命令行工作时,你就需要一个可以在控制 ...
- Coursera台大机器学习课程笔记11 -- Nonlinear Transformation
这一节讲的是如何将线性不可分的情况转为非线性可分以及转换的代价.特征转换是机器学习的重点. 最后得出重要的结论是,在做转换时,先从简单模型,再到复杂模型. 参考:http://www.cnblogs. ...