You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/Decorator Pattern.md
+10-15
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
title: Decorator Pattern
3
3
tags:
4
4
- structural
5
+
created: 2025-02-22
5
6
---
6
7
## Definition
7
8
@@ -80,16 +81,14 @@ _Design of the Coffee Shop_
80
81
---
81
82
## Coding Decorator Pattern
82
83
83
-
```java title:Coffee.java
84
-
// Coffee.java
84
+
```java title="Coffee.java"
85
85
interfaceCoffee {
86
86
publicStringgetDescription();
87
87
publicintgetCost();
88
88
}
89
89
```
90
90
The `Coffee` interface acts as an **abstraction layer** between the **variety of coffee types**. By implementing this interface, you can create **as many coffee varieties** as needed.
91
-
```java title:PlainCoffee.java
92
-
// PlainCoffee.java
91
+
```java title="PlainCoffee.java"
93
92
classPlainCoffeeimplementsCoffee {
94
93
95
94
@Override
@@ -104,8 +103,7 @@ class PlainCoffee implements Coffee {
104
103
}
105
104
```
106
105
107
-
```java title:CappuccinoCoffee.java
108
-
// CappuccinoCoffee
106
+
```java title="CappuccinoCoffee.java"
109
107
classCappuccinoCoffeeimplementsCoffee {
110
108
@Override
111
109
publicStringgetDescription() {
@@ -119,8 +117,7 @@ class CappuccinoCoffee implements Coffee {
119
117
}
120
118
```
121
119
In the code example above, the `Coffee` interface is implemented by both the **PlainCoffee** and **CappuccinoCoffee** concrete classes. For each coffee type, we override the methods and assign the **specific cost** for each coffee.
122
-
```java title:CoffeeDecorator.java
123
-
// CoffeeDecorator.java
120
+
```java title="CoffeeDecorator.java"
124
121
abstractclassCoffeeDecoratorimplementsCoffee {
125
122
protectedCoffee _coffee;
126
123
@@ -140,8 +137,7 @@ abstract class CoffeeDecorator implements Coffee {
140
137
}
141
138
```
142
139
The `CoffeeDecorator` class is the **decorator class** that implements the `Coffee` interface to **decorate different varieties of coffee**. In our case, the **CoffeeDecorator** acts as an **add-on** (e.g., milk, sugar) that can be added to the customer's coffee if requested.
143
-
```java title:MilkDecorator.java
144
-
// MilkDecorator.java
140
+
```java title="MilkDecorator.java"
145
141
classMilkDecoratorextendsCoffeeDecorator {
146
142
publicMilkDecorator(Coffeedecoratedcoffee) {
147
143
super(decoratedcoffee);
@@ -159,8 +155,7 @@ class MilkDecorator extends CoffeeDecorator {
159
155
}
160
156
```
161
157
162
-
```java title:SugarDecorator.java
163
-
// SugarDecorator.java
158
+
```java title="SugarDecorator.java"
164
159
classSugarDecoratorextendsCoffeeDecorator {
165
160
publicSugarDecorator(CoffeedecoratedCoffee) {
166
161
super(decoratedCoffee);
@@ -178,7 +173,7 @@ class SugarDecorator extends CoffeeDecorator {
178
173
}
179
174
```
180
175
When creating coffee objects, you can **layer decorators** as needed. For example, a **Cappuccino** with **milk and sugar** is created by **wrapping the decorators** around the base coffee object.
@@ -307,7 +302,7 @@ public class DecoratorPattern {
307
302
308
303
The Java I/O library is a textbook example of the Decorator Pattern. The base classes such as `InputStream` and `OutputStream` are extended by concrete classes (like `FileInputStream`) and then wrapped by decorator classes that add additional functionality at runtime.
Copy file name to clipboardExpand all lines: content/Strategy Pattern.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,8 @@
2
2
title: Strategy Pattern
3
3
tags:
4
4
- behavioral
5
+
created: 2025-02-11
5
6
---
6
-
7
7
## Definition
8
8
9
9
It defines a family of algorithms, encapsulates each one, and makes them interchangeable. The **Strategy Pattern** allows algorithms to vary independently from the clients that use them.
@@ -284,7 +284,7 @@ classDiagram
284
284
285
285
The `Comparator` interface acts as the Strategy Pattern. By inheriting this interface, you can create your custom comparator, which allows you to sort collections.
Copy file name to clipboardExpand all lines: content/index.md
+3-2
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
---
2
2
title: Design Patterns
3
+
created: 2025-02-07
3
4
---
4
5
### What is Design Pattern ?
5
6
@@ -30,7 +31,7 @@ In software design, **coupling** refers to the degree of dependency between tw
30
31
31
32
**Example :**
32
33
Consider a `MessageService` class that sends messages via Email, SMS, or other techniques. The implementation might look like the following code:
33
-
```java title:MessageService.java
34
+
```java title="MessageService.java"
34
35
classMessageService {
35
36
36
37
publicstaticvoidsendMessage(stringmessage){
@@ -48,7 +49,7 @@ These Approach Follows **Dependency Inversion Principle** which is again the par
48
49
49
50
**Example:**
50
51
In the earlier example, the `MessageService` class was heavily dependent on `EmailClient`. In such cases, if you need to create another service, it becomes inefficient. We can avoid this dependency by passing the `ProviderClient` interface as a parameter to the `sendMessage` method. Then, we implement the `ProviderClient` interface for various provider services.
51
-
```java title:MessageService.java
52
+
```java title="MessageService.java"
52
53
53
54
// Interface to be implemented by various provider services
0 commit comments