装饰模式【大话设计模式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();
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步