The OO Beer Case Analogy

Sometime ago I had a email conversation with a newbie programmer.  The topic of the conversation eventually led to Object Oriented programming.  Having done a fair amount of teaching over the years I have always fallen back on the beer bottle, beer case analogy which for some strange reason programmers, who are majority male, seem to get instantly.  Given this I thought it would be kind of neat to throw it out to the world with great trepidation.  Remember that this analogy is to explain the basic principles for a newbie trying to get to grips with the concept which may seem obvious and natural to many of us but to newbies is certainly not.  So without further adue ‘The  OO Beer Case’ directly from the email exchange:

Object Oriented programming is a conceptual model that can be applied to ANY language that supports it. Therefore regardless of the language you learn OO in you can apply it. OO is in essence very simple at its core and I shall give you a real world example I often use when I have taught. Imagine that your object is a case of beer. The beer case has many properties, such as color, width, height etc. It also has several methods, like open case, close case or getCountofBeer. Now we have a class called beer. We create many instances/copies of beer and then call the method on beer case called add and we pass that method a beer object. This results in the beer being added to the case. The beer of course has properties like width, height, weight, color etc and methods such as open. Now since a beer is based on a base class that we create instances of, if I ‘prototype’ a method or property into that beer base class then ALL existing beers that have already been created automatically get that new method or property we added to the base class. So from a OO perspective the beer case object ‘inherits’ X number of beer objects. Now suppose your fridge is an object and it can inherit multiple beer case objects. So in coding terms you could have this:

var fr = new Fridge()       // create a fridge
var bc = new BeerCase()     // create a beer case
var br = new Beer()         // create a beer
bc.add(br)                  // add the beer to the beer case
fr.add(br)                  // put the beer case in the fridge
fr.open()                   // open the fridge
// Get beer case number 1 from the fridge and call the beer case method get beer 
// and get the first beer and call the method open on the beer.
// This returns beer liquid to the variable getMeABeer
var getMeABeer = fr.beerCases[0].getBeer[0].open()
getMeABeer.drinkIt()             // drink the dam beer

This is the essence of OO and the above example is JS syntax.

So there you have it, the OO Beer Case analogy. Hope you enjoyed it.

Cheers
Keith

About these ads

One response to “The OO Beer Case Analogy

  1. Great analogy ;) Not very ‘deep’, but I feel it gets across the whole OOP idea, the problem I think is analogy’s are one thing but actually using OOP in your programming is a concept students/new programmers struggle with.

    It comes down to ‘how does this help me?’, if they can make something non OOP, and feel they are saving time, they will. On the other hand future projects might call for OOP structure, and if not initially created, would mean for longer update period later.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s