Purpose
Attaching additional responsibilities to an object dynamically. Decorators provide a flexible alternative to extending functionality (by extending subclass).
Client-specified embellishment of a core object by recursively wrapping it.
Wrapping a gift, putting it in a box, and wrapping the box.
Play with an Example
We know if we purchase a particular product. The product may have different licenses, Based on the license upgrade additional features are added on the product. But the features are adding into the core product.
Lets explain the same with windows 10 license.
Windows 10 having mainly 3 types of license are there Home, Premium, Enterprise.
Here if we check, Premium have all the features of home plus some additional features. Enterprise also having all the features of premium plus some additional features. In short the below image explains the actual meaning.
Lets create the program for the same
Here creating an interface Windows, which is a general interface for all the versions. The interface is mentioning for the common functionalities of all the versions. Here I just created a method called printFeatureList() which will print all the available features on the current version.
Next we can create a class for windows basic features. This class which contains the core features of windows. The class has been implemented with the printFeatureList() interface.
Next lets go with the Home version, which have all the features of basic version and plus some additional home specific features.
The premium version have all the features of home and premium specific features.
Last the enterprise version is the top most version, have all the features of windows.
Lets create an enterprise version instance. So that we created a new instance of enterprise version and passing premium version as parameter because it consist of premium version features. Also premium version having home version features, so passing home version instance as premium version parameter. Same way it is passing basic version to home version parameter
Points to Remember
- Ensure the context is: a single core (or non-optional) component, several optional embellishments or wrappers, and an interface that is common to all.
- Create a "Lowest Common Denominator" interface that makes all classes interchangeable.
- Create a second level base class (Decorator) to support the optional wrapper classes.
- The Core class and Decorator class inherit from the LCD interface.
- The Decorator class declares a composition relationship to the LCD interface, and this data member is initialized in its constructor.
- The Decorator class delegates to the LCD object.
- Define a Decorator derived class for each optional embellishment.
- Decorator derived classes implement their wrapper functionality - and - delegate to the Decorator base class.
- The client configures the type and ordering of Core and Decorator objects.
Advanced Points
- Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
- Adapter changes an object's interface, Decorator enhances an object's responsibilities. Decorator is thus more transparent to the client. As a consequence,
- Decorator supports recursive composition, which isn't possible with pure Adapters.
- Composite and Decorator have similar structure diagrams, reflecting the fact that both rely on recursive composition to organize an open-ended number of objects.
- A Decorator can be viewed as a degenerate Composite with only one component. However, a Decorator adds additional responsibilities - it isn't intended for object aggregation.
- Decorator is designed to let you add responsibilities to objects without subclassing. Composite's focus is not on embellishment but on representation. These intents are distinct but complementary. Consequently, Composite and Decorator are often used in concert.
- Composite could use Chain of Responsibility to let components access global properties through their parent. It could also use Decorator to override these properties on parts of the composition.
- Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.
- Decorator lets you change the skin of an object. Strategy lets you change the guts.
No comments:
Post a Comment