装饰(Decorator)模式
装饰模式应当在什么情况下使用
引用:http://www.cnblogs.com/singlepine/archive/2005/10/30/265023.html
在以下情况下应当使用装饰模式:
- 需要扩展一个类的功能,或给一个类增加附加责任。
- 需要动态地给一个对象增加功能,这些功能可以再动态地撤销。
- 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变得不现实。
以下是程杰的大话设计模式当中的例子,比较容易理解:using System;
using System.Collections.Generic;
using System.Text;namespace 装饰模式
{
class Program
{
static void Main(string[] args)
{
Person xc = new Person("小菜");Console.WriteLine("\n第一种装扮:");
Sneakers pqx = new Sneakers();
BigTrouser kk = new BigTrouser();
TShirts dtx = new TShirts();pqx.Decorate(xc);
kk.Decorate(pqx);
dtx.Decorate(kk);
dtx.Show();Console.WriteLine("\n第二种装扮:");
LeatherShoes px = new LeatherShoes();
Tie ld = new Tie();
Suit xz = new Suit();px.Decorate(xc);
ld.Decorate(px);
xz.Decorate(ld);
xz.Show();Console.WriteLine("\n第三种装扮:");
Sneakers pqx2 = new Sneakers();
LeatherShoes px2 = new LeatherShoes();
BigTrouser kk2 = new BigTrouser();
Tie ld2 = new Tie();pqx2.Decorate(xc);
px2.Decorate(pqx);
kk2.Decorate(px2);
ld2.Decorate(kk2);ld2.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);
}
}class Finery : Person
{
protected Person component;//打扮
public void Decorate(Person component)
{
this.component = component;
}public override void Show()
{
if (component != null)
{
component.Show();
}
}
}
class TShirts : Finery
{
public override void Show()
{
Console.Write("大T恤 ");
base.Show();
}
}class BigTrouser : Finery
{
public override void Show()
{
Console.Write("垮裤 ");
base.Show();
}
}class Sneakers : Finery
{
public override void Show()
{
Console.Write("破球鞋 ");
base.Show();
}
}class Suit : Finery
{
public override void Show()
{
Console.Write("西装 ");
base.Show();
}
}class Tie : Finery
{
public override void Show()
{
Console.Write("领带 ");
base.Show();
}
}class LeatherShoes : Finery
{
public override void Show()
{
Console.Write("皮鞋 ");
base.Show();
}
}
}
浙公网安备 33010602011771号