装饰模式(Decorator):可以将方法附加到另一个对象当中也就是给另一个对象附加了一个功能,最后统一执行。
实例代码:
class Program
{
static void Main(string[] args)
{
conPerson c = new conPerson();
red r = new red();
blue b = new blue();
r.setP(c);
b.setP(r);
b.show();
Console.ReadLine();
}
}
abstract class person
{
public abstract void show();
}
class conPerson : person
{
public override void show()
{
Console.WriteLine("conPerson");
}
}
class chuang:person
{
public person p;
public void setP(person p)
{
this.p = p;
}
public override void show()
{
if (p != null)
{
p.show();
}
}
}
class red : chuang
{
public override void show()
{
base.show();
Console.WriteLine("red");
}
}
class blue : chuang
{
public override void show()
{
base.show();
Console.WriteLine("blue");
}
}