装饰者模式的学习(c#)
案例转自https://www.cnblogs.com/stonefeng/p/5679638.html
//主体基类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorModeDemo
{
abstract class PanCake
{
public string desc = "";
public abstract string getDesc();
public abstract double price();
}
}
//主体:肉夹馍
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorModeDemo
{
class Roujiamo:PanCake
{
public Roujiamo()
{ this.desc = "肉夹馍"; }
public override string getDesc()
{
return desc;
}
public override double price()
{
return 6;
}
}
}
//主体手抓饼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorModeDemo
{
class TornCake:PanCake
{
public TornCake()
{
this.desc = "手抓饼";
}
public override string getDesc()
{
return desc;
}
public override double price()
{
return 4;
}
}
}
//装饰者基类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorModeDemo
{
abstract class Condiment:PanCake
{
public PanCake panCake;
public Condiment(PanCake p)
{
panCake = p;
}
}
}
//装饰者煎蛋
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorModeDemo
{
class FiredEgg:Condiment
{
public FiredEgg(PanCake pancake):base(pancake)
{
}
public override string getDesc()
{
return panCake.getDesc()+",煎蛋";
}
public override double price()
{
return this.panCake.price() + 2;
}
}
}
//主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecoratorModeDemo
{
class Program
{
static void Main(string[] args)
{
TornCake torncake = new TornCake();
Console.WriteLine(torncake.getDesc() + " ¥" +torncake.price().ToString());
PanCake roujiamo = new Roujiamo();
roujiamo = new FiredEgg(roujiamo);
roujiamo = new FiredEgg(roujiamo);
Console.WriteLine(roujiamo.getDesc()+" ¥"+roujiamo.price());
Console.ReadLine();
}
}
}
运行结果

心得:装饰者派生自主体基类,继承了主体需要操作的对象。同时装饰者的成员对象中有主体,用以耦合主体。通过创建不同的装饰者派生类,重写主体类方法来实现对主体类的不同操作。
roujiamo = new FiredEgg(roujiamo)实际上roujiamo已经变成了煎蛋,只不过这个煎蛋的成员包含肉夹馍,返回的方法经重写已经用原肉夹馍处理过了。
装饰者模式的学习(c#)的更多相关文章
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
装饰者模式的学习(c#) 案例转自https://www.cnblogs.com/stonefeng/p/5679638.html //主体基类 using System;using System.C ...
- Head First设计模式——装饰者模式
前言:对于设计模式我们有时候在想是否有必要,因为实际开发中我们没有那么多闲工夫去套用这么多设计模式,也没有必要为了模式而模式. 通常这些模式会引入新的抽象层,增加代码的复杂度,但是当我们掌握了这些设计 ...
- 重学 Java 设计模式:实战装饰器模式(SSO单点登录功能扩展,增加拦截用户访问方法范围场景)
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 对于代码你有编程感觉吗 很多人写代码往往是没有编程感觉的,也就是除了可以把功能按照固 ...
- 【设计模式】Java设计模式 - 装饰者模式
Java设计模式 - 装饰者模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起记录分享自 ...
- Python装饰器模式学习总结
装饰器模式,重点在于装饰.装饰的核心仍旧是被装饰对象. 类比于Java编程的时候的包装模式,是同样的道理.虽然概念上稍有不同但是原理上还是比较相近的.下面我就来谈一谈我对Python的装饰器的学习的一 ...
- 《Head First 设计模式》学习笔记——观察者模式 + 装饰者模式
装饰者模式是JDK中还有一个使用较多的设计模式,上一个是观察者模式(在Swing中大量使用),业内好的API设计无一离不开常见的设计模式,通常我们所说要阅读源代码,也是为了学习大牛们的设计思路.--- ...
- 设计模式学习之装饰者模式(Decorator,结构型模式)(16)
参考地址:http://www.cnblogs.com/zhili/p/DecoratorPattern.html 一.定义:装饰者模式以对客户透明的方式动态地给一个对象附加上更多的责任,装饰者模式相 ...
- javascript设计模式学习之十五——装饰者模式
一.装饰者模式定义 装饰者模式可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.这种为对象动态添加职责的方式就称为装饰者模式.装饰者对象和它所装饰的对象拥有一致的接口,对于用 ...
- 学习笔记——装饰器模式Decorator
装饰器模式,最典型的例子. 工厂新开了流水线,生产了手机外壳,蓝天白云花色.刚准备出厂,客户说还要印奶牛在上面,WTF…… 时间上来不及,成本也不允许销毁了重来,怎么办?弄来一机器A,专门在蓝天白云的 ...
随机推荐
- PHP内存溢出Allowed memory size of 解决办法
PHP内存溢出Allowed memory size of 解决办法 博客分类: php ============================Allowed memory size of x ...
- Android-Java-Thread线程两种方式的使用场景
Thread线程两种方式的优点/缺点 extends Thread 方式: 缺点:存在耦合度(因为线程任务run方法里面的业务逻辑 和 线程启动耦合了) 缺点:Cat extends Thread { ...
- CentOS 7配置nginx-1.13.10支持http/2和Server Push
0.确保openssl版本大于1.0.2 openssl version 1.下载nginx-1.13.10 wget http://nginx.org/download/nginx-1.13.10. ...
- C# 开源仪表盘库—Agauge App
1.简介 有个叫A.J.Bauer 的大神在System.Windows.Forms.Control类的基础上建立了一个显示各种仪表盘的类. 英文版简介:C# Tu ...
- 杀掉所有 skynet 进程
ps aux | grep skynet | awk '/config/{print $2}' | xargs kill
- 暴破助攻提权:ruadmin
i春秋作家:yangyangwithgnu 1 缘由 千辛万苦拿下的 webshell 不是 www-data 用户就是 networkservice 权限,要想拓展攻击面.扩大战果,提权,是必经之路 ...
- Linux巩固记录(5) hadoop 2.7.4下自己编译代码并运行MapReduce程序
程序代码为 ~\hadoop-2.7.4\share\hadoop\mapreduce\sources\hadoop-mapreduce-examples-2.7.4-sources\org\apac ...
- Jmeter之分布式执行测试
一. 安装Java 1.1下载JDK 1) Windows安装jdk,下载完成后,双击安装 2) Linux解压:tar -zxvf jdk-8u74-linux-x64.gz 1.2 Java环境变 ...
- 02-04:springboot 访问静态资源
1.SpringBoot从classpath/static的目录下:(目录名称必须叫static,可以理解为根目录为static) 2.servletContext根目录下,进行查找: 在src/ma ...
- 一口一口吃掉Hexo(三)
如果你想得到更好的阅读效果,请访问我的个人网站 ,版权所有,未经许可不得转载! 相信通过前一节的学习,你已经在你的本地部署好了你的网站,那么接下来就让你的朋友们通过网络访问你的网站吧!通过这一节你将免 ...