装饰者模式

using System;
using System.Collections.Generic;
using System.Text;

//装饰者模式
namespace designMode
{
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.WriteLine(
"大T恤");
base.Show();
}
}
}

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

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

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

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

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

}

  

using System;
using System.Collections.Generic;
using System.Text;

namespace designMode
{
class Program
{
static void Main(string[] args)
{

//装饰者模式
Person xc = new Person("小菜");
Console.WriteLine(
"\n第一张装扮:");

WearSneakers pqx
= new WearSneakers();
BigTrouser kk
= new BigTrouser();
TShirts dtx
= new TShirts();

pqx.Decorate(xc);
kk.Decorate(pqx);
dtx.Decorate(kk);
dtx.Show();

Console.WriteLine(
"\n第二种装扮:");

WearLeatherShoes px
= new WearLeatherShoes();
WearTie ld
= new WearTie();
WearSuit xz
= new WearSuit();

px.Decorate(xc);
ld.Decorate(px);
xz.Decorate(ld);
xz.Show();

Console.Read();
}
}
}

  

转载于:https://www.cnblogs.com/cjingzm/archive/2011/08/01/2124395.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值