2. 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-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012
6. 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
8. 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?
9. OCP – INTERFACES [SOLVED]
• Using interfaces, we have made
SomeService open for extension for
introducing different loggers and
closed it for any change required to
introduce different loggers
• OCP is situational related. Just because
SomeService adheres to the OCP for this,
does not mean that it violates it for some
other reason
10. OCP – INHERITANCE (BERTRAND MEYER)
Consider the following
• You have an existing product class that’s being
used by many other classes
• You want to introduce new fields specifically for
Airtime
• Without having to duplicate everything, how to we safely open
Product for extension and still keep it closed so that existing
functionality is not affected?
11. OCP – INHERITANCE (BERTRAND MEYER)
• We inherit directly off Product. Only code change
made was to make the GetProductDescription
method virtual so that we can override it
• Product can still be used as is and instantiated as
it has been
How does Robert C Martin solve this same problem?
12. OCP – INHERITANCE (ROBERT C MARTIN)
• Robert’s solution is more “pure”
• Making the class abstract means that
it can only be inherited and never
instantiated, forcing other classes to
use the child classes
• GetProductDescription is also made
abstract, so that each inheriting class
has to use it’s own implementation.
13. OCP – INHERITANCE [BERTRAND MEYER]
• Pro: Bertrand Meyer’s approach is very
practical if you’re thinking about
extending production code without
having to make any significant
refactoring
• Con: Can get messy if not managed
properly.
14. OCP – INHERITANCE [ROBERT C MARTIN
• Pro: Robert C Martin’s approach is
much less messy. It also (my personal
opinion) a better solution from a code
cohesion point of view
• Con: Easy to introduce with new code
design but very difficult when trying to
implement on an existing code base that does not already support this
design
16. 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?
17. Next Session: S.O.L.I.D – Liskov’s Substitution Principles
Where: Thursday (April 24) @ 2pm-3pm in Training Room 1
18. 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-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012
Editor's Notes
Basic srpExposeutils class antipatternExpose singleton as a bad pattern to use (use at own risk)
\
It’s very important to know where and how a feature might grow in the future. Which is why it’s so important for devs to work alongside architects when design software