![Page 1: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/1.jpg)
CODE LIKE A NINJATHE OPEN/CLOSED PRINCIPLE
![Page 2: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/2.jpg)
SESSION RESOURCES
• Presentation session notes including link to this session, will be available on http://learningaboutfudge.blogspot.com
• All the source for this session is publically available at: https://github.com/SheepWorx/Training
• RSS Feed: http://learningaboutfudge.blogspot.com/feeds/posts/default?alt=rss
• Local Network: \\dmeyer-m\share\training\Code Like a Ninja
• Source was compiled using Visual Studio 2012
![Page 3: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/3.jpg)
Single Responsibility PrincipleOpen/Close PrincipleLiskov’s Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle
![Page 4: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/4.jpg)
![Page 5: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/5.jpg)
DEFINITION
The Open/Closed Principle
Software Entities (classes, modules, functions, etc) should be open for extension but closed for modification
![Page 6: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/6.jpg)
WHY?
• The behavior should be extendable without having to change chat class’s, function’s, etc code.
• New requirements should be introducible with new code, not by changing existing, working code, thus minimizing change fallout.
• Depending on where OCP is implemented, some classes will never be 100% closed. Strategic closure is required
• Make sure that OCP is implemented for most probable changes
![Page 7: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/7.jpg)
OCP - DIFFERENT APPROACHES
• Interfaces (composition)
• Inheritance (Bertrand Meyer)
• Inheritance (Robert C Martin)
• Extension Methods (C# 3.0)
![Page 8: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/8.jpg)
OCP - INTERFACES
Consider the following
• SomeService uses a DBLogger class to write logs
• What would happen if we wanted to introduce a different logger? What will need to change?
Is SomeService adhering to the OCP in this case?
![Page 9: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/9.jpg)
OCP – INTERFACES [SOLVED]
• Using interfaces, we have madeSomeService open for extension forintroducing different loggers and closed it for any change required tointroduce different loggers
• OCP is situational related. Just becauseSomeService adheres to the OCP for this,does not mean that it violates it for someother reason
![Page 10: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/10.jpg)
OCP – INHERITANCE (BERTRAND MEYER)
Consider the following
• You have an existing product class that’s beingused by many other classes
• You want to introduce new fields specifically forAirtime
• Without having to duplicate everything, how to we safely openProduct for extension and still keep it closed so that existing functionality is not affected?
![Page 11: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/11.jpg)
OCP – INHERITANCE (BERTRAND MEYER)
• We inherit directly off Product. Only code changemade was to make the GetProductDescriptionmethod virtual so that we can override it
• Product can still be used as is and instantiated asit has been
How does Robert C Martin solve this same problem?
![Page 12: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/12.jpg)
OCP – INHERITANCE (ROBERT C MARTIN)
• Robert’s solution is more “pure”
• Making the class abstract means thatit can only be inherited and never instantiated, forcing other classes to use the child classes
• GetProductDescription is also madeabstract, so that each inheriting classhas to use it’s own implementation.
![Page 13: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/13.jpg)
OCP – INHERITANCE [BERTRAND MEYER]
• Pro: Bertrand Meyer’s approach is verypractical if you’re thinking about extending production code without having to make any significant refactoring
• Con: Can get messy if not managed properly.
![Page 14: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/14.jpg)
OCP – INHERITANCE [ROBERT C MARTIN
• Pro: Robert C Martin’s approach ismuch less messy. It also (my personalopinion) a better solution from a codecohesion point of view
• Con: Easy to introduce with new codedesign but very difficult when trying toimplement on an existing code base that does not already support this design
![Page 15: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/15.jpg)
OCP – C# 3.0 EXTENSION METHODS
Demo
![Page 16: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/16.jpg)
HOMEWORK
Have a look at the Homework folder in the SRP solution folder
We covered this during the SRP session, but now, go and revisit your original solution and see if you would do things differently.
What type of approach would you use to implement OCP if you only had to open the code to use multiple message sending mechanisms, for ex?
![Page 17: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/17.jpg)
Next Session: S.O.L.I.D – Liskov’s Substitution Principles
Where: Thursday (April 24) @ 2pm-3pm in Training Room 1
![Page 18: Code like a ninja session 3 open-closed principle](https://reader038.vdocuments.mx/reader038/viewer/2022103001/55823857d8b42a0d368b4ba6/html5/thumbnails/18.jpg)
SESSION RESOURCES
• Presentation session notes including link to this session, will be available on http://learningaboutfudge.blogspot.com
• All the source for this session is publically available at: https://github.com/SheepWorx/Training
• RSS Feed: http://learningaboutfudge.blogspot.com/feeds/posts/default?alt=rss
• Local Network: \\dmeyer-m\share\training\Code Like a Ninja
• Source was compiled using Visual Studio 2012