SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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,2020Puppet
 
Trunk based development for Beginners
Trunk based development for BeginnersTrunk based development for Beginners
Trunk based development for BeginnersNebulaworks
 
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 EconomicsPerforce
 
SOLID Software Principles with C#
SOLID Software Principles with C#SOLID Software Principles with C#
SOLID Software Principles with C#Ken Burkhardt
 
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 codevodQA
 
Walking Skeleton
Walking SkeletonWalking Skeleton
Walking Skeletonhepphep
 
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?Pekka Klärck
 
Distributed patching with composer
Distributed patching with composerDistributed patching with composer
Distributed patching with composerChristian Opitz
 
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 APIAurimas Niekis
 
Modern Module Development
Modern Module DevelopmentModern Module Development
Modern Module Development_morgan
 
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 aravamudhanXP Conference India
 
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 EnterpriseEficode
 
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 DapperMike Melusky
 
Angular Libraries & NPM
 Angular Libraries & NPM Angular Libraries & NPM
Angular Libraries & NPMKnoldus Inc.
 
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 badJoe Ferguson
 
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 platformNuxeo
 
Ansible, Idempotency, and Jenkins
Ansible, Idempotency, and JenkinsAnsible, Idempotency, and Jenkins
Ansible, Idempotency, and Jenkinstylerturk
 
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 PlatformPostman
 
Collaborative Package Development in R
Collaborative Package Development in RCollaborative Package Development in R
Collaborative Package Development in Regoodwintx
 

Tendances (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
 

Similaire à Code like a ninja session 3 open-closed principle

SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionKnoldus Inc.
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 
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 Molieremfrancis
 
Improving the Design of Existing Software
Improving the Design of Existing SoftwareImproving the Design of Existing Software
Improving the Design of Existing SoftwareSteven Smith
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentationBhavin Gandhi
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
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 BeyondSteve Westgarth
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing SoftwareSteven Smith
 
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 InterfacesJitendra Zaa
 
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 workChris O'Brien
 
Code refactor strategy part #1
Code refactor strategy part #1Code refactor strategy part #1
Code refactor strategy part #1Tracy LOISEL
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing SoftwareSteven Smith
 
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
 
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 2015Mirco Hering
 
Is your code SOLID enough?
 Is your code SOLID enough? Is your code SOLID enough?
Is your code SOLID enough?SARCCOM
 
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
 

Similaire à 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®
 

Dernier

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

Dernier (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

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

Notes de l'éditeur

  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