设计模式笔记(四) —— 装饰模式
2008-10-27 15:19
225 查看
装饰模式(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();
}
}
}
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();
}
}
}
相关文章推荐
- 设计模式学习笔记---装饰模式decorator(Java版)
- 步步为营 .NET 设计模式学习笔记 十四、Decorator(装饰模式)
- 步步为营 .NET 设计模式学习笔记 十四、Decorator(装饰模式)
- 设计模式 笔记 装饰模式 Decorator
- 黑马程序员 java学习笔记3-IO流之字节流与装饰设计模式
- 黑马程序员-Java学习笔记之设计模式(单例设计模式,装饰设计模式,模板设计模式)
- 5.[研磨设计模式笔记]装饰模式
- Java设计模式笔记(8)装饰模式
- java笔记-IO流-装饰设计模式、字节流
- 设计模式笔记:装饰模式
- 设计模式学习笔记-装饰模式
- java 设计模式学习笔记九 decorator装饰模式
- 设计模式笔记---装饰模式(2013.4.02)
- 设计模式 学习笔记 之 装饰模式 Decorator(6)
- 设计模式 笔记 装饰模式 Decorator
- 设计模式笔记之装饰模式---变继承关系为组合关系
- 设计模式学习笔记(三:装饰模式)
- 设计模式学习笔记之装饰器模式
- 咕泡-装饰器 decorator 设计模式笔记
- 设计模式学习笔记(2)之装饰模式(Decorator)