Magazine
 

Design Pattern

 
Structural Patterns are design patterns, which describe the best possible ways to combine the objects and classes forming a larger complex structure in an easy manner. It deals with the objects delegating responsibilities to other objects. It is a simple way to design and realize relationships between the entities. 

In this article, we are discussing Decorator, Façade and Flyweight Design Patterns.

I. Decorator Design Pattern

This section deeply discusses the Decorator design pattern, a kind of structural pattern.

In object-oriented programming, the decorator pattern allows the developers to add new behaviors to an existing functionality of an object, dynamically.

The decorator pattern can be used whenever there is a need to add some additional functionality to an object or to the group of objects. It provides a flexible alternative for subclassing to extend the functionality. This pattern is also known as the “wrapper” design pattern as it works by wrapping around the original object to get a new “decorator” object. Wrapping is typically achieved by passing the original object as a parameter to the
constructor of the decorator, and then it is ready to implement the new functionality.

Decorators are very much similar to subclassing feature of an object-oriented programming. The only difference is that, Subclassing adds behavior at compile time where as a decoratorprovides a new behavior at the runtime.

The UML diagram for this pattern in our scenario looks something like this:
  This UML diagram illustrates that the Decorators should be abstract classes and the concrete implementation should be derived from them according to the requirement.

A decorator can be added or removed from an object without realizing the client about any changed occurred. It is a good idea to use a Decorator in a situation where you want to change the behavior of an object repeatedly (by adding and subtracting functionality) during runtime.

This dynamic behavior modification capability of the decorators are useful for adapting objects to new functionality without re-writing the original object’s code. 

The given design pattern shows the implementation code of the decorated class.In java, the code for a decorator class would be something like this:

 Nov 2007 | Java Jazz Up |37
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69   Download PDF