Loose coupling vs Tight coupling in the object-oriented design

Loose coupling vs Tight coupling in the object-oriented design

What are the differences between tight coupling and loose coupling in object-oriented design?

Object-Oriented Design

Object-oriented design (OOD) is the process of planning a system of interacting objects to solve a software problem.n OOD skills are a significant plus for software engineers.

Object-Oriented design vs System Design

System design is designing the software application as a whole high level that may include analysis, modeling, architecture, Components, Infrastructure, etc.

Objected-oriented design is the set of defined rules & concepts to implement the functionalities within the software.


Introduction

Hi folks! Today, in this article we will explore the most misunderstood topic of tight coupling and loose coupling with simple examples.

What is Tight coupling?

Tight coupling means classes and objects are dependent on one another.

In general, tight coupling is usually not good because it reduces the flexibility and re-usability of the code.

What is Loose coupling?

Loose Coupling means one class is dependent on the interface rather than the class.

while using Loose coupling means reducing the dependencies of a class that uses the different class directly.

In Simple Terms

Tight coupling refers to a situation where two or more software components are closely connected and depend on each other to function properly.

Loose coupling, on the other hand, means that the components are less dependent on each other and can operate more independently.

Example

Let's consider a scenario in which you have to create an application for Travelers in your program who can travel in Car. In a straightforward approach. we may create a class Car & Traveler class.

Tight couple example

<?php
class Car {
    public function move() {
         echo "Car is moving";
    }
}

class Traveler {

    public $vehicle;
    public __constructor() {
        $this->vehicle = new Car();
        $this->vehicle->move();
    }

}

But, In future, the traveler may use any mode of transport like Bike & Bus. Now you have to go and modify the whole Traveler class & Car class.To avoid this inconvenience of expanding the functionality the class & object structure must be loosely coupled.

Loose couple example

<?php
interface Vehicle {
      public void move();
}

class Car implements Vehicle {
    public function move() {
         echo "Car is moving";
    }
}

class Bike implements Vehicle {
    public function move() {
         echo "Bike is moving";
    }
}

class Bus implements Vehicle {
    public function move() {
         echo "Bus is moving";
    }
}

class Traveler {

    public Vehicle $vehicle;
    public __constructor(Vehicle $vehicle) {
        $this->vehicle = $vehicle;
        $this->vehicle->move();
    }
}

//Because of loosly coupling we are initializing transport on higher level
$car = new Car();
$person_1 = new Traveler($car); 

$bus = new Bus();
$person_2 = new Traveler($bus);

Now, you can see the difference. In the future, we can extend the transport mode in various forms. The Traveler class doesn't depend on any of the concrete implementations.

I hope now you got a clear idea of both loosely & tightly coupling.

Differences Between Loose Coupling and Tight Coupling

Loose Coupling

Tight Coupling

Objects are independent of each other.

One object is dependent on the other object to complete a task.

Better testability

Testability is not as great as the loose coupling.

Less coordination. Swapping code between two classes is not easy.

Provides better coordination. You can easily swap code between two objects.

Highly changeable

It does not have the change capability.

Conclusion

Hopefully, this will be useful to some of you :).

I'd love to hear your comments on improvements. Follow for more content like this & share it with your friends

Did you find this article valuable?

Support Saravana Sai by becoming a sponsor. Any amount is appreciated!