Skip to content

Commit 5f40128

Browse files
committed
added decorator pattern
1 parent 9a4562f commit 5f40128

File tree

3 files changed

+112
-6
lines changed

3 files changed

+112
-6
lines changed

.obsidian/workspace.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
"state": {
2828
"type": "markdown",
2929
"state": {
30-
"file": "README.md",
30+
"file": "Design Patterns/Decorator Pattern.md",
3131
"mode": "source",
3232
"source": false
3333
},
3434
"icon": "lucide-file",
35-
"title": "README"
35+
"title": "Decorator Pattern"
3636
}
3737
}
3838
],
@@ -199,8 +199,8 @@
199199
"Design Patterns/Design Principles.md",
200200
"Design Patterns/Decorator Pattern.md",
201201
"Design Patterns/Observer Pattern.md",
202-
"System Design/0. Introduction/0.1 Systems Development Life Cycle (SDLC).md",
203202
"README.md",
203+
"System Design/0. Introduction/0.1 Systems Development Life Cycle (SDLC).md",
204204
"LICENSE",
205205
"System.md",
206206
"System Design.md",

Design Patterns/Decorator Pattern.md

+105
Original file line numberDiff line numberDiff line change
@@ -1 +1,106 @@
11

2+
# Definition
3+
4+
## The "formal" definition
5+
6+
> The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for extending functionality.
7+
8+
9+
10+
# How does it work
11+
12+
```java
13+
// Coffee.java
14+
public interface Coffee {
15+
String getDescription();
16+
double getCost();
17+
}
18+
19+
// PlainCoffee.java
20+
public class PlainCoffee implements Coffee {
21+
@Override
22+
public String getDescription() {
23+
return "Plain Coffee";
24+
}
25+
26+
@Override
27+
public double getCost() {
28+
return 2.0;
29+
}
30+
}
31+
32+
// CoffeeDecorator.java
33+
public abstract class CoffeeDecorator implements Coffee {
34+
protected Coffee decoratedCoffee;
35+
36+
public CoffeeDecorator(Coffee decoratedCoffee) {
37+
this.decoratedCoffee = decoratedCoffee;
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return decoratedCoffee.getDescription();
43+
}
44+
45+
@Override
46+
public double getCost() {
47+
return decoratedCoffee.getCost();
48+
}
49+
}
50+
51+
// MilkDecorator.java
52+
public class MilkDecorator extends CoffeeDecorator {
53+
public MilkDecorator(Coffee decoratedCoffee) {
54+
super(decoratedCoffee);
55+
}
56+
57+
@Override
58+
public String getDescription() {
59+
return decoratedCoffee.getDescription() + ", Milk";
60+
}
61+
62+
@Override
63+
public double getCost() {
64+
return decoratedCoffee.getCost() + 0.5;
65+
}
66+
}
67+
68+
// SugarDecorator.java
69+
public class SugarDecorator extends CoffeeDecorator {
70+
public SugarDecorator(Coffee decoratedCoffee) {
71+
super(decoratedCoffee);
72+
}
73+
74+
@Override
75+
public String getDescription() {
76+
return decoratedCoffee.getDescription() + ", Sugar";
77+
}
78+
79+
@Override
80+
public double getCost() {
81+
return decoratedCoffee.getCost() + 0.2;
82+
}
83+
}
84+
85+
// Main.java
86+
public class Main {
87+
public static void main(String[] args) {
88+
// Plain Coffee
89+
Coffee coffee = new PlainCoffee();
90+
System.out.println("Description: " + coffee.getDescription());
91+
System.out.println("Cost: $" + coffee.getCost());
92+
93+
// Coffee with Milk
94+
Coffee milkCoffee = new MilkDecorator(new PlainCoffee());
95+
System.out.println("\nDescription: " + milkCoffee.getDescription());
96+
System.out.println("Cost: $" + milkCoffee.getCost());
97+
98+
// Coffee with Sugar and Milk
99+
Coffee sugarMilkCoffee = new SugarDecorator(new MilkDecorator(new PlainCoffee()));
100+
System.out.println("\nDescription: " + sugarMilkCoffee.getDescription());
101+
System.out.println("Cost: $" + sugarMilkCoffee.getCost());
102+
}
103+
}
104+
```
105+
106+
As you can see, for each additional property that can be in a cup of coffee, we create a decorator class. This decorator class can alter other attributes of the main object (like `cost` and `description`). Using this pattern will able us to create extensions and removes the need to alter the original class.

Design Patterns/Design Principles.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
1. Identify the aspects of your app that vary and separate them from what stays the same.
3-
2. Program to an interface, not an implementation.
4-
3. Classes should be open for extensions, but closed for modifications.
5-
1.
3+
2. Favor composition over inheritance.
4+
3. Program to an interface, not an implementation.
5+
4. Loosely coupled designs allow us to build flexible object oriented systems that can handle change because they minimize the inter-dependency between objects.
6+
5. Classes should be open for extensions, but closed for modifications.

0 commit comments

Comments
 (0)