Sunday, January 29, 2012

Object Oriented Programming

Object Oriented Concepts

 Inheritance

   Creating a class based on an existing base class or super type. As the title suggests, this concept defines reuse or the recreation of objects using an existing class.

In the programming world we realize this by defining a base class that contains a set of shared or common methods and then create subclasses that inherit from this class. These methods and properties are then automatically available in the subclass.

What can we achieve by Inheritance?
The implementation of a common method/methods can be reused or shared among the subclasses from the base class.

 Abstraction

The basic principle behind abstraction is to hide the complex details and keep things simple.


In the programming world we realize this by defining a base abstract class that contains a method/methods with or without concrete  implementations.The subclasses that extend this abstract class may or may not in turn implement these methods.

What can we achieve by Abstraction?
As you will read later below, one of the principles of object oriented principles is to code to an interface rather than an implementation. Abstraction helps in coding to a interface or a super type giving you the flexibility to change the implementation at a later stage.


 Polymorphism

 Polymorphisms is a generic term that means 'many shapes'. In OOP's polymorphism is the ability to be able define multiple implementations for the same method call.

In the programming world we realize this by defining a common interface across various concrete class with different implementations.

Ad-hoc polymorphism: polymorphism is not the same as method overloading or method overriding 

What can we achieve by Polymorphism?
We can achieve interchangeability. That is we can easily change the behavior of a given method without the need to create a new type.

Encapsulation

Simply means hiding information that does not need to be seen by others. An analogy can be made here with the notion of a capsule, which not only encloses its contents, but also protects it from the exterior environment.

In the programming world we realize this by the use of access modifiers. For example we can make the methods of  a class private thus making its implementation in accessible to other objects.

What can we achieve by Encapsulation?
We can reduce the dependency of objects on each other. For example we can prevent users from setting the internal data of the component into an invalid or inconsistent state.


Object oriented principles

1) Code to an interface rather than to an implementation
2) Favor composition over inheritance
3) Encapsulate what changes or varies



Reference

http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
http://www.my-project-management-expert.com/definition-object-oriented-programming.html


Saturday, January 28, 2012

The startegy pattern

Definition: The Strategy pattern defines a family of algorithms whose individual behaviors are encapsulated allowing them to be interchangeable. Strategy lets the algorithm vary independently from clients that use it.




The above example illustrates the strategy pattern. The client in this case Jetplane is a subclass of type Plane. The Jetplane uses the flight algorithms Jetflight. Each flight algorithm is encapsulated in its own concrete class. The jet plane could very easily use a different flight algorithm making them  interchangeable.

Thursday, January 19, 2012

Design Patterns

Ive been saying to myself that I will set aside time, to write a tutorial on design patterns. I think that time has come and here is what I have come up with.

What are design patterns?

In the object oriented world the solutions/designs we create to overcome problems or requirements have probably been done before. Over time these solutions/designs have evolved into repeatable set of patterns. Some of these patterns have been identified and classified into one of the three types of design patterns.

However there is no hard and fast rules for these patterns and you might have already been using this patterns unconsciously. However there are a few patterns that are well documented and widely used. Even these patterns might have several variants and be customiz3d to solve specific problems.


What are the types of design patterns? How man are there in total?

There are three main types of design patterns. There are several design patterns and their variants in use, here are a few that have been well documented and frequently used.

Creational Patterns
Abstract Factory
Builder
Factory Method
Prototype
Singleton

Structural Patterns
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy

Behavioral Patterns
Chain of Resp.
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor

Why do we need design patterns? Are they really useful?

Design patterns help implement the object oriented concepts. They also provide us with a standard set of available and well researched solutions to familiar software design problems.

"Are they really useful" is a hard question to answer and also quite opinionated, but My understanding is Yes they are useful. My justification for this is, by implementing design patterns in the long run you will create code that is standardized and understandable by all. Further there is no point in re inventing the wheel, especially with less efficient code and designs.

How do I know when to apply which pattern?

Unfortunately like all things abstract, this is the hardest to master. We can only learn when to apply a certain design pattern from experience and prior learning. As mentioned before there is no limitations to the number of patterns. There are a number of variants of any given pattern and every solution would require some modifications to even a well defined pattern.

However one method I have used to learn "When to use a pattern" is by looking at code written by others. Try and identify what patterns they have used to solve a particular design problem, this is your best bet in learning when to use a particular design pattern.