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

Lecture 11 - Decorator Design Pattern

The document discusses the decorator design pattern. It describes what the decorator pattern is, its advantages, usage, UML diagram, and provides an example using a Christmas tree.

Uploaded by

Iffat Nowshin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 11 - Decorator Design Pattern

The document discusses the decorator design pattern. It describes what the decorator pattern is, its advantages, usage, UML diagram, and provides an example using a Christmas tree.

Uploaded by

Iffat Nowshin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Lecture 11

Kazi Rifat Ahmed


Lecturer
Department of Software Engineering
Daffodil International University
Decorator Design Pattern
● What is Decorator Pattern
● Advantage of Decorator Pattern
Outline ●

Usage of Decorator Pattern
UML of Decorator Pattern
● Example of Adapter Pattern
Decorator Design Pattern
● Decorator patterns allow a user to add new functionality to an existing object without
altering its structure. So, there is no change to the original class.
● The decorator design pattern is a structural pattern, which provides a wrapper to the
existing class.
● The decorator design pattern uses abstract classes or interfaces with the composition
to implement the wrapper.
Decorator Design Pattern
● Decorator design patterns create decorator classes, which wrap the original class and
supply additional functionality by keeping the class methods’ signature unchanged.
● Decorator design patterns are most frequently used for applying single responsibility
principles since we divide the functionality into classes with unique areas of concern.
● The decorator design pattern is structurally almost like the chain of responsibility
pattern.
Advantage of Decorator Design Pattern
○ It provides greater flexibility than static inheritance.
○ It enhances the extensibility of the object, because changes are made by coding new
classes.
○ It simplifies the coding by allowing you to develop a series of functionality from
targeted classes instead of coding all of the behavior into the object.
Usage of Decorator Design Pattern
It is used:
○ When you want to transparently and dynamically add responsibilities to objects
without affecting other objects.
○ When you want to add responsibilities to an object that you may want to change in
future.
○ Extending functionality by sub-classing is no longer practical.
UML of Decorator Design Pattern
Example of Decorator Design Pattern
First, we’ll create a ChristmasTree interface and its implementation:
public interface ChristmasTree {
String decorate();
}
Example of Decorator Design Pattern
The implementation of this interface will look like:
public class ChristmasTreeImpl implements ChristmasTree {

@Override
public String decorate() {
return "Christmas tree";
}
}
Example of Decorator Design Pattern
We’ll now create an abstract TreeDecorator class for this tree. This decorator will implement the ChristmasTree
interface as well as hold the same object. The implemented method from the same interface will simply call the
decorate() method from our interface:

public abstract class TreeDecorator implements ChristmasTree {


private ChristmasTree tree;

// standard constructors
@Override
public String decorate() {
return tree.decorate();
}
}
Example of Decorator Design Pattern
We’ll now create some decorating element. These decorators will extend our abstract TreeDecorator class and will modify its
decorate() method according to our requirement:

public class BubbleLights extends TreeDecorator {

public BubbleLights(ChristmasTree tree) {


super(tree);
}

public String decorate() {


return super.decorate() + decorateWithBubbleLights();
}

private String decorateWithBubbleLights() {


return " with Bubble Lights";
}
}
Example of Decorator Design Pattern
For this case, the following is true:
@Test
public void whenDecoratorsInjectedAtRuntime_thenConfigSuccess() {
ChristmasTree tree1 = new Garland(new ChristmasTreeImpl());
assertEquals(tree1.decorate(),
"Christmas tree with Garland");

ChristmasTree tree2 = new BubbleLights(


new Garland(new Garland(new ChristmasTreeImpl())));
assertEquals(tree2.decorate(),
"Christmas tree with Garland with Garland with Bubble Lights");
}

Note that in the first tree1 object, we’re only decorating it with only one Garland, while the other tree2 object we’re
decorating with one BubbleLights and two Garlands. This pattern gives us this flexibility to add as many decorators as we
want at runtime.
References
1. https://baeldung.com/java-decorator-pattern
2. https://www.geeksforgeeks.org/decorator-design-pattern-in-java-with-example/
3. https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm
4. https://www.javatpoint.com/decorator-pattern
Thank You

You might also like