Abstract
Suppose you’re working with a class your colleague wrote, and you want to extend that class’ functionality. How would you do it, without modifying the original code? Well, one approach is inheritance: you make a derived class, add the functionality you need, maybe even override something, and you’re good to go.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Strictly speaking, it is possible to store state in extension methods, albeit in a very roundabout way. Essentially, what you’d do is have your extension class keep a static member of type Dictionary<WeakReference, Dictionary<string,object>> and then modify the entries in this dictionary to map an object to its set of properties. Plenty of fiddling is required here, both in terms of working with weak references (we don’t want this store to extend the lifetime of the original object, right?) and the boxing and unboxing that comes with storing a bunch of objects.
- 2.
Mixin inheritance is a C++ technique for adding functionality to classes by using inheritance. In the context of the decorator, it would allow us to compose a class of type T<U<V>> that would inherit from both U and V, giving us access to all the underlying members. Also, constructors would work correctly thanks to constructor forwarding and C++’s variadic templates.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2020 Dmitri Nesteruk
About this chapter
Cite this chapter
Nesteruk, D. (2020). Decorator. In: Design Patterns in .NET Core 3. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-6180-4_10
Download citation
DOI: https://doi.org/10.1007/978-1-4842-6180-4_10
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-6179-8
Online ISBN: 978-1-4842-6180-4
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)