Tight Coupling- Suppose we have 2 classes A and B and other class C, D, E are tightly coupled with A and B. In this if we change some code in A, B then dependent classes needs to be changed and so on. So that’s why in real time we have to avoid or try to minimize tight coupling.
Loose Coupling: – In here when we change the change should be isolated we don’t want to change dependent classes of application.
How to achieve loose coupling?
For that, you need to understand isolation by using Encapsulation. The relationship between classes and most important Interface. It is an Is-a relationship
Ex. In Mobile service center front desk people not supposed to tell how to repair the mobile phone. This is the responsibility of that repairer.
In C# there are 2 types of relationship
- Inheritance – more of tight coupling
- Composition – Composition relationship results in loose coupling.
- Inheritance:- Depict parent-child relationship
- is used for the code to reuse.
- Polymorphic behavior
- Composition:- Is a kind of relationship between 2 classes that allows one object to contain into another object. Is Has-a relationship.
Ex. The bike has an Engine