SOLID – The Dependency Inversion Principle

Definition:

It states that high-level classes should not depend on low-level classes. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.

low level classes: are those which implement basic and primary operations.

high level classes: are those which encapsulate complex logic and rely on the low level classes.

dependency_inversion_principle

The Dependency Inversion principle (DIP) helps to loosely couple your code by ensuring that your high-level modules depend on abstractions rather than concrete implementations of lower-level modules.

The Dependency Injection pattern is an application/ implementation of this principle.

Example:

suppose that we have a banking system software. As a part of that software it is necessary to transfer money between accounts. So there is a bank account class which contains 2 properties account number and the balance. also 2 methods addFunds and removeFunds. Continue reading

Advertisements

SOLID – Interface Segregation Principle

Definition :

Clients should not be forced to depend upon interfaces that they do not use. Or as Uncle Bob says: Make fine grained interfaces that are client specific.

dggn8fwf_33cq6mtncq_b

The idea in this picture that we have a very small dependency which is the USB cable in the upper left but instead of being able to plug that into a USB port in a computer or something, we are forced to use that device here in the right which contains buttons , switches and lights and a lot of things that we don’t need.

When we have non-cohesive interfaces, the ISP guides us to create multiple, smaller, cohesive interfaces. The original class implements each such interface. Client code can then refer to the class using the smaller interface without knowing that other members exist.

Example:-

We have a fat Interface called IBird that contains these four methods or behaviors ( Walk , Eat , Sleep, Fly )

3-6-2013 2-19-02 PM

Continue reading