天天看点

C#设计模式——装饰者模式(Decorator Pattern)

一、例子

在软件开发中,我们往往会想要给某一类对象增加不同的功能。比如要给汽车增加ESP、天窗或者定速巡航。如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCar,ESPCCSCar……很容易就导致“子类爆炸”问题。

上述“子类爆炸”问题的根源在于该解决方案利用继承来扩展功能,缺乏灵活性。那么如何能在扩展功能的同时避免“子类爆炸”呢?

二、装饰者模式

装饰者模式装饰者模式可以动态地给一个对象增加一些额外的职责。就增加功能来说,装饰者模式相比生成子类更为灵活。

装饰者模式的结构图如下

C#设计模式——装饰者模式(Decorator Pattern)

Component定义了一个接口,可以利用Decorator为实现了这个接口的对象动态地增加职责

ConcreteComponent定义了一个实现了Component接口的对象

Decorator是抽象装饰者类,继承自Component接口,并包含有一个实现了Component接口的对象

ConcreteDecorator是具体装饰者类,用于为ConcreteComponent增加职责

在以上结构中,Component和Decorator可以情况省略。

三、实现

下面用装饰者模式来解决前面的例子

首先实现Car

C#设计模式——装饰者模式(Decorator Pattern)
C#设计模式——装饰者模式(Decorator Pattern)

接下来实现ConcreteDecorator

ESP装饰者

C#设计模式——装饰者模式(Decorator Pattern)
C#设计模式——装饰者模式(Decorator Pattern)

定速巡航装饰者

C#设计模式——装饰者模式(Decorator Pattern)
C#设计模式——装饰者模式(Decorator Pattern)

天窗装饰者

C#设计模式——装饰者模式(Decorator Pattern)
C#设计模式——装饰者模式(Decorator Pattern)

最后看一下如何使用装饰者来给Car增加功能

C#设计模式——装饰者模式(Decorator Pattern)
C#设计模式——装饰者模式(Decorator Pattern)