Grokking SOLID Design Principles

0% completed

Coupling and its Relation to the Single Responsibility Principle (SRP)

What is Coupling?

General Example: Tightly Connected Devices vs. Independent Devices

High Coupling in Code: One Class Doing Too Much

Reducing Coupling: Separate Database Logic from Student Class

Coupling and SRP

What is Coupling?

Coupling refers to how closely connected different modules, classes, or methods are to each other.

In software design, we aim for low coupling, meaning that classes and components should be as independent from each other as possible. High coupling, on the other hand, means that classes are tightly dependent on one another, making it harder to change or reuse parts of the system.

General Example: Tightly Connected Devices vs. Independent Devices

Let’s think of a general example. Imagine you have a TV and remote that only work together. The remote can only control this specific TV, and the TV can’t be operated by any other remote. This represents high coupling because the two devices are tightly connected, and you can't easily swap one out without the other.

Image

Now, imagine you have a universal remote that can control multiple devices, and a TV that works with any remote. This is an example of low coupling, where the devices are more independent, flexible, and interchangeable.

High Coupling in Code: One Class Doing Too Much

Let’s start with an example where the Student class is responsible for both student information and database operations. This creates high coupling because the Student class is tightly dependent on the database functionality.

Python3
Python3
. . . .

Here, the Student class handles both the student’s information (like getStudentId and setStudentId) and the database interaction (save() method). This creates high coupling, as any change to the database logic will require changes to the Student class as well.

Reducing Coupling: Separate Database Logic from Student Class

To improve the code and reduce coupling, we can move the database-related logic into a separate class, say DatabaseManager. The Student class will then focus on handling student data, and the database operations will be managed by the DatabaseManager class. This leads to low coupling and follows the Single Responsibility Principle (SRP).

Step 1: Create a Separate Class for Database Operations

Python3
Python3
. . . .

Step 2: Modify the Student Class to Use DatabaseManager

Python3
Python3
. . . .

Now, the Student class no longer handles database logic directly. It delegates the responsibility to the DatabaseManager. This reduces coupling and ensures that each class focuses on a single responsibility:

  • Student class: Manages student data (ID).
  • DatabaseManager class: Handles database-related functionality.

Coupling and SRP

Reducing coupling makes it easier to follow the Single Responsibility Principle. Here’s why:

  • High coupling often means that classes are doing too much, leading to multiple responsibilities, which violates SRP.
  • Low coupling helps ensure that each class focuses on a single responsibility, making the code more modular, easier to maintain, and simpler to understand.

By splitting the responsibilities (like student management and database operations) into different classes, we maintain low coupling and follow SRP. This ensures that each part of the system is focused and independent, making future changes much easier.

Mark as Completed
N

necas.radek

· 2 years ago

Hi, I like this course and how are different principles linked together in this part. I wanted to discuss the part where implementing SRP leads to low coupling. I think it can when it is doing well, but sometimes it might have an opposite effect. For example you have legacy code with class A with many responsibilities. You separate some responsibilities into new class B. But now class B can easily become tightly coupled with A because it was created (extracted) based on requirements of A. It can happen also when junior developers try to implement SRP blindly to their design. They have bigger functionality or use case. To implement it with SRP in mind they need to create classes with different responsibilities. But each class is then tightly coupled to the rest because it was created for th

Show 1 reply

On This Page

What is Coupling?

General Example: Tightly Connected Devices vs. Independent Devices

High Coupling in Code: One Class Doing Too Much

Reducing Coupling: Separate Database Logic from Student Class

Coupling and SRP