设计模式笔记(四) —— 装饰模式
来源:互联网 发布:可牛闪图软件下载 编辑:程序博客网 时间:2024/06/16 02:03
装饰模式(Decorator):装饰模式是为已有功能动态的添加更多功能的一种方式。
- using System;
- namespace StuDesignMode.Decorator
- {
- public class AbsCustomer
- {
- public string Name
- {
- get;
- set;
- }
- public AbsCustomer() { }
- public AbsCustomer(string name)
- {
- this.Name = name;
- }
- public virtual void Show()
- {
- Console.WriteLine("装扮的{0}", this.Name);
- }
- }
- /// <summary>
- /// 人
- /// </summary>
- public class Person:AbsCustomer
- {
- public Person(string name)
- {
- base.Name = name;
- }
- }
- /// <summary>
- /// 猫
- /// </summary>
- public class Cat : AbsCustomer
- {
- public Cat(string name)
- {
- base.Name = name;
- }
- }
- /// <summary>
- /// 服饰
- /// </summary>
- class Finery :AbsCustomer
- {
- private AbsCustomer _component;
- /// <summary>
- /// 打扮
- /// </summary>
- /// <param name="component"></param>
- public void Decorate(AbsCustomer component)
- {
- this._component = component;
- }
- public override void Show()
- {
- if (this._component != null)
- {
- this._component.Show();
- }
- }
- }
- /// <summary>
- /// T恤
- /// </summary>
- class TShirts : Finery
- {
- public override void Show()
- {
- Console.Write("大T恤 ");
- base.Show();
- }
- }
- /// <summary>
- /// 垮裤
- /// </summary>
- class BigTrouser : Finery
- {
- public override void Show()
- {
- Console.Write("垮裤 ");
- base.Show();
- }
- }
- /// <summary>
- /// 破球鞋
- /// </summary>
- class BrokenShoes : Finery
- {
- public override void Show()
- {
- Console.Write("破球鞋 ");
- base.Show();
- }
- }
- /// <summary>
- /// 西装
- /// </summary>
- class Suit : Finery
- {
- public override void Show()
- {
- Console.Write("西装 ");
- base.Show();
- }
- }
- /// <summary>
- /// 领带
- /// </summary>
- class Tie : Finery
- {
- public override void Show()
- {
- Console.Write("领带 ");
- base.Show();
- }
- }
- /// <summary>
- /// 皮鞋
- /// </summary>
- class LeatherShoes : Finery
- {
- public override void Show()
- {
- Console.Write("皮鞋 ");
- base.Show();
- }
- }
- class ClientTest
- {
- static void Main(string[] args)
- {
- AbsCustomer xc = new Person("小菜");
- Console.WriteLine("/n第一种装扮:");
- TShirts ts = new TShirts();
- BigTrouser bt = new BigTrouser();
- BrokenShoes bs = new BrokenShoes();
- //装饰过程
- ts.Decorate(xc);
- bt.Decorate(ts);
- bs.Decorate(bt);
- bs.Show();
- Console.WriteLine("/n第二种装扮:");
- Suit xz = new Suit();
- Tie ld = new Tie();
- LeatherShoes px = new LeatherShoes();
- xz.Decorate(xc);
- ld.Decorate(xz);
- px.Decorate(ld);
- px.Show();
- //装扮花花
- AbsCustomer huahua = new Cat("小猫花花");
- ts.Decorate(huahua);
- bt.Decorate(ts);
- Console.WriteLine("/n小猫花花的装扮:");
- bt.Show();
- Console.WriteLine();
- }
- }
- }
- 设计模式笔记(四) —— 装饰模式
- 大话设计模式笔记(四)——装饰模式
- 大话设计模式学习(四)——装饰模式
- 设计模式系列(四)——装饰模式
- 设计模式笔记——装饰模式
- 大话设计模式(四)装饰模式
- 设计模式(四)装饰模式
- 设计模式之四——装饰模式
- 装饰模式——结构型设计模式之四
- 设计模式学习(四)——装饰者模式
- 设计模式—装饰模式(三)
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式之装饰模式(笔记)
- 扩展系统功能——装饰模式(四):透明与半透明装饰模式,装饰模式注意事项,装饰模式总结
- 学习指针不可少的好文章
- IFRAME自适应高度
- 四个故事让你终身受用
- 数据绑定控件收藏 DropDownList ListBox GridView DataList Repeater FormView DetailsView
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- 设计模式笔记(四) —— 装饰模式
- 视频格式转化为FLV (C#)
- 招聘.NET程序员
- 简单的客户端脚本为gridview“删除”列添加确认对话框
- 打包zip图片上传发帖
- 在.NET中调用Oracle9i存储过程经验总结
- DataSet,DataTable 不使用EXCEL组件直接保存为EXCEL
- 【已解决】Adobe CS3遇到问题需要关闭……
- 类型转换