solid principles part 2

13
SOLID Principles - II LSP – Liskov Substitution Principle ISP – Interface Segregation Principle Maulik Soni

Upload: maulik-soni

Post on 15-Apr-2017

235 views

Category:

Software


0 download

TRANSCRIPT

Page 1: SOLID Principles Part 2

SOLID Principles - IILSP – Liskov Substitution PrincipleISP – Interface Segregation Principle

Maulik Soni

Page 2: SOLID Principles Part 2

Maulik Soni

Liskov Substitution Principle

Subtypes must be substitutable for their base types.

Page 3: SOLID Principles Part 2

Maulik Soni

Is LSP Violated Here ?

Page 4: SOLID Principles Part 2

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

Page 5: SOLID Principles Part 2

Maulik Soni

Resolution

Page 6: SOLID Principles Part 2

Maulik Soni

Interface Segregation Principle

Client should not be forced to depend on methods they do not use

Page 7: SOLID Principles Part 2

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

Page 8: SOLID Principles Part 2

Maulik Soni

Example

Software

Page 9: SOLID Principles Part 2

Maulik Soni

Is ISP Violated Here?

Page 10: SOLID Principles Part 2

Maulik Soni

Resolution

Page 11: SOLID Principles Part 2

Maulik Soni

Re-Visit the solution

Software

IPrint IScan IFaxIPrintIScan

IFax IPrintIScan

IStaple

Page 12: SOLID Principles Part 2

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

Page 13: SOLID Principles Part 2

Maulik Soni

End of Presentation