论文标题

装饰器应该保留组件界面吗?

Should Decorators Preserve the Component Interface?

论文作者

Niculescu, Virginia, Sterca, Adrian, Bufnea, Darius

论文摘要

装饰器设计模式是一种众所周知的模式,可以动态地附加到对象上的其他功能。已经提出了装饰器作为扩展功能的子类的灵活替代方案。尽管如此,装饰器模式仍存在一定的局限性,尤其是与以下事实相关的,即它以经典形式将其限制在单个接口上,该接口被我们打算装饰的混凝土组件的类型隐含地定义。与装饰器图案相关的另一个问题与装饰的线性组成有关,这可能会导致访问新添加的职责的问题。在这种情况下,本文介绍了装饰器模式的变体:混音器和d2代表,以及仅根据模板-HybridDecorator的C ++语言的特定于C ++语言的变体。混音仪可以被视为装饰器图案的新增强版本,可以消除装饰器图案的某些限制,但也可以用作一般扩展机构的基础。使用MixDecorator的主要优点是,它允许直接访问所有新添加的职责,因此,我们可以将不同的接口 - 反应性(新添加的公共方法)结合在一起,并直接与它们一起以任何顺序进行操作,从而隐藏了装饰的线性组成。 D2代理是基于双分配机制的变体。基于模板的C ++元编程机制允许装饰器的有趣混合变体 - 杂交器,该变体将按需定义的继承与组成混合在一起。使用这些装饰图案的这些变体,我们不再限于一个单个接口。可以放大可以发送到对象的消息集,因此,我们可以考虑使用它们,我们可以动态地更改对象的类型。

Decorator design pattern is a well known pattern that allows dynamical attachment of additional functionality to an object. Decorators have been proposed as flexible alternative to subclassing for extending functionality. Still, the Decorator pattern has certain limitations, especially related to the fact that in its classical form it is constrained to a single interface, which is implicitly defined by the type of the concrete components that we intend to decorate. Another problem associated to the Decorator pattern is related to the linear composition of the decorations, which could lead to problems in accessing the newly added responsibilities. In this context, the paper presents variants of the Decorator pattern: MixDecorator and D2Decorator, and a variant specific only to C++ language based on templates - HybridDecorator. MixDecorator could be considered a new enhanced version of the Decorator pattern that eliminates some constraints of the Decorator pattern, but also it could be used as a base of a general extension mechanism. The main advantage of using MixDecorator is that it allows direct access to all newly added responsibilities, and so, we may combine different interface-responsibilities (newly added public methods) and operate with them directly and in any order, hiding the linear composition of the decorations. D2Decorator is a variant based on a double-dispatch mechanism. The C++ metaprogramming mechanism based on templates allows an interesting hybrid variant of the Decorator - HybridDecorator, which mixes on-demand defined inheritance with composition. Using these variants of the Decorator pattern we are not longer limited to one single interface; the set of the messages that could be sent to an object could be enlarged, and so, we may consider that using them, we can dynamically change the type of objects.

扫码加入交流群

加入微信交流群

微信交流群二维码

扫码加入学术交流群,获取更多资源