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/index.md
+63Lines changed: 63 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,69 @@ title: Design Patterns
22
22
23
23
Before diving into design patterns, it’s important to understand the **SOLID Principles**, **Tightly Coupled Classes**, and **Loosely Coupled Classes**. Additionally, some tips and tricks for writing clean code.
24
24
25
+
---
26
+
## Tightly Coupled and Loosely Coupled Classes
27
+
28
+
In software design, **coupling** refers to the degree of dependency between two classes or modules. It determines how closely one class is connected to another.
29
+
### 1. Tightly Coupled Classes
30
+
**Definition:** These classes are highly dependent on each other. Changes in one class often require changes in the other, making the system rigid and harder to maintain. These classes violates the **Single Responsibility Principle** which is the part of the SOLID Principle.
31
+
32
+
**Example :**
33
+
Consider a `MessageService` class that sends messages via Email, SMS, or other techniques. The implementation might look like the following code:
34
+
```java title:MessageService.java
35
+
classMessageService {
36
+
37
+
publicstaticvoidsendMessage(stringmessage){
38
+
// Tightly Coupled on the email only
39
+
EmailClient emailclient =newEmailClient();
40
+
emailclient.sendEmail(message);
41
+
}
42
+
43
+
}
44
+
```
45
+
In this code, you can see that the `sendMessage` method sends the message exclusively via Email. The `EmailClient` class is tightly coupled with this method. If you need to send messages using other providers (e.g., SMS or other techniques), you would have to create separate services for each provider. This approach is inefficient and leads to code repetition.
46
+
### 2. Loosely Coupled Classes
47
+
**Definition:** These classes have minimal dependency on each other. They interact through interfaces or abstractions, making the system flexible, maintainable, and reusable.
48
+
These Approach Follows **Dependency Inversion Principle** which is again the part of the SOLID Principle.
49
+
50
+
**Example:**
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.
52
+
```java title:MessageService.java
53
+
54
+
// Interface to be implemented by various provider services
55
+
interfaceProviderClient {
56
+
voidsend(Stringmessage, Stringto);
57
+
}
58
+
59
+
// Email Service implementing the ProviderClient interface
60
+
publicclassEmailClientimplementsProviderClient {
61
+
publicvoidsend(Stringmessage, Stringto) {
62
+
// Code to send an email
63
+
}
64
+
}
65
+
66
+
// SMS Service implementing the ProviderClient interface
0 commit comments