装饰模式
动态的给对象添加职责
Decorator.PNG
用例: 给一个人添加装饰
代码语言:javascript复制class Program
{
static void Main(string[] args)
{
Trainee trainee = new Trainee("cxk");
Finery finery0 = new Student();
finery0.SetPerson(trainee);
Finery finery1 = new BasketBall();
finery1.SetPerson(finery0);
finery1.Show();
}
}
abstract class Person
{
public abstract void Show();
}
class Trainee : Person
{
protected string name;
public Trainee(string name)
{
this.name = name;
}
public override void Show()
{
Console.WriteLine("我是" name "n");
}
}
abstract class Finery : Person
{
protected Person person;
public void SetPerson(Person person)
{
this.person = person;
}
public override void Show()
{
if(person != null)
{
person.Show();
}
}
}
class Student : Finery
{
public override void Show()
{
base.Show();
Console.WriteLine("I am student" "n");
}
}
class BasketBall : Finery
{
public override void Show()
{
base.Show();
Console.WriteLine("I am BasketBaller" "n");
}
}