Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench Room F203 Phone: (812)
Today The SOLID coding exercises See course website for zip file of all 5 of these They go with the quiz to turn in before class (as usual) Bring along your answers to discuss in class, too 2
Recall what SOLID is: S - SRP: Single Responsibility Principle We discussed this one in Session 13 O - OCP: Open Closed Principle L - LSP: Liskov Substitution Principle I - ISP: Interface Segregation Principle D - DIP: Dependency Inversion Principle Pioneered by Robert Martin (Uncle Bob) 3
Recall SRP THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE. (You looked at Pablo’s eBook on this – see especially pp ) 4
OCP SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION BUT CLOSED FOR MODIFICATION. 5
LSP FUNCTIONS THAT USE REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT. 6
ISP CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE. 7
DIP A. HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND UPON ABSTRACTIONS. B. ABSTRACTIONS SHOULD NOT DEPEND UPON DETAILS. DETAILS SHOULD DEPEND UPON ABSTRACTIONS 8
More Info Look at the SOLID article we saw before. There’s more in there beyond SRP! The SRP part you already studied is thru p 26. Then there’s an article on each of the other SOLID principles, too. 9
Let’s Learn By Doing! 10 Download SolidExamples (a zip file of 5 Java examples) from the course website. The quiz asks describe where there are issues, with which of the SOLID principles. And – How you would fix them! Do the quiz before class, as usual, but also be ready to discuss your answers in class.
A “Solid” Summary SRP: THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE. OCP: SOFTWARE ENTITIES SHOULD BE OPEN FOR EXTENSION BUT CLOSED FOR MODIFICATION LSP: FUNCTIONS THAT USE REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT. ISP: CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE. 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 11