Should Decorators Preserve the Component Interface?

Preprint, arXiv:2009.06414 [cs.SE], September 2020.

Cite as

Full paper

Should Decorators Preserve the Component Interface?

Authors

Virginia Niculescu*, Adrian Sterca*, Darius Bufnea*
* Department of Computer Science, Faculty of Mathematics and Computer Science, Babeș-Bolyai University of Cluj-Napoca, Romania

Abstract
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.

Key words

OOP; design patterns; decorator; interface; responsibility; extensibility.

BibTeX bib file

mixdecorator2020.bib

References

  • Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object Oriented Software. Addison Wesley, Oct 1994.
  • Alan Shalloway and James R. Trott. Design Patterns Explained: A New Perspective on Object Oriented Design, 2nd Edition. Addison Wesley, 2004.
  • Martin Büchi and Wolfgang Weck. Generic wrappers. In Elisa Bertino, editor, ECOOP 2000 — Object-Oriented Programming, pages 201–225, Berlin, Heidelberg, 2000. Springer Berlin Heidelberg.
  • Günter Kniesel, Tobias Rho, and Stefan Hanenberg. Evolvable pattern implementations need generic aspects. In RAM-SE, 2004.
  • Aminata Sabané, Yann-Gaël Guéhéneuc, Venera Arnaoudova, and Giuliano Antoniol. Fragile base-class problem, problem? Empirical Software Engineering, 22:2612–2657, 2016.
  • Joshua Bloch. Effective Java, 3rd Edition. Addison-Wesley Professional, 2017.
  • Virginia Niculescu. Mixdecorator: An enhanced version of decorator pattern. In Proceedings of the 20th European Conference on Languages of Programs, EuroPLoP ’15, pages 36:1–36:12, New York, USA, 2015. ACM.
  • Oracle. Java SE 8: Implementing default methods in interfaces. [online: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JavaSE8DefaultMethods/JavaSE8DefaultMethods.html], 2018. Accessed: July 20, 2020.
  • Microsoft. What’s new in C# 8.0. [online: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8], 2020. Accessed: July 20, 2020.
  • Kotlin. Kotlin – extension methods. [online: https://kotlinlang.org/docs/reference/extensions.html], 2020. Accessed: August 07, 2020.
  • Viviana Bono, Enrico Mensa, and Marco Naddeo. Trait-oriented programming in Java 8. In Proceedings of the 2014 International Conference on Principles and Practices of Programming on the Java Platform: Virtual Machines, Languages, and Tools, PPPJ ’14, pages 181–186, New York, NY, USA, 2014. ACM.
  • Microsoft. Extension methods (C# programming guide). [online: https://msdn.microsoft.com/en-us/library/bb383977.aspx], 2015. Accessed: February 14, 2020.
  • Andrei Alexandrescu. Modern C++ design: generic programming and design patterns applied. Addison-Wesley, 2001.
  • David Abrahams and Aleksey Gurtovoy. C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley, 2003.
  • Y. Smaragdakis and D. Batory. Mixin-based programming in C++. In Proceedings of the Second International Symposium on Generative and Component-Based Software Engineering, 2000.
  • Gilad Bracha and William Cook. Mixin-based inheritance. In Proceedings of the European Conference on Object-oriented Programming on Object-oriented Programming Systems, Languages, and Applications, OOPSLA/ECOOP ’90, pages 303–311, New York, NY, USA, 1990. ACM.
  • Virginia Niculescu. Efficient decorator pattern variants through C++ policies. In Proceedings of the 15th International Conference on Evaluation of Novel Approaches to Software Engineering – Volume 1: ENASE,, pages 281–288. INSTICC, SciTePress, 2020.
  • Nathanael Schärli, Stéphane Ducasse, Oscar Nierstrasz, and Andrew P. Black. Traits: Composable units of behaviour. In ECOOP, 2002.
  • Scala. Scala documentation: Traits. [online: http://docs.scala-lang.org/tour/traits.html], 2015. Accessed: July 20, 2020.

Darius Bufnea