Interface Segregation and Dependency Inversion Principle

Interface Segregation Principle

  1. No client should be forced to depend on methods it does not use
  2. One fat interface need to be split to many smaller and relevant interfaces so that clients can know about the interfaces that are relevant to them
  3. this principle was first used and formulated by Robert C. Martin while consulting for xerox

Case study:Problem
  1. As we all know Xerox had created a new printer system that could perform a variety of tasks such as stapling and faxing along with the regular printing task
  2. The software for this system was created from the ground up .
  3. Modification and Deployment to the system became more complex.
Note: The design problem was that in single class was used by almost of all task whenever a print job or stapling job be performed a call was made to the big job class.
To cure this problem Robert Mrtins has suggested
this ISP principle

Solution 

One large Job class is segregated to multiple interfaces depending on the requirement.

Let us understand with following code and picture.


Dependency Inversion Principle


  1. High level modules should not depend on low-level modules.Both should depend on abstractions
  2. abstraction should not depend on details .Details should depend on abstractions.
  3. The interaction between high level and low level modules should be thought of as an abstract interaction between them
  4. This principle was introduced by Robert C Martin


Note: Before understanding of intention of usage let's try to understand a traditional application of architecture in application during the process of design lower level components trying to consumed of higher level components enable increasingly complex systems to be build in this process of composition higher level components depend directly on lower level of task.



So this does not follow of DIP
Now see the below code and picture.


Notice that business layer directly  depend on lower level data access layer. It is very hard to perform unit test. Off course it is bad idea and much more complex.

Hence based on inversion principle we apply on abstraction to T-coupled this layer.
In order to achieve that we introduced that we use Interface and repository We have used.



If we look this code based on above pictorial representation diagram we have introduced interface which are extended by the data access-layer and referred in the business logic-layer

Real Example 
Adapter design pattern applying dependency inversion principle




Post a Comment

0 Comments