Pages

Tuesday, August 9, 2016

SOLID


https://www.youtube.com/watch?v=gwIS9cZlrhk


Single Responsibility Principle - a function should only have one reason to change

OC Principle - a class should be open for extension but closed for modification. Once a class is done, it is done (unless there is a bug).

Meyer: Derives from requirements for the code. If requirements change, instead of changing the class, make a new class that inherits from the old class. Make the changes in the new class instead and leave the old class untouched. If you make the method in the old class "virtual", then you can "override" the method in the new class.

Massive question: how much of the class do you make "virtual". Very difficult to answer because once its coded, you are not supposed to go back and change from "virtual" to not "virtual" and vice versa.

Also, sometimes its hard to determine if its a bug or feature change.

Upside: if you code something and build a bunch of unit tests, if the code requirements change and you recode the logic, then the unit tests need to be redone. However, using OCP, because you don't change the original class, there is no need to redo existing tests. Instead, create a new class (which inherits from the old class) and therefore, you only need to create a new unit test of the new class.

Polymorphic: instead of Meyer method, create an abstract base class or an Interface and build new implementations as needed and plug in.

How to get it to work with abstract classes (AC) or interface? Since you are not inheriting, you have to really know what your AC or interface is supposed to be doing.

Both Meyer and Polymorphic hold that the original class should not need to be modified (unless there is a bug) to implement changes or to extend the original requirements.

BENEFIT: you don't change code once it works. Then you don't break code that is working.

Liskov Principle (about 29 min)

No comments:

Post a Comment