SlideShare a Scribd company logo
Implementing The Open/Closed Principle
Sam Hennessy




    • Originally from the UK
    • Now live in Denver, CO
    • Software Architect at i3logix
    • Ex-Pro Services Consultant For Zend
    • Regular at Front Range PHP Users Group


2
i3logix




3
What is it?




     Part of SOLID

     Protect your system from change

     Do you already do it?


4
SOLID


            S       Single responsibility
                          principle

            O      Open/closed principle


            L   Liskov substitution principle


            I      Interface segregation
                          principle


5
            D      Dependency inversion
                        principle
Open/Closed Principle




    "Be open for extension,
           but closed for
    modification."

6
And I Care Why?


                             Maintenance



               Let your
                                             Customer
            users bridge a
                                           customization
             feature gap




             Competitive                     Preventing
                edge                        the "FORK"!



                              A-la-cart
                               feature
                              offerings
7
What about YAGNI and KISS?




8
Where Can It Be Applied?


                               Class


                               Application


                               Enterprise




9
Classes
Classes




                         Inheritance




               Polymorphism            Interfaces




11
Small Methods




12
No Public Properties




                            Always be private?


                              Encapsulation


                            Information hiding


                             It's cheap to do


13
Globals and OCP




14
De-coupling




15
class Calculator{
    public function __invoke($calc, $left, $right) {
        if ($calc == 'add') {
            return $left + $right;
        } else if ($calc == 'sub') {
            return $left - $right;
        }
    }
}
class Calculator{
    public function __invoke(Calc $calc, $left, $right){
        return $calc($left, $right);
    }
}


$calculator = new Calculator();
echo $calculator(new AddCalc(), 2, 1), "n";
echo $calculator(new SubCalc(), 2, 1), "n";
interface Calc {
    public function __invoke($left, $right);
}


class AddCalc implements Calc {
    public function __invoke($left, $right) {
        return $left + $right;
    }
}


class SubCalc implements Calc {
    public function __invoke($left, $right) {
        return $left - $right;
    }
}
if ($logType == 'echo') {
    echo $message;
} else if ($logType == 'file') {
    $logger->writeToFile($message);
}
Composition Over Inheritance




20
Behavior vs. Dependencies




21
Application
Plugins




23
Service Locator




24
Dependency Injection




25
Event Driven Programming




26
Aspect Oriented Programming




27
Rules Engine




28
Build Process




29
Enterprise
Public API




31
Service Oriented Architecture (SOA)




32
Global Broadcast




33
When to break OCP!?
When to break OCP!?



                             Separation of
                               concerns




                Excessive                        Single
               duplication                   responsibility



35
Everyday vs. the Revolutionary




       Facts don't                      Feature
          fit the                      doesn't fit
         theory                       the design



36
What’s Your Strategy?
Questions?
Thanks You!

More Related Content

PPTX
Solid principles
PPTX
Learning solid principles using c#
PPTX
Solid principles
PDF
SOLID Design Principles applied in Java
PPT
OO design principles & heuristics
PPTX
Solid Principles
PPT
principles of object oriented class design
PPTX
Object Oriented Design SOLID Principles
Solid principles
Learning solid principles using c#
Solid principles
SOLID Design Principles applied in Java
OO design principles & heuristics
Solid Principles
principles of object oriented class design
Object Oriented Design SOLID Principles

What's hot (20)

PPTX
Open Closed Principle kata
PPT
SOLID Design Principles
PPT
The OO Design Principles
PPTX
Solid design principles
PPTX
Design Patterns: From STUPID to SOLID code
PPTX
S.O.L.I.D. Principles for Software Architects
PDF
Solid principles of oo design
PDF
Solid Principles
PDF
Object-oriented design principles
PPTX
Principios solid con c#
PDF
SOLID design principles applied in Java
PPTX
SOLID principles
PPTX
Writing Maintainable Software Using SOLID Principles
PDF
SOLID Design principles
PDF
The Open Closed Principle - Part 1 - The Original Version
PPTX
The Solid Principles
PPTX
Design principles - SOLID
KEY
SOLID Design Principles
PPTX
Software Design principles
PPT
SOLID principles
Open Closed Principle kata
SOLID Design Principles
The OO Design Principles
Solid design principles
Design Patterns: From STUPID to SOLID code
S.O.L.I.D. Principles for Software Architects
Solid principles of oo design
Solid Principles
Object-oriented design principles
Principios solid con c#
SOLID design principles applied in Java
SOLID principles
Writing Maintainable Software Using SOLID Principles
SOLID Design principles
The Open Closed Principle - Part 1 - The Original Version
The Solid Principles
Design principles - SOLID
SOLID Design Principles
Software Design principles
SOLID principles
Ad

Viewers also liked (11)

PDF
PHP in the Real World
PPT
Task Scheduling and Asynchronous Processing Evolved. Zend Server Job Queue
PPT
PHP – Faster And Cheaper. Scale Vertically with IBM i
PPTX
Adding Dependency Injection to Legacy Applications
ODP
Open Close Principle
PPTX
Solid principles
PDF
jeas_0816_4883
PPTX
Dependency Inversion Principle
PPTX
Single Responsibility Principle
PPTX
SOLID Principles part 1
PPTX
List of Software Development Model and Methods
PHP in the Real World
Task Scheduling and Asynchronous Processing Evolved. Zend Server Job Queue
PHP – Faster And Cheaper. Scale Vertically with IBM i
Adding Dependency Injection to Legacy Applications
Open Close Principle
Solid principles
jeas_0816_4883
Dependency Inversion Principle
Single Responsibility Principle
SOLID Principles part 1
List of Software Development Model and Methods
Ad

Similar to Implementing The Open/Closed Principle (20)

PPTX
Improving The Quality of Existing Software
PPTX
Improving the Quality of Existing Software
PPTX
Improving the Quality of Existing Software
PPTX
Refactoring Applications using SOLID Principles
PPTX
SOLID Principles of Refactoring Presentation - Inland Empire User Group
PPTX
Improving the Quality of Existing Software - DevIntersection April 2016
PPTX
Improving the Quality of Existing Software
PPTX
Object Oriented Apologetics
ODP
PPT
Object Oriented Concepts and Principles
PPTX
1012892161-Module-4-Agile-Software-Design-and-Development.pptx
PDF
The rocket internet experience @ PHP.TO.START 2013 in Turin
PDF
NDC 2011 - Building .NET Applications with BDD
PPTX
Application Architecture April 2014
PPTX
Improving the Design of Existing Software
PDF
Object Oriented Design Principles
PPTX
Common ASP.NET Design Patterns - Telerik India DevCon 2013
PPT
Arch factory - Agile Design: Best Practices
PPTX
Software Engineering Primer
PPTX
Building Large Scale PHP Web Applications with Laravel 4
Improving The Quality of Existing Software
Improving the Quality of Existing Software
Improving the Quality of Existing Software
Refactoring Applications using SOLID Principles
SOLID Principles of Refactoring Presentation - Inland Empire User Group
Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software
Object Oriented Apologetics
Object Oriented Concepts and Principles
1012892161-Module-4-Agile-Software-Design-and-Development.pptx
The rocket internet experience @ PHP.TO.START 2013 in Turin
NDC 2011 - Building .NET Applications with BDD
Application Architecture April 2014
Improving the Design of Existing Software
Object Oriented Design Principles
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Arch factory - Agile Design: Best Practices
Software Engineering Primer
Building Large Scale PHP Web Applications with Laravel 4

Recently uploaded (20)

PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
KodekX | Application Modernization Development
NewMind AI Monthly Chronicles - July 2025
Chapter 3 Spatial Domain Image Processing.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Unlocking AI with Model Context Protocol (MCP)
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology

Implementing The Open/Closed Principle

Editor's Notes

  • #2: A note
  • #5: I hope to enable you to add some consistency by formalizing your thinking around OCP
  • #8: Risk of making changeLessened by good automated testing coveragePlatforms Amazon vs. GoogleTwitter
  • #9: You can apply the principle reactively
  • #10: Let’s take a look at techniques for each layer
  • #12: Traditional ideas of OCP what you be able to write the class code once and never have to come back to edit itThese techniques focus on this level changeSome of my application level techniques kind of blur the linePrevent changes to a classDoesn’t prevent change to the app, unless you are using DI or something like it
  • #13: Small is fast when it comes to changing direction10 executable lines is a good target, 20 the max (obviously there are always exceptions)
  • #14: Just one client can mess it up for everyone
  • #15: Traditional ideas of OCP say that globals break OCPThese are points of view that are just thinking about riskYou are introducing risk that other parts of the system will break this code
  • #16: Making your code ignorantAbstracting – to
  • #20: Imagine this all over your application.Then we want to add FireBug/FirePHP logging support for our Ajax calls
  • #21: Also called Composite Reuse Principle
  • #22: What’s your goal
  • #23: Can anyone think of any more class level techniques
  • #24: Lots of examples from ZFThink how this has helped word press
  • #27: Signal & SlotsPub/Sub
  • #28: Cross cutting concernsFlow 3
  • #31: Can anyone think of any more APPLICATION level techniques
  • #33: Message bus
  • #34: PubsubQueueTrue broadcast
  • #37: Martin FowlerIsolate the assumptions scattered around the code to a single element (by assumptions, you assumed they wouldn't need to change) E.g. You are echoing all over the code, but you need to support encoding to jsonThis will reduce the risk for the big change Or start from scratch
  • #38: Why are you doing this?