Skip to content

Commit f52ff18

Browse files
Notes and Layout updated. In notes tightly coupled and loosely coupled concept added
1 parent c46e925 commit f52ff18

File tree

4 files changed

+123
-8
lines changed

4 files changed

+123
-8
lines changed

content/index.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,69 @@ title: Design Patterns
2222
2323
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.
2424

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+
class MessageService {
36+
37+
public static void sendMessage(string message){
38+
// Tightly Coupled on the email only
39+
EmailClient emailclient = new EmailClient();
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+
interface ProviderClient {
56+
void send(String message, String to);
57+
}
58+
59+
// Email Service implementing the ProviderClient interface
60+
public class EmailClient implements ProviderClient {
61+
public void send(String message, String to) {
62+
// Code to send an email
63+
}
64+
}
65+
66+
// SMS Service implementing the ProviderClient interface
67+
public class SMSClient implements ProviderClient {
68+
public void send(String message, String to) {
69+
// Code to send an SMS
70+
}
71+
}
72+
73+
// MessageService class using ProviderClient
74+
class MessageService {
75+
public static void sendMessage(ProviderClient client, String message, String to) {
76+
client.send(message, to);
77+
}
78+
}
79+
80+
```
81+
Now, you can add as many providers as needed, and `MessageService` will continue to work without being tightly coupled to `EmailClient`.
82+
83+
---
84+
## SOLID Principle
85+
86+
87+
2588
---
2689
## Contents:
2790

0 commit comments

Comments
 (0)