arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2009.06414cs.SE
收藏

装饰者应该保留组件接口吗?

Should Decorators Preserve the Component Interface?

Virginia Niculescu, Adrian Sterca, Darius Bufnea

摘要:

装饰器设计模式是一种众所周知的模式,它允许动态地将附加功能附加到对象上。装饰器已经被提议作为子类化的灵活替代方案来扩展功能。尽管如此,Decorator模式还是有一定的局限性,特别是在它的经典形式中,它被限制在一个接口上,这个接口是由我们想要装饰的具体组件的类型隐式定义的。与Decorator模式相关的另一个问题与装饰的线性组合有关,这可能导致访问新添加的职责时出现问题。在此背景下,本文介绍了装饰器模式的变体:MixDecorator和D2 Decorator,以及一个仅针对基于模板的C++语言的变体- HybridDecorator。MixDecorator可以被认为是Decorator模式的一个新的增强版本,它消除了Decorator模式的一些限制,但它也可以用作通用扩展机制的基础。使用MixDecorator的主要优点是,它允许直接存取所有新添加的责任,因此,我们可以组合不同的接口责任(新添加的公共方法),并直接以任何顺序操作它们,隐藏装饰的线性组合。D2 Decorator是一个基于双重分派机制的变体。基于模板的C++元编程机制允许一个有趣的装饰器混合变体- HybridDecorator,它混合了按需定义的继承和组合。使用装饰者模式的这些变体,我们不再局限于一个单一的接口;可以发送给对象的消息集可以扩大,因此,我们可以考虑使用它们,我们可以动态地改变对象的类型。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑