装饰模式【大话设计模式Demo】
代码
class DecoratePattern
{
static void Main(string[] args)
{
Person person = new Person("小菜");
Sneaker ydx = new Sneaker();
BigTrouser kk = new BigTrouser();
TShirts dtx = new TShirts();
ydx.Decorate(person);
kk.Decorate(ydx);
dtx.Decorate(kk);
dtx.Show();
Console.Read();
}
}
class Person
{
public Person()
{ }
private string Name;
public Person(string name)
{
this.Name = name;
}
public virtual void Show()
{
Console.WriteLine("装扮的{0}", Name);
}
}
/// <summary>
/// 穿衣服的人
/// </summary>
class Finery : Person
{
protected Person component;
//打扮
public void Decorate(Person _component)
{
this.component = _component;
}
public override void Show()
{
if (component != null)
{
component.Show();
}
}
}
/// <summary>
/// 穿T恤的人
/// </summary>
class TShirts : Finery
{
public override void Show()
{
Console.WriteLine("大T恤");
base.Show();
}
}
/// <summary>
/// 穿裤子的人
/// </summary>
class BigTrouser : Finery
{
public override void Show()
{
Console.WriteLine("跨裤");
base.Show();
}
}
/// <summary>
/// 穿运动的人
/// </summary>
class Sneaker : Finery
{
public override void Show()
{
Console.WriteLine("跨裤");
base.Show();
}
}