SlideShare una empresa de Scribd logo
1 de 13
The Open/Closed Principle
S O L I D
The Open/Closed Principle
“software entities
(classes, modules, functions, etc.)
should be open for extension,
but closed for modification”
that is, such an entity can allow its
behaviour to be modified without altering
its source code
adding a new feature
Source: Dave Nicolette
OCP kata
0. Write the first failing test.
Then write a factory that returns an object,
or an aggregate of objects,
that make the test pass.
 The factory should be limited to creating objects
and linking them together.
 No conditionals allowed in the factory.
Factory test example
@Test
public void simpleAddition() {
Calculator calc = new CalculatorFactory().create();
assertEquals(7, calc.eval(“3 + 4”));
}
OCP kata
1. Write the next failing test.
OCP kata
2. Can you make it pass by changing the factory
and/or creating a new class and nothing else?
If yes, great! Go back to 1.
If not, refactor until you can.
The refactoring should bring the system to a state
where it’s possible to implement the next test just
by changing the aggregate of objects that is
returned by the factory. Be careful not to implement
new functionality; the current test must still fail.
Remember: no conditionals in the factory.
(For extra bonus points: no conditionals anywhere!)
tests
Credits:
Matteo Vaccari invented the OCP kata:
http://matteo.vaccari.name/blog/archives/293
http://www.slideshare.net/xpmatteo/20101125-ocpxpday
Dave Nicolette created the jigsaw drawing
The Open/Closed Principle (OCP
The Open/Closed Principle (OCP
The Open/Closed Principle (OCP
The Open/Closed Principle (OCP
The Open/Closed Principle (OCP

Más contenido relacionado

La actualidad más candente

La actualidad más candente (13)

Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
Mocking with Mockito
Mocking with MockitoMocking with Mockito
Mocking with Mockito
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
 
Productionizing spark
Productionizing sparkProductionizing spark
Productionizing spark
 
Mock with Mockito
Mock with MockitoMock with Mockito
Mock with Mockito
 
Power mock
Power mockPower mock
Power mock
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
Junit
JunitJunit
Junit
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
React hooks
React hooksReact hooks
React hooks
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
One mock to far
One mock to farOne mock to far
One mock to far
 
Mockito intro
Mockito introMockito intro
Mockito intro
 

Similar a The Open/Closed Principle (OCP

Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kataPaul Blundell
 
TDD step patterns
TDD step patternsTDD step patterns
TDD step patternseduardomg23
 
Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7ashhadiqbal
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It'sJim Lynch
 
Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Myeongseok Baek
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitAmr E. Mohamed
 
Mutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekMutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekPaco van Beckhoven
 
Oracle Unit Testing with utPLSQL
Oracle Unit Testing with utPLSQLOracle Unit Testing with utPLSQL
Oracle Unit Testing with utPLSQLBrendan Furey
 
SAP Testing Training
SAP Testing TrainingSAP Testing Training
SAP Testing TrainingVGlobal Govi
 
INTRODUCTION TO MACHINE LEARNING FOR MATERIALS SCIENCE
INTRODUCTION TO MACHINE LEARNING FOR MATERIALS SCIENCEINTRODUCTION TO MACHINE LEARNING FOR MATERIALS SCIENCE
INTRODUCTION TO MACHINE LEARNING FOR MATERIALS SCIENCEIPutuAdiPratama
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 

Similar a The Open/Closed Principle (OCP (20)

Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kata
 
TDD step patterns
TDD step patternsTDD step patterns
TDD step patterns
 
Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7
 
Test Driven
Test DrivenTest Driven
Test Driven
 
Come si applica l'OCP
Come si applica l'OCPCome si applica l'OCP
Come si applica l'OCP
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
Lab5
Lab5Lab5
Lab5
 
Ansible testing
Ansible   testingAnsible   testing
Ansible testing
 
L2624 labriola
L2624 labriolaL2624 labriola
L2624 labriola
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
 
Java bad coding practices
Java bad coding practicesJava bad coding practices
Java bad coding practices
 
Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기
 
Refactoring Chapter 6,7.pptx
Refactoring Chapter 6,7.pptxRefactoring Chapter 6,7.pptx
Refactoring Chapter 6,7.pptx
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
 
Mutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekMutation testing Bucharest Tech Week
Mutation testing Bucharest Tech Week
 
Oracle Unit Testing with utPLSQL
Oracle Unit Testing with utPLSQLOracle Unit Testing with utPLSQL
Oracle Unit Testing with utPLSQL
 
QTP Faqs
QTP FaqsQTP Faqs
QTP Faqs
 
SAP Testing Training
SAP Testing TrainingSAP Testing Training
SAP Testing Training
 
INTRODUCTION TO MACHINE LEARNING FOR MATERIALS SCIENCE
INTRODUCTION TO MACHINE LEARNING FOR MATERIALS SCIENCEINTRODUCTION TO MACHINE LEARNING FOR MATERIALS SCIENCE
INTRODUCTION TO MACHINE LEARNING FOR MATERIALS SCIENCE
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 

Más de Kevin Rutherford (20)

Making the most of autism
Making the most of autismMaking the most of autism
Making the most of autism
 
Evolving the Kanban Board
Evolving the Kanban BoardEvolving the Kanban Board
Evolving the Kanban Board
 
Don't forget the developers!
Don't forget the developers!Don't forget the developers!
Don't forget the developers!
 
Love and death
Love and deathLove and death
Love and death
 
Connascence hunting
Connascence huntingConnascence hunting
Connascence hunting
 
The 5-day challenge
The 5-day challengeThe 5-day challenge
The 5-day challenge
 
Red, green, ... what now?!
Red, green, ... what now?!Red, green, ... what now?!
Red, green, ... what now?!
 
Pair programming
Pair programmingPair programming
Pair programming
 
Tdd is dead
Tdd is deadTdd is dead
Tdd is dead
 
Hex arch
Hex archHex arch
Hex arch
 
Shepherding antipatterns
Shepherding antipatternsShepherding antipatterns
Shepherding antipatterns
 
Kata rules
Kata rulesKata rules
Kata rules
 
Movie app kata
Movie app kataMovie app kata
Movie app kata
 
Object discovery
Object discoveryObject discovery
Object discovery
 
Connascence
ConnascenceConnascence
Connascence
 
OCP Checkout kata
OCP Checkout kataOCP Checkout kata
OCP Checkout kata
 
OCP bowling kata
OCP bowling kataOCP bowling kata
OCP bowling kata
 
OCP String Calculator kata
OCP String Calculator kataOCP String Calculator kata
OCP String Calculator kata
 
Telling not-asking
Telling not-askingTelling not-asking
Telling not-asking
 
Uml
UmlUml
Uml
 

Último

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 

Último (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 

The Open/Closed Principle (OCP

  • 2. The Open/Closed Principle “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” that is, such an entity can allow its behaviour to be modified without altering its source code
  • 3. adding a new feature Source: Dave Nicolette
  • 4. OCP kata 0. Write the first failing test. Then write a factory that returns an object, or an aggregate of objects, that make the test pass.  The factory should be limited to creating objects and linking them together.  No conditionals allowed in the factory.
  • 5. Factory test example @Test public void simpleAddition() { Calculator calc = new CalculatorFactory().create(); assertEquals(7, calc.eval(“3 + 4”)); }
  • 6. OCP kata 1. Write the next failing test.
  • 7. OCP kata 2. Can you make it pass by changing the factory and/or creating a new class and nothing else? If yes, great! Go back to 1. If not, refactor until you can. The refactoring should bring the system to a state where it’s possible to implement the next test just by changing the aggregate of objects that is returned by the factory. Be careful not to implement new functionality; the current test must still fail. Remember: no conditionals in the factory. (For extra bonus points: no conditionals anywhere!)
  • 8. tests Credits: Matteo Vaccari invented the OCP kata: http://matteo.vaccari.name/blog/archives/293 http://www.slideshare.net/xpmatteo/20101125-ocpxpday Dave Nicolette created the jigsaw drawing