solid principles part 2
TRANSCRIPT
SOLID Principles - IILSP – Liskov Substitution PrincipleISP – Interface Segregation Principle
Maulik Soni
Maulik Soni
Liskov Substitution Principle
Subtypes must be substitutable for their base types.
Maulik Soni
Is LSP Violated Here ?
Maulik Soni
How To Spot LSP Violation ?
Derivative that tries to do less than base class Instance of checks Hiding or stubbing parent methods
Void SomeMethod(){} Void SomeMethod(){ Throw new ShouldNotBecalledException(“…”);}
Polymorphic if Statements
Maulik Soni
Resolution
Maulik Soni
Interface Segregation Principle
Client should not be forced to depend on methods they do not use
Maulik Soni
Why ISP Important ?
Each member of an interface needs to be implemented in its entirety Properties Events Methods
Unless every client of an interface requires every member It does not make sense to force client to implement such a large contract
Maulik Soni
Example
Software
Maulik Soni
Is ISP Violated Here?
Maulik Soni
Resolution
Maulik Soni
Re-Visit the solution
Software
IPrint IScan IFaxIPrintIScan
IFax IPrintIScan
IStaple
Maulik Soni
Summary
Design cohesive interfaces and avoid “fat” interfaces The dependency of one class to another should depend on the smallest possible
interface The interface of the class can be broken up into groups of methods
Each group serves a different set of clients Check Classes with a high number of Public methods
Group clients according to their calls to the public methods Check for methods that frequently change together
Maulik Soni
End of Presentation