Sharon Brand
Sharon Brand
Decorator at NDP Group
The decorator pattern are often wont to extend (decorate) the practicality of a particular object statically, or in some cases at run-time, severally of different instances of an equivalent category, provided some groundwork is finished at style time. this can be achieved by planning a brand new decorator category that wraps the first category. This wrapping can be achieved by the subsequent sequence of steps:
Subclass the first "Component" category into a "Decorator" category (see UML diagram);
In the Decorator category, add a element pointer as a field;
Pass a element to the Decorator builder to initialize the element pointer;
In the Decorator category, direct all "Component" ways to the "Component" pointer; and
In the ConcreteDecorator category, override any element method(s) whose behavior has to be changed.
This pattern is meant so multiple decorators are often stacked on high of every different, on every occasion adding a brand new practicality to the overridden method(s).
Note that decorators and also the original category object share a typical set of options. within the previous diagram, the "operation()" technique was on the market in each the adorned and plain versions.
The decoration options (e.g., methods, properties, or different members) area unit typically outlined by AN interface, mixin (a.k.a. "trait") or category inheritance that is shared by the decorators and also the adorned object. within the previous example the category "Component" is genetic by each the "ConcreteComponent" and also the subclasses that descend from "Decorator".
The decorator pattern is another to subclassing. Subclassing adds behavior at compile time, and also the modification affects all instances of the first class; decorating will give new behavior at run-time for individual objects.
This distinction becomes most significant once there area unit many freelance ways in which of extending practicality. In some object-oriented programming languages, categories can't be created at runtime, and it's generally inconceivable to predict, at style time, what mixtures of extensions are going to be required. this could mean that a brand new category would got to be created for each attainable combination. against this, decorators area unit objects, created at runtime, and might be combined on a per-use basis. The I/O Streams implementations of each Java and also the .NET Framew