设计模式示例二 Decorator(装饰者)
示例名称:超级递送员
示例说明:定义基类(BaseDelivery)和虚方法(Deliver),定义继承自该基类的三个子类(MilkDelivery、NewspaperDelivery和CarDelivery),重写基类的虚方法(Deliver)。
示例类图:
关键部分说明
BaseDelivery
示例说明:定义基类(BaseDelivery)和虚方法(Deliver),定义继承自该基类的三个子类(MilkDelivery、NewspaperDelivery和CarDelivery),重写基类的虚方法(Deliver)。
示例类图:
关键部分说明
BaseDelivery
usingSystem;
namespaceDelivery
...{
publicclassBaseDelivery
...{
publicvirtualvoidDeliver()
...{
Console.WriteLine("我是早晨的送递员");
}
}
}
namespaceDelivery
...{
publicclassBaseDelivery
...{
publicvirtualvoidDeliver()
...{
Console.WriteLine("我是早晨的送递员");
}
}
}
Deliver是虚方法,子类需要重写它。
MilkDelivery、NewspaperDelivery和CarDelivery
MilkDelivery、NewspaperDelivery和CarDelivery
usingSystem;
namespaceDelivery
...{
publicclassMilkDelivery:BaseDelivery
...{
privateBaseDelivery_delivery;
publicMilkDelivery(BaseDeliverydelivery)
...{
_delivery=delivery;
}
publicoverridevoidDeliver()
...{
_delivery.Deliver();
Console.WriteLine("我送来牛奶!");
}
}
}
usingSystem;
namespaceDelivery
...{
publicclassNewspaperDelivery:BaseDelivery
...{
privateBaseDelivery_delivery;
publicNewspaperDelivery(BaseDeliverydelivery)
...{
_delivery=delivery;
}
publicoverridevoidDeliver()
...{
_delivery.Deliver();
Console.WriteLine("我送来了报纸!");
}
}
}
usingSystem;
namespaceDelivery
...{
publicclassCarDelivery:BaseDelivery
...{
privateBaseDelivery_delivery;
publicCarDelivery(BaseDeliverydelivery)
...{
_delivery=delivery;
}
publicoverridevoidDeliver()
...{
_delivery.Deliver();
Console.WriteLine("我还送来了你买的小汽车,梦想成真了吧,哈哈!");
}
}
}
namespaceDelivery
...{
publicclassMilkDelivery:BaseDelivery
...{
privateBaseDelivery_delivery;
publicMilkDelivery(BaseDeliverydelivery)
...{
_delivery=delivery;
}
publicoverridevoidDeliver()
...{
_delivery.Deliver();
Console.WriteLine("我送来牛奶!");
}
}
}
usingSystem;
namespaceDelivery
...{
publicclassNewspaperDelivery:BaseDelivery
...{
privateBaseDelivery_delivery;
publicNewspaperDelivery(BaseDeliverydelivery)
...{
_delivery=delivery;
}
publicoverridevoidDeliver()
...{
_delivery.Deliver();
Console.WriteLine("我送来了报纸!");
}
}
}
usingSystem;
namespaceDelivery
...{
publicclassCarDelivery:BaseDelivery
...{
privateBaseDelivery_delivery;
publicCarDelivery(BaseDeliverydelivery)
...{
_delivery=delivery;
}
publicoverridevoidDeliver()
...{
_delivery.Deliver();
Console.WriteLine("我还送来了你买的小汽车,梦想成真了吧,哈哈!");
}
}
}
这三个类分别继承了BaseDelivery(is a的关系),含有一个BaseDelivery类型的私有字段和一个BaseDelivery参数的构造方法,重写父类BaseDelivery的Deliver方法。
应用
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceDelivery
...{
classProgram
...{
staticvoidMain(string[]args)
...{
BaseDeliverybaseDelivery=newBaseDelivery();
MilkDeliverymilkDelivery=newMilkDelivery(baseDelivery);
NewspaperDeliverynewsDelivery=newNewspaperDelivery(milkDelivery);
CarDeliverycarDelivery=newCarDelivery(newsDelivery);
carDelivery.Deliver();
Console.Read();
}
}
}
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceDelivery
...{
classProgram
...{
staticvoidMain(string[]args)
...{
BaseDeliverybaseDelivery=newBaseDelivery();
MilkDeliverymilkDelivery=newMilkDelivery(baseDelivery);
NewspaperDeliverynewsDelivery=newNewspaperDelivery(milkDelivery);
CarDeliverycarDelivery=newCarDelivery(newsDelivery);
carDelivery.Deliver();
Console.Read();
}
}
}
任何继承了BaseDelivery的子类都可以做为其它子类的构造函数的参数传递。即可实现对该子类的装饰(Decorator)。
运行结果
现在是不是很开心!
运行结果
现在是不是很开心!