SOLID WHAT’S HE GONNA TALK ABOUT What is SOLID The S in SOLID The O in SOLID The L in SOLID The I in SOLID The D in SOLID Questions WHAT’S THIS SOLID BUSINESS SOLID Principles Polymorphism Inheritance Encapsulation Object-Oriented Design WHAT’S THIS SOLID BUSINESS A means to avoid technical debt Decrease Coupling Increase Cohesion Maintainable Extensible Testable WHAT’S THIS SOLID BUSINESS Guidelines, NOT LAWS You got to know when to hold’em, know when to fold’em, Know when to walk away and know when to run. -Kenny Rogers THE REQUEST 1 Application EmailSender •SendEmail •GetMessage Flat File Single Responsibility Principle SRP There should never be more than one reason for a class to change -Uncle Bob VIOLATES THE PRINCIPLE Application EmailSender •SendEmail •GetMessage Flat File ABIDES BY THE PRINCIPLE Application EmailSender •SendEmail FileReader •GetMessage Flat File SINGLE RESPONSIBILITY PRINCIPLE A REQUEST 2 Application EmailSender •SendEmail Flat File FileReader •GetMessage XML File Open-Close Principle OCP Software entities (classes, modules, functions, etc…) should be open for extension, but closed for modification -Uncle Bob VIOLATES THE PRINCIPLE ABIDES BY THE PRINCIPLE Application EmailSender •SendEmail Flat File Application FileReader •GetMessage EmailSender •SendEmail FileReader •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader XML File BaseFileTypeReader •CanRead •ReadFile FlatFileReader •CanRead •ReadFile Flat File OPEN-CLOSED PRINCIPLE XmlFileReader •CanRead •ReadFile XML File REQUEST 3 Application EmailSender •SendEmail FileReader •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader BaseFileTypeReader Database FlatFileReader •CanRead •ReadFile Flat File •CanRead •ReadFile XmlFileReader •CanRead •ReadFile XML File DbFileReader •CanRead •ReadFile DB File Liskov Substitution Principle LSP Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it -Uncle Bob VIOLATES THE PRINCIPLE ABIDES BY THE PRINCIPLE Application EmailSender •SendEmail Application FileReader •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader Database BaseFileTypeReader •CanRead •ReadFile FlatFileReader •CanRead •ReadFile Flat File XmlFileReader •CanRead •ReadFile XML File EmailSender •SendEmail DbReader •GetMessage •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader BaseFileTypeReader •CanRead •ReadFile FlatFileReader DbFileReader •CanRead •ReadFile •CanRead •ReadFile DB File FileReader Database Flat File LISKOV SUBSTITUTION PRINCIPLE XmlFileReader •CanRead •ReadFile XML File REQUEST 4 Application EmailSender •SendEmail IReader •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader DbReader •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader FileReader •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader FlatFileReader •CanRead •ReadFile Database Flat File BaseFileTypeReader •CanRead •ReadFile XmlFileReader •CanRead •ReadFile XML File Interface Segregation Principle ISP Clients should not be forced to depend upon interfaces that they do not use -Uncle Bob ABIDES BY THE PRINCIPLE IFileTypeReaderRegisterable •RegisterDefaultFileTypeReader •RegisterFileTypeReader FileReader Application EmailSender •SendEmail •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader BaseFileTypeReader IReader •CanRead •ReadFile •GetMessage DbReader •GetMessage Database FlatFileReader •CanRead •ReadFile Flat File XmlFileReader •CanRead •ReadFile XML File INTERFACE SEGREGATION PRINCIPLE Dependency Inversion Principle DIP High level modules should not depend upon low level modules. Both should depend upon abstractions Abstractions should not depend upon details. Details should depend upon abstractions -Uncle Bob ABIDES BY THE PRINCIPLE IFileTypeReaderRegisterable •RegisterDefaultFileTypeReader •RegisterFileTypeReader FileReader Application IEmailSender •SendEmail EmailSender •SendEmail •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader BaseFileTypeReader IReader •CanRead •ReadFile •GetMessage DbReader •GetMessage Database FlatFileReader •CanRead •ReadFile Flat File XmlFileReader •CanRead •ReadFile XML File DEPENDENCY INVERSION PRINCIPLE WITHOUT SOLID Application Database EmailSender •SendEmail •GetMessage Flat File Xml File WITH SOLID IFileTypeReaderRegisterable •RegisterDefaultFileTypeReader •RegisterFileTypeReader FileReader Application IEmailSender •SendEmail EmailSender •SendEmail •GetMessage •RegisterDefaultFileTypeReader •RegisterFileTypeReader BaseFileTypeReader IReader •CanRead •ReadFile •GetMessage DbReader •GetMessage Database FlatFileReader •CanRead •ReadFile Flat File XmlFileReader •CanRead •ReadFile XML File QUESTIONS? REFERENCE LINKS Martin Folwer – Technical Debt Wikipedia – Coupling http://en.wikipedia.org/wiki/Cohesion_(computer_science) Robert Martin – The Principles of OOD http://en.wikipedia.org/wiki/Coupling_(computer_programming) Wikipedia – Cohesion http://www.martinfowler.com/bliki/TechnicalDebt.html http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod Derick Bailey – SOLID Principles – Step by step code https://github.com/derickbailey/presentations-andtraining/tree/master/SOLID%20Principles%20%20Step%20By%20Step%20Code MY INFORMATION Email – twang1978@gmail.com Twitter – @tonycwang LinkedIn – www.linkedin.com/in/tonycwang