SlideShare a Scribd company logo
1 of 18
CODE LIKE A NINJA
THE 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-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012
Single Responsibility Principle
Open/Close Principle
Liskov’s Substitution Principle
Interface Segregation Principle
Dependency 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 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
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?
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?
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.
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.
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
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-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012

More Related Content

What's hot

What's hot (20)

Virtual Puppet Ecosystem Workshop - March 18,2020
Virtual Puppet Ecosystem Workshop - March 18,2020Virtual Puppet Ecosystem Workshop - March 18,2020
Virtual Puppet Ecosystem Workshop - March 18,2020
 
Trunk based development for Beginners
Trunk based development for BeginnersTrunk based development for Beginners
Trunk based development for Beginners
 
Trunk Based Development in the Enterprise - Its Relevance and Economics
Trunk Based Development in the Enterprise - Its Relevance and EconomicsTrunk Based Development in the Enterprise - Its Relevance and Economics
Trunk Based Development in the Enterprise - Its Relevance and Economics
 
SOLID Software Principles with C#
SOLID Software Principles with C#SOLID Software Principles with C#
SOLID Software Principles with C#
 
vodQA Pune (2019) - Jenkins pipeline As code
vodQA Pune (2019) - Jenkins pipeline As codevodQA Pune (2019) - Jenkins pipeline As code
vodQA Pune (2019) - Jenkins pipeline As code
 
Walking Skeleton
Walking SkeletonWalking Skeleton
Walking Skeleton
 
RoboCon 2018: How did we get here? Where do we go next?
RoboCon 2018: How did we get here? Where do we go next?RoboCon 2018: How did we get here? Where do we go next?
RoboCon 2018: How did we get here? Where do we go next?
 
Distributed patching with composer
Distributed patching with composerDistributed patching with composer
Distributed patching with composer
 
How to Supercharge your PHP Web API
How to Supercharge your PHP Web APIHow to Supercharge your PHP Web API
How to Supercharge your PHP Web API
 
Modern Module Development
Modern Module DevelopmentModern Module Development
Modern Module Development
 
Common blind spots on the journey to production vijay raghavan aravamudhan
Common blind spots on the journey to production  vijay raghavan aravamudhanCommon blind spots on the journey to production  vijay raghavan aravamudhan
Common blind spots on the journey to production vijay raghavan aravamudhan
 
DevOps Automation with Puppet Bolt & Puppet Enterprise
DevOps Automation with Puppet Bolt & Puppet EnterpriseDevOps Automation with Puppet Bolt & Puppet Enterprise
DevOps Automation with Puppet Bolt & Puppet Enterprise
 
Effective .NET Core Unit Testing with SQLite and Dapper
Effective .NET Core Unit Testing with SQLite and DapperEffective .NET Core Unit Testing with SQLite and Dapper
Effective .NET Core Unit Testing with SQLite and Dapper
 
Angular Libraries & NPM
 Angular Libraries & NPM Angular Libraries & NPM
Angular Libraries & NPM
 
Refactoring
RefactoringRefactoring
Refactoring
 
Continuous Integration: How I stopped guessing if that merge was bad
Continuous Integration: How I stopped guessing if that merge was badContinuous Integration: How I stopped guessing if that merge was bad
Continuous Integration: How I stopped guessing if that merge was bad
 
The Nuxeo Way: leveraging open source to build a world-class ECM platform
The Nuxeo Way: leveraging open source to build a world-class ECM platformThe Nuxeo Way: leveraging open source to build a world-class ECM platform
The Nuxeo Way: leveraging open source to build a world-class ECM platform
 
Ansible, Idempotency, and Jenkins
Ansible, Idempotency, and JenkinsAnsible, Idempotency, and Jenkins
Ansible, Idempotency, and Jenkins
 
The Economic Benefits of the Postman API Platform
The Economic Benefits of the Postman API PlatformThe Economic Benefits of the Postman API Platform
The Economic Benefits of the Postman API Platform
 
Collaborative Package Development in R
Collaborative Package Development in RCollaborative Package Development in R
Collaborative Package Development in R
 

Similar to Code like a ninja session 3 open-closed principle

Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
Alex Borsuk
 
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Junade Ali
 
Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®
Hannes Lowette
 

Similar to Code like a ninja session 3 open-closed principle (20)

SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
android principle.pptx
android principle.pptxandroid principle.pptx
android principle.pptx
 
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
 
Improving the Design of Existing Software
Improving the Design of Existing SoftwareImproving the Design of Existing Software
Improving the Design of Existing Software
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Entity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and BeyondEntity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and Beyond
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
0. series overview
0. series overview0. series overview
0. series overview
 
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
Episode 3 – Classes, Inheritance, Abstract Class, and InterfacesEpisode 3 – Classes, Inheritance, Abstract Class, and Interfaces
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
 
Chris OBrien - Azure DevOps for managing work
Chris OBrien - Azure DevOps for managing workChris OBrien - Azure DevOps for managing work
Chris OBrien - Azure DevOps for managing work
 
Code refactor strategy part #1
Code refactor strategy part #1Code refactor strategy part #1
Code refactor strategy part #1
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
 
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
 
Dev Ops for systems of record - Talk at Agile Australia 2015
Dev Ops for systems of record - Talk at Agile Australia 2015Dev Ops for systems of record - Talk at Agile Australia 2015
Dev Ops for systems of record - Talk at Agile Australia 2015
 
Is your code SOLID enough?
 Is your code SOLID enough? Is your code SOLID enough?
Is your code SOLID enough?
 
Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Buy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxBuy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptx
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Buy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdfBuy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdf
 
THE BEST IPTV in GERMANY for 2024: IPTVreel
THE BEST IPTV in  GERMANY for 2024: IPTVreelTHE BEST IPTV in  GERMANY for 2024: IPTVreel
THE BEST IPTV in GERMANY for 2024: IPTVreel
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
The UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, OcadoThe UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, Ocado
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 

Code like a ninja session 3 open-closed principle

  • 1. CODE LIKE A NINJA THE OPEN/CLOSED PRINCIPLE
  • 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
  • 3. Single Responsibility Principle Open/Close Principle Liskov’s Substitution Principle Interface Segregation Principle Dependency Inversion Principle
  • 4.
  • 5. DEFINITION The Open/Closed Principle Software Entities (classes, modules, functions, etc) should be open for extension but closed for modification
  • 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
  • 7. OCP - DIFFERENT APPROACHES • Interfaces (composition) • Inheritance (Bertrand Meyer) • Inheritance (Robert C Martin) • Extension Methods (C# 3.0)
  • 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
  • 15. OCP – C# 3.0 EXTENSION METHODS Demo
  • 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

  1. Basic srpExposeutils class antipatternExpose singleton as a bad pattern to use (use at own risk)
  2. \
  3. 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