• Save
Implementing The Open/Closed Principle
Upcoming SlideShare
Loading in...5

Implementing The Open/Closed Principle






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • A note
  • I hope to enable you to add some consistency by formalizing your thinking around OCP
  • Risk of making changeLessened by good automated testing coveragePlatforms Amazon vs. GoogleTwitter
  • You can apply the principle reactively
  • Let’s take a look at techniques for each layer
  • 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
  • Small is fast when it comes to changing direction10 executable lines is a good target, 20 the max (obviously there are always exceptions)
  • Just one client can mess it up for everyone
  • 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
  • Making your code ignorantAbstracting – to
  • Imagine this all over your application.Then we want to add FireBug/FirePHP logging support for our Ajax calls
  • Also called Composite Reuse Principle
  • What’s your goal
  • Can anyone think of any more class level techniques
  • Lots of examples from ZFThink how this has helped word press
  • Signal & SlotsPub/Sub
  • Cross cutting concernsFlow 3
  • Can anyone think of any more APPLICATION level techniques
  • Message bus
  • PubsubQueueTrue broadcast
  • 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
  • Why are you doing this?

Implementing The Open/Closed Principle Implementing The Open/Closed Principle Presentation Transcript

  • 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 Group2
  • i3logix3
  • 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 principle5 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 offerings7
  • What about YAGNI and KISS?8
  • Where Can It Be Applied? Class Application Enterprise9
  • Classes
  • Classes Inheritance Polymorphism Interfaces11
  • Small Methods12
  • No Public Properties Always be private? Encapsulation Information hiding Its cheap to do13
  • Globals and OCP14
  • De-coupling15
  • 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 Inheritance20
  • Behavior vs. Dependencies21
  • Application
  • Plugins23
  • Service Locator24
  • Dependency Injection25
  • Event Driven Programming26
  • Aspect Oriented Programming27
  • Rules Engine28
  • Build Process29
  • Enterprise
  • Public API31
  • Service Oriented Architecture (SOA)32
  • Global Broadcast33
  • When to break OCP!?
  • When to break OCP!? Separation of concerns Excessive Single duplication responsibility35
  • Everyday vs. the Revolutionary Facts dont Feature fit the doesnt fit theory the design36
  • What’s Your Strategy?
  • Questions?
  • Thanks You!