Decorate Design Pattern

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.

  1.      Decorator Pattern is flexible than inheritance because inheritance add responsibilities at compile time and it will add at run-time.
  2.      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.

Nabilahmed Patel

Sr Software Engineer at GivePulse, Inc.

8 å¹´

nice

赞
回复
Sandeep Upadhyay

Engineering Lead @ Bny Mellon (Ex Deutsche Bank | Morgan Stanley | Atos)

8 å¹´

Great post javed.

赞
回复

要查看或添加评论,请登录

Javed Mulla的更多文章

  • Dynamic Programming

    Dynamic Programming

    Dynamic Programming: DP is an algorithm's technique which is usually based on a formula executed recently and one…

    2 条评论
  • JUnit

    JUnit

    JUnit Docs JUnit is a Regression Testing Framework used by developers to implement unit testing in Java. (Adv)That is…

  • Performance

    Performance

    p1

  • Servlet Life Cycle

    Servlet Life Cycle

    Servlet Life Cycle Describe the purpose and event sequence of the servlet life cycle: (1) servlet class loading, (2)…

    1 条评论
  • JSF life cycle

    JSF life cycle

    JSF life cycle

  • Servlet to Servlet communication & Threading

    Servlet to Servlet communication & Threading

    Servlet

  • Spring core

    Spring core

    IOC is heart of Spring framework. We are divided spring application into three logical parts.

  • DI and IOC

    DI and IOC

    DI and IOC Using the DI is to way of getting the decoupling in java. Using the DI we are not create/instantiate the…

    1 条评论
  • Logical Question 3

    Logical Question 3

    you have a set of integers between 1 ..

    3 条评论
  • Logical Question 2

    Logical Question 2

    n the party everyone do handshake with the one another. So total 66 handshake are done.

社区洞察

其他会员也浏览了