decorator pattern java real time example

Decorator Pattern:

it is one of the structural design patterns. In this post, we will elaborate on the decorator pattern and importance. the primary purpose of this pattern is used to add additional functionalities to object during run time. you might think of inheritance to add additional functionalities to the existing class.

class Calc

{

     add(){

      }

}

Class Calc2 extends Calc

{

     sub()

     {

      }

}

if you look at the above two classes, the first class is having only a method. the second class contains an additional function which is sub() method. this is one way we can add functionalities to object. the application will be more complex when we have many subclasses in our system. subclasses can add functionalities but in turn making it more complicated.  The decorator pattern has come as a hero to overcome these problems. the main principle of this pattern is can’t modify the existing but we extend them. and one more important thing about this pattern, we will not add functionalities to the whole class but to a specific object.

to achieve this we need to enclose components in the other object. the enclosing object is called a decorator. we can add or detach those decorators. it will not disturb the code.

advantages of following decorator pattern:

  1. add additional functionalities to a specific object during run time.
  2. more flexible than inheritance.
  3. adding and removing additional functionalities will be easier.
  4. we can add more code incrementally
  5. code readability.

java real time example for decorator design pattern :

 

package decorator;

abstract class Component {
abstract void doJob();
}

class ConcreteComponent extends Component {

@Override
void doJob() {
System.out.println("concrete component with actual fucntionalities");
System.out.println("closed for modifications");

}

}

abstract class AbstractDecorator extends Component {
protected Component com;

public void setComponent(Component com) {
this.com = com;
}

public void doJob() {
if (com != null) {
com.doJob();
}
}
}

class ConcreteDecorator extends AbstractDecorator {

public void doJob() {
super.doJob();
System.out.println("additional functionalities");
System.out.println("write code here for enhancements to current object");
}
}


package decorator;

public class DecortatorPatternDemo {
public static void main(String[] args) {
Component com=new ConcreteComponent();
com.doJob();
ConcreteDecorator decorator=new ConcreteDecorator();
decorator.setComponent(com);
decorator.doJob();

}

}

expected output:

concrete component with actual fucntionalities
closed for modifications
concrete component with actual fucntionalities
closed for modifications
additional functionalities
write code here for enhancements to current object

Hope you all are having a good understanding of the decorator design pattern. Thanks 🙂

 

 

 


Posted

in

by

Tags:

Comments

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started