# 90 - Creating SOLID Code: Open/Closed Principle (OCP)


3/5/2009
Level: Intermediate
Tags: SOLID Principle
Comments: (16)
Author:
Stephen Bohlen


This is the second in a 5 part series where we will explore the tenets of the S.O.L.I.D principles.

In this episode we will take a look at the O (Open/Closed Principle) and see how following this principle can help us to create cleaner, more manageable code

S - Single Responsibility Principle
O - Open/Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle
Click here to Watch this Episode

Download (12.30) (10:45) (1440x900)
Download (9.20) (10:45) (960x600)

Links/Resources:


Comments: (16)

Julian Easterling said....

I recently found this website/feed and at first was excited about the podcast. That was until I learned that the Standard RSS feed did not have the enclosures in it and don't work with my podcast program (Juice). Then I found that enclosures are in the iPhone feed, but unfortunately, the video is so compress that you can't see anything on the screen. Any chance you guys can add the full size WMV files as enclosures on the Standard feed?


3/5/2009 6:02:34 PM

Dimecasts said....

@Julian,

I a sorry to say, but there is no plans at this point in time to put the full video into the RSS feed.

 


3/6/2009 4:05:17 AM

vedic said....

how can you portray such an important principle by massive code duplication practice. this is hilarious and irritating at the same time. fail.


3/7/2009 5:30:59 AM

joey said....

I agree that this is a poor representation of the OCP.

If I saw this and didn't understand the SOLID principles, I would run away from them.


3/7/2009 1:41:01 PM

Steve Bohlen said....

@vedic, @joey:

I'm sorry that you're not finding value in this content.

FWIW I struggled for some time to balance the needs of the delivery format (a single 10-minute screencast) with the complexity of each topic being demonstrated for this series.  My intent was to find a demo software instance that was at the same time...

a) simple enough for everyone to understand without any time spent learning it
b) complex enough to be able to provide enough structure to (somehow) benefit in even a minor way from each of the SOLID principles having been applied to it

As you are pointing out, this meant some serious compromises in the way the samples are engineered and I think that you are (accurately) pointing this out in your comments about such things as 'code duplication', etc.

I would encourage you to try to focus, however, not so much on the mechanics of what each class does but the pattern of refactoring being applied to the classes.  The fact that my early act of subclassing the superclasses of Report, ReportPrinter, and ReportFormatter was to copy en masse the entire body of each superclasses into its subclass was really not intended to be the focus of the screencast (and as you point out, is --hopefully!-- not what anyone would do in 'real life') with a more complex class.  My act of copy-paste-modify was nothing more than a technique to enable me to skip having to write more code in the screencast and since each class already only had a single method, copying it seemed the most expeditious manner ensure the screencast kept moving at a rapid pace.

The format of a 10-minute delivery forces many compromises and I would like to think that despite this, the goal of the screencast has been met: to demonstrate that one of the most common patterns for applying the OCP is to replace a superclass instance with a subclass instance that provides different behaviors, permitting the overall behavior of the system to change without editing the existing code and with minimal impact on the classes that don't change.  IMO this is a completley legitimate implementation of the OCP in software systems and I would hope that many would agree.

The 10-minute limited format of these screencasts probably leaves the door open for ALL KINDS of misinterpretations.  For example, one could possibly (erroneously!) conclude from my SRP screencast that SRP really means one-method-per-class since my application of SRP to my incredibly simple class model had indeed resulted in just a single method-per-class.  This would of course be a completely incorrect reading of the single responsibility principle in the same way that you have pointed out that copying a base class' guts into a derived class is similarly *NOT* what OCP is about.

All that being said, if either of you two (or anyone else) has CONSTRUCTIVE suggestions about how these screencasts could be improved in re: their content, focus, or otherwise while continuing to operate within the boundaries of the 10-minute time-limit that constrains them, I would be more than interested to take those suggestions to heart and even to apply them to re-recording of the screencast(s) as its my hope that this content be of the maximum value to the maximum number of people and its only by the receipt of CONSTRUCTIVE criticism with ACTIONABLE suggestions that we can all hope to achieve that goal.

Thanks for your feedback and I look forward to your suggestions.


3/8/2009 10:29:20 AM

vedic said....

An Exception of Type RunningLikeCrap has Occured... :))

but seriously...

I believe that this inauspicious limit of content delivery running time to just 10 minutes is appaling. It clearly forces authors to resort to wishful thinking that the goals to demonstrate most common patterns have been met.

I suggest lifting the time limit.


3/8/2009 12:58:26 PM

Dimecasts said....

@Vedic,

The timeline of give or take 10 minutes is the design/goal of the site.  Because we have the limit we have drawn a great crowd and people love it. 

If a few casts get 'mis-represented' that is a shame, but the format of the site is >5 min, <13.

Sorry


3/8/2009 1:32:33 PM

Steve Bohlen said....

@vedic:

As so often happens on the internet with so much of the 'conversation' mechanisms available, I fear that we are talking *past* each other instead of *to* each other.  Since I genuinely *am* interested in hearing some input from you about how to improve the quality of this installment of the series, I will try just one more time...

To be as clear as I can in my request to you: I am requesting any specific, *ACTIONABLE* suggestions that you might have that you feel would improve the quality of this installment.  Examples of such things might be:

a) a code sample (both 'before' and 'after') that you feel better demonstrates the Open-Close Principle
b) a better way to refactor the existing sample such that it better represents the Open-Close Principle
c) anything else that you can think of

All of these kinds of suggestions would have to fit within the 10-minute constraints of the format.

While it can be fun to take the tact "If the rules are too limiting, let's just re-write the rules" but this is not an option in this context and so the suggestion "lift the time limit" doesn't really represent an ACTIONABLE suggestion.  My approach to this installment was to attempt to balance the needs of explaining and demonstrating the OCP applied to a sample code base against the constraints of the medium for the delivery of the content.

If you feel that there is some way that I could improve the balance between these two competing forces then I am ready, willing, and able to discuss this with you and anyone else in the interests of improving the quality of the content.  But I am not ready to capitulate on the point you seem to be trying to make that there is no value in attempting to introduce this concept within a 10-minute screencast.

As before, I await your suggestions.


3/8/2009 1:45:59 PM

Thedric said....

 Great screencast. Some people just need something to complain about. For if they thought for a second they wold realize that the code duplication will be taken care of when you pull the LID off the remaining principles.


3/8/2009 6:00:21 PM

vedic said....

@Dimecasts
Yes, I like the site too, otherwise i'd be clicking away already. I promise I will as soon as things on it start to piss me off.

@Stevie
Good. You're quite perceptive. And yes i KNOW what you are genuinely INTERESTED in. And why. etc... But quite frankly i have no interest in pages long discussions so your request had to be rejected. Don't be sad now.

I'll Focus instead on trying to find a screencast where i could post a positive comment. lets see... 


3/9/2009 12:10:14 AM

Jon Shin said....

Thanks for doing these videos Stephen.  Don't listen to the detractors that can't see the big picture.  Keep em coming!


3/12/2009 7:36:14 AM

TERACytE said....

I appreciate this and the other casts that you have done.  I found it very informative.


3/13/2009 7:49:59 PM

Kris said....

Hello,

Please add your site at http://www.sweebs.com. Sweebs.com is a place where other people can find you among the best sites on the internet!
Its just started and we are collecting the best found on the net! We will be delighted to have you in the sweebs listings.

Regards
Kris


3/19/2009 11:53:55 AM

flipdoubt said....

@Steve Bohlen: My suggestion for how to deal with the time limits and limitations of scope is to point to more in depth discussions that you find helpful. The link to Uncle Bob's SOLID principles is fine but too general. What resources or discussions really opened your eyes on OCP with concrete examples that would require more time to digest than a 10 minute Dimecast allows? 


4/6/2009 5:01:37 AM

Dan Crowell said....

Steve, thank you for the podcast. I am really enjoying the SOLID series. Dan


4/10/2009 1:48:59 PM

prettyreplica said....

Replica Watches Collection

Bvlgari Watches Replica, Swiss Rolex Watch Collection, Best Fake Watch ,Cartier Watches ,Discount Watches , Fashion Watches , Wristwatch, Fake Watch, Wholesale Watches, Jewelry Watches, Replica Jewelry Watches. How to choose Sales Replica Watches the perfect Rolex Watch IWC Fake Watch for you. Buying a Rolex Jewelery Watches watch is certainly a large Jacob & Co. Watch Replica investment, as Rolex Replica Watches watches certainly cost Emporio Armani Replica Watches more than the average wristwatch. Most Rolex watches such as the Rolex yacht master, cost Replica Watch thousands of dollars. Chopard Watch Replica However, Replica Breguet Watches be aware Fake Franck Muller that you will be buying a watch of TAG Heuer Watches that will not Replica Watches ,Jaeger LeCoultre Replica Watches only Ebel Watches Replica work your whole lifetime, it will also look pretty Replica Watch good doing it. However, a Zenith Replica new Replica Mont Blanc Watches Rolex watch may simply be too much money for some people. Wholesale Replica Watches If that is Replica Bvlgari the Replica Handbag case, Jacob & Co. Watch Replica consider buying a Ebel Wristwatch pre-owned Rolex watch Luxury Vacheron Constantin instead. Whether you buy a new or used Rolex watch, you will definitely enjoy the Rado Replica Watches use of Patek Philippe Mens Watches a finely made watch made by the Fake Chopard Watches finest watch company in the last one hundred Replica Watch years. A Gucci Replica Watch Rolex, watch, Replica Patek Philippe Watches omega or cartier wristwatch Luxury Audemars Piguet are all good investments,Ulysse Nardin Watches but the Swiss Luxury Watches Rolex is simply the best Special Watches .

watches brand | Luxury watches | replica hangbag | Luxury watches | replica watch | Very Nice Bag | Real Replica Watches

A Lange&Sohne Watches     Aigner Watches     Alain Silberstein Watches     Audemars Piguet Watches     Bell&Ross Watches     Breguet Watches     Breitling Watches     Bvlgari Watches Replica     Cartier Watches     Chanel Watches Replica     Chopard Watches Replica     Concord Watches     Corum Watches     D&G Watches     Dior Watches Replica     Ebel Replica Watches     Emporio Armani Watches     Ferrari Watches     Franck Muller Watches     Glashutte Watches     Gucci Watches     Hermes Watches     Hublot Watches     IWC Watches     Jacob&Co Watches     Jaeger LeCoultre watches     Longines Watches     Louis Vuitton Watches     Michele Watches     Montblanc Watches     Movado Watches     Omega Watches     Oris Watches     Panerai Watches Replica     Patek Philippe Watches     Philip Stein Watches     Piaget Watches     Porsche Design Watches     Rado Replica Watches     Raymond Weil Watches     Richard Mille Watches     Roger Dubuis Watches     Rolex Japan Watches     Rolex Swiss Watches     Sarcar Watches     Swiss Watch International     TAG Heuer Watches     Tissot Watches     Tudor Watches     Ulysse Nardin Watches     Vacheron Constantin Watches     Versace Replica Watches     Victorinox Swiss Watches     Zenith Watches  

Balenciaga ;   Bally     Burberry     Chanel     Chloe     Christian Dior     Coach     D&G     Fendi     Gucci Handbags     Hermes     Jimmy Choo Handbags     Loewe Handbags     Louis Vuitton Bags     Marc Jacobs Handbags     Miu Miu Handbags     Mulberry Handbags     Prada     Thomas Wylde Handbags     Versace Handbags     YSL Handbag  

A Lange&Sohne Watches     Aigner Watches     Alain Silberstein Watches     Audemars Piguet Watches     Bell&Ross Watches     Breguet Watches     Breitling Watches     Bvlgari Watches     Cartier Watches     Chanel Watches     Chopard Watches     Concord Watches     Corum Watches     Dior Watches     Dolce&Gabbana Watches     Ebel Watches     Emporio Armani Watches     Ferrari Watches     Franck Muller Watches     Glashutte Watches     Gucci Watches     Hermes Watches     Hublot Watches     IWC Watches     Jacob&Co Watches     Jaeger LeCoultre watches     Longines Watches     Louis Vuitton Watches     Michele Watches     Mont Blanc Watches     Movado Watches     Omega Watches     Oris Watches     Panerai Watches     Patek Philippe Watches     Philip Stein Watches     Piaget Watches     Porsche Design Watches     Rado Watches     Raymond Weil Watches     Richard Mille Watches     Roger Dubuis Watches     Rolex Japan Watches     Rolex Swiss Watches     Sarcar Watches     Swiss Watch International     TAG Heuer Watches     Tissot Watches     Tudor Watches     Ulysse Nardin Watches     Vacheron Constantin Watches     Versace Watches     Victorinox Swiss Watches     Zenith Watches  


2/1/2010 8:04:21 PM


Name: *
Email (not displayed): *
Url:
Comments: *



So, you Human...?



Submit this story to DotNetKicks
Shout it