Decorate Design Pattern
Javed Mulla
(Immediate Joiner, Valid Work Visa for UK)Senior Software Engineer at Consors Finanz BNP Paribas
WHY required ?
Let's assume you are looking for a girlfriend. You want a girl with different qualities. Girls who looking good, with a normal family, a literated..etc. They may have different personalities and hobbies. In a dating web like sadi.com, if I create the girls with individual qualities than I want to create thousand of classes. That is a serious problem called class explosion. Moreover, this design is not extensible. Whenever there is a new girl with different quantity is created, a new class needs to be created which is very bad as per programmer perspective.So how to resolve the problem..? We want to decorate our Girlfriend.
Generally, We use inheritance or composition to extend the behavior of an object but this is done at compile time and its applicable to all the instances of the class. We can’t add any new functionality or remove any existing behavior at runtime – this is when Decorator pattern comes into picture.
Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviours
Decorator pattern allows a user to add new functionality to an existing object (means girls) without altering its structure(change in Girl class). This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.
This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.
Decorator design pattern is used to modify the functionality of an object at runtime.
Traditionally, you might consider subclassing to be the best way to approach this - but there will be cases that subclassing isn't possible, or is impractical. This leads us to the Open/Closed Principle: classes should be open for extension, but closed for modification. This is a good principle to keep in mind, as it keeps your class stable, but leaves it open for extension if someone wants to add behaviour.
When to use Decorator pattern in Java
- When sub classing is become impractical and we need large number of different possibilities to make independent object or we can say we have number of combination for an object.
- Secondly when we want to add functionality to individual object not to all object at run-time we use decorator design pattern.
Advantage of Decorator design Pattern in Java
In brief we see what the main advantages of using decorator design patterns are.
- Decorator Pattern is flexible than inheritance because inheritance add responsibilities at compile time and it will add at run-time.
- Decorator pattern enhance or modify the object functionality
Disadvantage
Main disadvantage of using Decorator Pattern in Java is that the code maintenance can be a problem as it provides a lot of similar kind of small objects (each decorator).
That’s all on decorator design pattern in Java. To get mastery on decorator pattern I suggest looking inside JDK library itself and finding what classes are decorated, why they are decorated. Also think of scenario where inheritance is impractical and you look more flexibility and try to use decorator pattern in Java there.
Java IO API and wrapper classes follow decorated design pattern.
Sr Software Engineer at GivePulse, Inc.
8 å¹´nice
Engineering Lead @ Bny Mellon (Ex Deutsche Bank | Morgan Stanley | Atos)
8 å¹´Great post javed.