Monday, June 29, 2009

The three pillars of Object Oriented Design (easily defined)

The root to understanding Objected Oriented Design is grasping the fundamentals of three concepts: Inheritance, Encapsulation, and Polymorphism. Typically I hate reading definitions and go straight to analyzing code, but I have found that when it comes to understanding Object Oriented Design, that it makes it much easier to really understand the definitions behind the concepts before writing any code. Regardless what language you are coding in, the concept of Object Oriented Design is the same.

And believe it or not, finding clear and understandable definitions for some of these was a real pain, as it seemed like every author has their own way of defining them, and some were better at it than others, and ironing out their definitions is what really set the wheels in motion for starting this blog.

Inheritance, simply put, enables one "thing" to inherit from another "thing". In programming, we are specifically refering to classes, which I will define in another post. For now, understand that inheritance involves using something as a blueprint that can be used by something else (such as a builder using a blueprint for a house to build a house).

Encapsulation is more tricky to explain. A good text book explaination would be that Encapsulation involves a single object that contains both its data and behaviors and can hide what it wants from other objects. Well what does that mean? Think of driving a car: When we push down on the accelerator, the car goes faster. When we hit the break, it stops. We aren't concerned with the actual mechanisms that make the car go faster, nor break, but we know how to get the car to do both. In programming terms, we can hide those mechanisms from other objects, and just give them the option to move faster or stop, without them being able to see how either are being done.

There are various was to do this, and some good words to associate with this concept for now would be public, private, get, and set, all which you may have seen by now. We'll come back to those later.

Polymorphism is probably the hardest Object Oriented Design concept I ever tried to dig up a clear and consise defintion for, and I am not talking about the Greek definition of it. In reading a dozen C# books, I've only come by one that explained it as well as this: Polymorphism enables an object to perform an operation the way that it wants to. For example, all vehicles have a method for starting their engine, but a car starts differently than a rocket. Both are vehicles, but perform the start action in a different way. A key term associated with Polymorphism is Overloading, which we'll get to later.

And those three are the big ones. Get a basic understanding of them, then expand your knowledge of them, and you'll have a great start understanding Object Oriented Design.

1 comment: