Upload
deon-meyer
View
153
Download
1
Tags:
Embed Size (px)
Citation preview
CODE LIKE A NINJATHE OPEN/CLOSED PRINCIPLE
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
Single Responsibility PrincipleOpen/Close PrincipleLiskov’s Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle
DEFINITION
The Open/Closed Principle
Software Entities (classes, modules, functions, etc) should be open for extension but closed for modification
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
OCP - DIFFERENT APPROACHES
• Interfaces (composition)
• Inheritance (Bertrand Meyer)
• Inheritance (Robert C Martin)
• Extension Methods (C# 3.0)
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?
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
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?
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?
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.
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.
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
OCP – C# 3.0 EXTENSION METHODS
Demo
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?
Next Session: S.O.L.I.D – Liskov’s Substitution Principles
Where: Thursday (April 24) @ 2pm-3pm in Training Room 1
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