Skip to content

Commit e043978

Browse files
committed
add Decorator
1 parent 3f85030 commit e043978

10 files changed

+162
-0
lines changed

src/structural/decorator/Banana.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package structural.decorator;
2+
3+
public class Banana extends WaffleDecorator {
4+
public Banana(Waffle waffle) {
5+
super(waffle);
6+
}
7+
8+
public String getDescription() {
9+
return decoratedWaffle.getDescription() + ", Banana";
10+
}
11+
12+
public double getCost() {
13+
return decoratedWaffle.getCost() + 1.0;
14+
}
15+
}

src/structural/decorator/Main.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package structural.decorator;
2+
3+
public class Main {
4+
public static void main(String[] args) throws InterruptedException {
5+
6+
// my favorite one
7+
Waffle waffle1 = new RegularWaffle();
8+
waffle1 = new Banana(waffle1);
9+
waffle1 = new Strawberry(waffle1);
10+
waffle1 = new PeanutButter(waffle1);
11+
System.out.println(waffle1.getDescription());
12+
System.out.println("Total: $" + waffle1.getCost());
13+
System.out.println();
14+
System.out.flush();
15+
Thread.sleep(2000);
16+
17+
Waffle waffle2 = new RegularWaffle();
18+
waffle2 = new Banana(waffle2);
19+
waffle2 = new MapleSyrup(waffle2);
20+
waffle2 = new Nutella(waffle2);
21+
System.out.println(waffle2.getDescription());
22+
System.out.println("Total: $" + waffle2.getCost());
23+
System.out.println();
24+
System.out.flush();
25+
Thread.sleep(2000);
26+
}
27+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package structural.decorator;
2+
3+
public class MapleSyrup extends WaffleDecorator {
4+
public MapleSyrup(Waffle waffle) {
5+
super(waffle);
6+
}
7+
8+
public String getDescription() {
9+
return decoratedWaffle.getDescription() + ", Maple Syrup";
10+
}
11+
12+
public double getCost() {
13+
return decoratedWaffle.getCost() + 0.8;
14+
}
15+
}

src/structural/decorator/Nutella.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package structural.decorator;
2+
3+
public class Nutella extends WaffleDecorator {
4+
public Nutella(Waffle waffle) {
5+
super(waffle);
6+
}
7+
8+
public String getDescription() {
9+
return decoratedWaffle.getDescription() + ", Nutella";
10+
}
11+
12+
public double getCost() {
13+
return decoratedWaffle.getCost() + 1.5;
14+
}
15+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package structural.decorator;
2+
3+
public class PeanutButter extends WaffleDecorator {
4+
public PeanutButter(Waffle waffle) {
5+
super(waffle);
6+
}
7+
8+
public String getDescription() {
9+
return decoratedWaffle.getDescription() + ", Peanut Butter";
10+
}
11+
12+
public double getCost() {
13+
return decoratedWaffle.getCost() + 1.3;
14+
}
15+
}

src/structural/decorator/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[< Back To Design Patterns](../../../)
2+
3+
# Decorator Design Pattern
4+
### Please support my repo with your star.
5+
6+
## Definition
7+
The Decorator Design Pattern is a structural pattern that allows behavior to be added to individual objects, either statically or dynamically, without affecting the behavior of other objects from the same class. It provides a flexible alternative to subclassing for extending functionality, by wrapping an object with a series of decorator classes that implement the same interface.
8+
9+
## Example in Real World/Nature Inspired
10+
1. **Gift Wrapping:** A gift can be wrapped with decorative layers (boxes, ribbons, paper) that enhance its appearance without altering the actual gift inside.
11+
2. **Clothing Layers:** A person can wear a base outfit and then add layers like jackets, scarves, or hats — each adding functionality like warmth or style without changing the person.
12+
3. **Tree Decorations:** A Christmas tree is decorated with lights, ornaments, and garlands. Each added decoration enhances the tree’s look, and they can be combined in various ways.
13+
4. **Makeup or Accessories:** Just as accessories or makeup enhance someone’s appearance or performance for a specific event, decorators enhance an object’s behavior for specific contexts.
14+
15+
## Real Usage/System Design Problems
16+
1. **Extending UI Elements:** In GUI frameworks, decorators are used to add scrollbars, borders, or drop shadows to UI components without changing the base class.
17+
2. **Adding Responsibilities in Logging:** A logging system may decorate messages with timestamps, tags, or formatting styles in a composable way.
18+
3. **Stream I/O Systems:** In Java I/O, classes like BufferedInputStream, DataInputStream, etc., decorate base input streams to add buffering or data-type handling without modifying the original stream.
19+
20+
## Important Points of Implementation
21+
- **Same Interface:** Both the core object and decorators implement the same interface, allowing decorators to be nested and treated uniformly.
22+
- **Composition Over Inheritance:** Functionality is extended through object composition rather than deep inheritance chains, promoting more flexible designs.
23+
- **Order Matters:** The order in which decorators are applied can affect the final behavior, so careful composition is needed.
24+
- **Transparent Wrapping:** Clients using the final decorated object should not need to know whether it has been wrapped — interaction remains consistent.
25+
26+
[< Back To Design Patterns](../../../)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package structural.decorator;
2+
3+
public class RegularWaffle implements Waffle {
4+
public String getDescription() {
5+
return "Regular Waffle";
6+
}
7+
8+
public double getCost() {
9+
return 3.0;
10+
}
11+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package structural.decorator;
2+
3+
public class Strawberry extends WaffleDecorator {
4+
public Strawberry(Waffle waffle) {
5+
super(waffle);
6+
}
7+
8+
public String getDescription() {
9+
return decoratedWaffle.getDescription() + ", Strawberry";
10+
}
11+
12+
public double getCost() {
13+
return decoratedWaffle.getCost() + 1.2;
14+
}
15+
}

src/structural/decorator/Waffle.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package structural.decorator;
2+
3+
public interface Waffle {
4+
String getDescription();
5+
double getCost();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package structural.decorator;
2+
3+
public abstract class WaffleDecorator implements Waffle {
4+
protected Waffle decoratedWaffle;
5+
6+
public WaffleDecorator(Waffle waffle) {
7+
this.decoratedWaffle = waffle;
8+
}
9+
10+
public String getDescription() {
11+
return decoratedWaffle.getDescription();
12+
}
13+
14+
public double getCost() {
15+
return decoratedWaffle.getCost();
16+
}
17+
}

0 commit comments

Comments
 (0)