The Joys of Object Oriented Design

Today I helped to interview a candidate for a senior development role, and the subject of object oriented design came up. There are some standard topics such as┬áLiskov’s Substitution Principle.

The way I’d think of describing this if asked is that if A declares that it is a B then anywhere I expect B I can substitute an A, so if “Volkswagen Golf Gti” is a type of “Car” then anywhere I expect “Car” I can use “Volkswagen Golf Gti”.

Intuitively this seems a little wrong. What if I want a “Fast Car”? Is “Fast” a type of car or a property? Maybe there’s “Sports Car”. Maybe the Jeremy Clarkson class would declare that it needs a Sports Car, but then he’s not a class but an instance. Even if “Petrol Head” was a class, then most modern programming languages won’t let you override the Car property of the Person base class with a different type. He’ll just have to throw a CarNotFastEnoughException or something.

If Fast Car is a property then how do we describe it? Maybe we could specify horse power, or perhaps maximum acceleration, or a relationship between speed and maximum acceleration. What if you really want to tow a caravan? Then you’re interested in pulling power, so Torque?

Would “Car” have Torque, or for that matter Horse Power? What if it’s a new fangled direct drive electric car, with plate motors in each wheel? Maybe such a Car could provide meaningful numbers for “The Torque I would have had if I had an engine and gearbox and normal drive train”. Properties such as Torque expose a little bit about the internal workings of the Car. That knowledge becomes coupled into the rest of the system so when the electric car comes along it’s hard to change.

Maybe a better approach would be to expose the force characteristics of the towing hitch both horizontally and vertically, or include information about load when proving the maximum acceleration figures. This information is valid for  the engined car, the direct drive electric car and the jet powered car (with molten caravan). It also places the knowledge of how to calculate things like the maximum acceleration from 30mph towing 1.5 tons up a 10% gradient in the Car class where it really belongs.

So Object Oriented Design does need a little bit of thinking about.

This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *