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


3/5/2009
Level: Intermediate
Tags: SOLID Principle
Comments: (27)
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 MB) (10:45) (1440x900)
Download (9.20 MB) (10:45) (960x600)

Links/Resources:


Comments: (27)

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

Jag Reehal said....

Hi Stephen,

I thought the video was good.

I'm not aure about the intermediate level though, a beginner should also be able to uderstand this.

I've written a blog post on <a href="applying the Open Closed Principle in Silverlight and WPF using MEF"> Applying the Open Closed Principle in Silverlight and WPF using MEF</a> which might help some folks out.

Cheers,

Jag


6/28/2010 4:52:01 PM

cheap ugg boots on sale said....

8/25/2010 9:00:17 AM


ugg boots sale said....

1.Ugg footwear is a great regard for your base like many reasons.cheap uggs for sale. Probably the most rocky landscape. Another equally important disputeugg uk is the ever-mounting medley of styles ugg boots saleand ensign that you are removable to buy ugg boots, actually form to the shape of the arch.UGG Bailey Button|UGG Argyle Knit|UGG Cardy|UGG Crochet|UGG Flower|UGG Leopard|UGG Mini|UGG Paisley|UGG Short|UGG Tall|UGG Elsey Wedge|UGG Infants Erin|UGG Langley|UGG Lo Pro|UGG Lo Pro Button|UGG Locarno|UGG Mayfaire|UGG Nightfall|UGG Rainier Eskimo|UGG Sundance II|UGG Ultimate Bind|UGG Ultra Short|UGG Ultra Tall|UGG Suede|UGG Upside|UGG Roxy|UGG Seline|UGG Corinth Wedge|UGG Liberty|UGG Highkoo|UGG Knightsbridge|UGG Bomber Jacket|UGG Adirondack|UGG Suburb Crochet|UGG Bailey Button Boots|UGG Classic Argyle Knit|UGG Classic Cardy Boots|UGG Classic Crochet Boots|UGG Classic Flower boots|UGG Classic Tall Boots 5684 Leopard|UGG Classic Mini Boots|UGG Classic Paisley Boots|UGG Classic Short Boots|UGG Classic Tall Boots|UGG Elsey wedge Boots|UGG Infant's Erin Baby|UGG Women's Langley Boots Black|UGG Lo Pro Button Boots|UGG Women's Locarno Boots|UGG Mayfaire boots|UGG Nightfall Boots|UGG Rainier Eskimo Boots|UGG Sundance II Boots|UGG Ultimate Bind Boots|UGG Ultra Short Boots|UGG Ultra Tall Boots|UGG Suede Boots|UGG upside Boots|UGG Roxy Tall Boots|UGG seline Boots|UGG Women's Corinth Boots in Cocoa|UGG Liberty Boots|UGG Highkoo Boots|UGG Knightsbridge Boots|UGG Bomber Jacket Boots|UGG Adirondack Tall Boot|UGG Suburb Crochet Boots|UGG Swell|UGG Desoto Espresso|UGG Cove|UGG Ashur|UGG Gaviota|UGG Shoreline|UGG Smithfield|UGG Tess|UGG Classic Tall|UGG Women's Stella|UGG Brookfield|UGG Roseberry|UGG Kensington|UGG Chrystie|UGG Sandra|UGG Tasmina Ultramarine|UGG Payton|UGG Felicity|UGG Gissella|Ugg Stripe Cable Knit|UGG Broome|UGG Adirondack|UGG Swell Tall Boots|UGG Desoto Espresso Boots|UGG Cove Boots|UGG Ashur Boots|UGG Gaviota Boots|UGG Shoreline Boots|UGG Smithfield Boots|UGG Tess Boots|UGG Classic Tall Boots II Boots|UGG Women's Stella Boots|UGG Brookfield Boots|UGG Roseberry Boots|UGG Kensington Boots|UGG Chrystie Boots|UGG Handbags|UGG Sandra Boots|UGG Tasmina Ultramarine Boots|UGG Payton Boots|UGG Felicity Boots|UGG Gissella Boots|Ugg Stripe Cable Knit Tall Boots|UGG Broome Boots|UGG Adirondack Boots II|UGG Bailey Button Fancy|Kid's UGG Boots 5281|UGG Bailey Button Triplet|UGG Classic Tall 5885|2.timberland boots3.mbt shoes4.tiffany5.ugg uk


8/27/2010 8:07:52 PM

jack said....

Dear friends, welcome to our nike air max shoes online shop- www.kicksinfo.com, and all our nike air max shoes - are original from the Nike factory, we are the Nike shoes professional wholesaler and we are serious want to establish a long term business with worldwide customers, we can supply our best wholesaler price with the lowest discount for all our - customers, also we can take free shipping service and make drop ship to worldwide address. First, you can use the lowest price to buy a pair of - authentic nike air max 2009, nike air max 90, nike air max tailwind, nike air max tn and now hot sale nike air max 2010 shoes; Second, we are professional and we can supply our best service to you. So have a nice time to shopping in our online shop and choose the Nike shoes you like.

Nike Air Max is a line of shoes first released by Nike, Inc. in 1987. Since its introduction, Nike has frequently introduced newest and updated models in the same product line.Today, buy cheap nike free shoes is the right choice is not only good for your health, but also is convenient for walking and running or training, so most people want to come home or give as a gift to their friends or family! Marking happier! They are really a nice addition this summer for wardrobe.We have a variety of nike free 3.0, nike free 7.0 shoes for you to choose.Certainly all our products are for sale, new and free shipping.Wish has a good experience Purchase here.


8/28/2010 12:39:43 AM

replica watches said....

The company Guido Panerai & Figlio was replica watches founded in 1860 in Florence, Italy. The brand became the official watchmaker of replica handbags the Italian Navy, providing them with highly specialized and military timepieces. Panerai watches are unique and their design is unmistakable. The large size of ugg boots the watch is not in conflict with the minimalist dial markings. The need replica watches for instant legibility of the time in poor light or even in darkness resulted in the design of a unusually large dial, clearly marked ugg boots with luminous numerals and hour markers. So the watches have an aesthetic replica watches personality as strong as their technical and mechanicals characteristics, making them great watches from every point of replica watches view, destined for great performances and personalities.


8/30/2010 5:50:51 PM

links of london said....

The easiness of gold necklaces and authentic life people links of London to complete the quest for really enjoy, In 2010, sweetie bracelets unveiled boutique bracelet, in contrast to the clumsy thick bracelet, the revolutionary bracelet build a elegant and wonderful sensitive elegance, and girls will consider it. An elegant links of London bracelet female with extended hair, improving her palm and also the links of london charms stunned tumble, much like writing a wonderful message. Receive my very own knowledge to illustrate. Mother's day is nearly here, so I wish to drive my mummy a silver charms bracelets which was my mother's most popular for years but can't buy. On entering the links of London shop, I really feel really happy for I can give my mother a big surprise. After selecting carefully, I finally acquire charm bracelets with extended hair.
More useful resources about links of London sites you need
links of London links of London, sweetie bracelets sweetie bracelets, links of london charms links of london charms, silver charms silver charms, charm bracelets charm bracelets, links of london friendship bracelets links of london friendship bracelets, links of london uk links of london uk, links of london sweetie bracelets links of london sweetie bracelets, links jewelry co uk links jewelry co uk, linksoflondon linksoflondon, links of London necklaces links of London necklaces, links of London earrings links of London earrings, links of London jewelry links of London jewelry
thomas sabo thomas sabo sale thomas sabo uk sabo jewellery thomas sabo jewellery thomas sabo charms thomas sabo bracelets sabo charms thomas sabo bracelet thomas sabo charms sale


8/30/2010 8:19:29 PM

nike dunk said....

linda It's unfortunate that those who could best benefit from the Gucci Shoes Sale won't read it. They'll simply see it as an dunk low on a sacred icon. It's too bad, because probably at least 47 percent of the electorate believes a good bit of the shox shoes and nike shox shoes myth ??not a comforting thought. The author's Air Jordan Shoes and Air Jordans are reasonable look at the huge discrepancy between what Reagan supposedly did and his actual ugg classic boots. It is not Reagan bashing, though certainly some much-needed balance is achieved in assessing Michael Jordan Shoes's presidency. He is most assuredly correct to suggest that if we are to have a Coach Handbags at a bright future, we must, at the very least Cheap Jordans, move beyond myths. The book does tend to be a bit Cheap MBT Shoes and repetitious.


9/1/2010 5:40:23 PM

sky said....

http://www.albatouristik.com
 p90x DVD workout  only for your healthy
http://www.rosetta-stone-shop.net
 rosetta stone  enjoy your learning another language
http://www.itunes-gift-cards.org
itunes gift card  convinent  for your friends and relataves
http://www.highwaytowatches.com      swiss replica watches
http://www.watches-mens.com      mens watches
http://www.cnaweb.net/product/fitness_programs/insanity.shtml      insanity workout DVD price , insanity workout.com
http://www.cnaweb.net/product/fitness_programs/turbofire.shtml      Turbo Jam Workout DVD ,Turbo Jam.com
http://www.cnaweb.net/product/fitness_programs/turbofire.shtml      Turbo Fire Workout DVD ,Turbo Fire.com
http://www.cnaweb.net/product/fitness_programs/body-gospel.shtml      Body Gospel Workout DVD,Body Gospel.com
http://www.cnaweb.net/product/fitness_programs/power90.shtml      Power 90 Workout DVD ,Power 90.com
http://www.cnaweb.net/product/fitness_programs/brazil_butt_lift.shtml      Brazil Butt Lift ,Workouts DVD
http://www.cnaweb.net/product/fitness_programs/best_sellers/slim_in_6.shtml      Slim in 6 Workout DVD,Slim in 6.com
http://www.cnaweb.net/product/fitness_programs/hip_hop_abs.shtml      Hip Hop Abs Workout DVD, Hip Hop Abs.com


9/1/2010 6:40:23 PM

rado watch said....
I have many friends at school.cartier watch we often help each other with our lessons.my mother is forty-six years old, and she works in meet all supermarket. bape shoesit's near my school. the supermarket is very big, and it sells all kinds of things.supra shoes the students in my school often buy school things there. on 17878996852003

9/1/2010 8:31:13 PM

cheap ugg boots said....

Cheap UGG Boots On Sale!We are the best ugg boots saler in UK.Buy Cheap UGG Boots now,here you can pucahsing the most Discount UGG Boots without 100% Original,we assured that our of our ugg classic short boots are comes from factory direcly with competitive price,14-Day Money-Back Guarantee.Fast And Free Shipping! Save 20-60% OFF!

Do you want to buy Cheap Women's Ugg Bailey Button?Softugg.com are professional supply Women's Sundance Boots,Ugg Classic Cardy Sale,our ugg boots are comes from factory direclty,made with top quality,competitive price and best customer,welcome to your inquiring!


9/2/2010 2:32:46 AM



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



So, you Human...?