当前位置: X-MOL 学术arXiv.cs.SE › 论文详情
Our official English website, www.x-mol.net, welcomes your feedback! (Note: you will need to create a separate account there.)
Should Decorators Preserve the Component Interface?
arXiv - CS - Software Engineering Pub Date : 2020-09-14 , DOI: arxiv-2009.06414
Virginia Niculescu, Adrian Sterca, Darius Bufnea

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.

中文翻译:

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

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