0% found this document useful (0 votes)
19 views

Decorator: Created Tags

The Decorator pattern allows adding new behaviors to individual objects dynamically at runtime without affecting other objects of the same class. Decorators wrap the original component and provide additional responsibilities. The pattern has four main components: a Component interface, ConcreteComponents that implement the interface, Decorators that are wrapped around components, and ConcreteDecorators that add functionality. The example shows applying different Decorator classes like SeniorJavaDeveloper and JavaTeamLead to a JavaDeveloper to add extra responsibilities without subclassing.

Uploaded by

leonardo freitas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Decorator: Created Tags

The Decorator pattern allows adding new behaviors to individual objects dynamically at runtime without affecting other objects of the same class. Decorators wrap the original component and provide additional responsibilities. The pattern has four main components: a Component interface, ConcreteComponents that implement the interface, Decorators that are wrapped around components, and ConcreteDecorators that add functionality. The example shows applying different Decorator classes like SeniorJavaDeveloper and JavaTeamLead to a JavaDeveloper to add extra responsibilities without subclassing.

Uploaded by

leonardo freitas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Decorator

Created Oct 7, 2020 454 PM

Tags

Sometimes it is necessary to impose additional responsibilities on a separate


object rather than the whole class.

The Decorator is a structural pattern used to add new responsibilities to an


object dynamically without extending functionality.

Decorators are used for adding some behavior that is not part of the core
functionality to all interface methods. Decorator pattern perfectly suits for the
following tasks:

caching the work results;

measuring the execution time of methods;

user access control.

The decorator pattern has the following components:

Component is the interface for the objects that will get new responsibilities
from the decorators;

Concrete Component defines objects which implement the Component


interface and will get new responsibilities from the concrete decorators;

Decorator has reference to the Component and overridden component


methods;

Concrete Decorator extends Decorator class and adds new functions,


properties or state without creating new classes;

Decorator 1
Code

public interface Developer {

public String makeJob();


}

public class JavaDeveloper implements Developer {

public String makeJob() {


return "Write Java Code";
}
}

// describe the developer decorator to add functionality to our developers dynamically


public class DeveloperDecorator implements Developer {
private Developer developer;

public DeveloperDecorator(Developer developer) {


this.developer = developer;

Decorator 2
}

public String makeJob() {


return developer.makeJob();
}
}

// concrete decorator
public class SeniorJavaDeveloper extends DeveloperDecorator {

public SeniorJavaDeveloper(Developer developer) {


super(developer);
}

public String makeCodeReview() {


return "Make code review";
}

public String makeJob() {


return super.makeJob() + " " + makeCodeReview();
}
}

// concrete decorator
public class JavaTeamLead extends DeveloperDecorator {

public JavaTeamLead(Developer developer) {


super(developer);
}

public String sendWeekReport() {


return "Send week report to customers.";
}

public String makeJob() {


return super.makeJob() + " " + sendWeekReport();
}
}

// demo
public class Task {

public static void main(String[] args) {


Developer developer = new JavaTeamLead(
new SeniorJavaDeveloper(
new JavaDeveloper()));

System.out.println(developer.makeJob());
}
}

Decorator 3
Decorator 4

You might also like