设计模式示例二 Decorator(装饰者)

示例名称:超级递送员
示例说明:定义基类(BaseDelivery)和虚方法(Deliver),定义继承自该基类的三个子类(MilkDelivery、NewspaperDelivery和CarDelivery),重写基类的虚方法(Deliver)。
示例类图:
设计模式示例二 Decorator(装饰者)
关键部分说明

BaseDelivery
设计模式示例二 Decorator(装饰者)usingSystem;
设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
namespaceDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
publicclassBaseDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
publicvirtualvoidDeliver()
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)Console.WriteLine(
"我是早晨的送递员");
设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}
Deliver是虚方法,子类需要重写它。

MilkDelivery、NewspaperDelivery和CarDelivery
设计模式示例二 Decorator(装饰者)usingSystem;
设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
namespaceDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
publicclassMilkDelivery:BaseDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
privateBaseDelivery_delivery;
设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
publicMilkDelivery(BaseDeliverydelivery)
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)_delivery
=delivery;
设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
publicoverridevoidDeliver()
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)_delivery.Deliver();
设计模式示例二 Decorator(装饰者)Console.WriteLine(
"我送来牛奶!");
设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
usingSystem;
设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
namespaceDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
publicclassNewspaperDelivery:BaseDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
privateBaseDelivery_delivery;
设计模式示例二 Decorator(装饰者)
publicNewspaperDelivery(BaseDeliverydelivery)
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)_delivery
=delivery;
设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)
publicoverridevoidDeliver()
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)_delivery.Deliver();
设计模式示例二 Decorator(装饰者)Console.WriteLine(
"我送来了报纸!");
设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
usingSystem;
设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
namespaceDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
publicclassCarDelivery:BaseDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
privateBaseDelivery_delivery;
设计模式示例二 Decorator(装饰者)
publicCarDelivery(BaseDeliverydelivery)
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)_delivery
=delivery;
设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)
publicoverridevoidDeliver()
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)_delivery.Deliver();
设计模式示例二 Decorator(装饰者)Console.WriteLine(
"我还送来了你买的小汽车,梦想成真了吧,哈哈!");
设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)

这三个类分别继承了BaseDelivery(is a的关系),含有一个BaseDelivery类型的私有字段和一个BaseDelivery参数的构造方法,重写父类BaseDelivery的Deliver方法。

应用
设计模式示例二 Decorator(装饰者)usingSystem;
设计模式示例二 Decorator(装饰者)
usingSystem.Collections.Generic;
设计模式示例二 Decorator(装饰者)
usingSystem.Text;
设计模式示例二 Decorator(装饰者)
设计模式示例二 Decorator(装饰者)
namespaceDelivery
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
classProgram
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)
staticvoidMain(string[]args)
设计模式示例二 Decorator(装饰者)设计模式示例二 Decorator(装饰者)
...{
设计模式示例二 Decorator(装饰者)BaseDeliverybaseDelivery
=newBaseDelivery();
设计模式示例二 Decorator(装饰者)MilkDeliverymilkDelivery
=newMilkDelivery(baseDelivery);
设计模式示例二 Decorator(装饰者)NewspaperDeliverynewsDelivery
=newNewspaperDelivery(milkDelivery);
设计模式示例二 Decorator(装饰者)CarDeliverycarDelivery
=newCarDelivery(newsDelivery);
设计模式示例二 Decorator(装饰者)carDelivery.Deliver();
设计模式示例二 Decorator(装饰者)Console.Read();
设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}

设计模式示例二 Decorator(装饰者)}
任何继承了BaseDelivery的子类都可以做为其它子类的构造函数的参数传递。即可实现对该子类的装饰(Decorator)。

运行结果
设计模式示例二 Decorator(装饰者)

现在是不是很开心!