C# 装饰模式

 

 

 

 

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();

}
}

 

//TShirt类

class TShirts : Finery
{
public override void Show()
{
Console.Write("大T恤");
base.Show();
}
}

//领带类

class Tie : Finery
{
public override void Show()
{
Console.Write("领带");
base.Show();
}
}

//西装类

class Suit : Finery
{
public override void Show()
{
Console.Write("西装");
base.Show();
}
}

//破球鞋类

class Sneakers : Finery
{
public override void Show()
{
Console.Write("破球鞋");
base.Show();
}
}

 

//皮鞋类

class LeatherShoes : Finery
{
public override void Show()
{
Console.Write("皮鞋");
base.Show();
}
}

 

//垮裤类

class BigTrouser : Finery
{
public override void Show()
{
Console.Write("垮裤");
base.Show();
}
}

 

/// <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();
}
}
}

 

//Person类

class Person
{
public Person()
{ }

private string name;
public Person(string name)
{
this.name = name;
}

public virtual void Show()
{
Console.WriteLine("装扮的{0}",name);
}
}

posted @ 2022-04-02 10:02  KevinSteven  阅读(49)  评论(0编辑  收藏  举报