Thursday, August 8, 2013

Object oriented design principles



Many people have given many thoughts on achieving good object oriented design and basic principles for doing object oriented design. There are many design principles out there. At the basic level there are five basic design principles which are abbreviated as the SOLID principles (Uncle Bob - Robert C. Martin)

o   S-Single Responsibility principle
o   O-Opened Closed Principle
o   L-Liscov Substitution Principle
o   I-Interface Segregation Principle
o   D-Dependency Inversion Principle

Single Responsibility principle –SRP
A class should have one and only one responsibility. Also we can say it as a class should have only one reason to change.
 If a class has more responsibilities then the class can have more than one reason to change.

This does not mean that we can’t have multiple methods in one class. A class surely can have multiple methods. But they have to meet a single purpose. This is a very important thing.You must understand the difference.

Let’s see why this is important and why we should not violate this principle.

                As I said earlier when class have more responsibilities there can be more reasons to change , so more changes can be expected and more changes can lead to bugs and increase complexity of the code. Also change of one responsibility can have impact on other responsibilities.

I will explain this through a well-known example .Then you can understand this clearly. 


The Rectangle class has two responsibilities .One to draw a rectangle and the other for calculating the value of the rectangular area.                                     
As in the picture,             
o   Computational geometric application uses Rectangle to calculate the area
o   Graphical application uses this Rectangle to draw a rectangle in the area
 
Since the class has two responsibilities it’s violating SRP. This causes several problems.
o   We have to include GUI class library to computational geometric application with no use of it.
o   Also if we change rectangle class for graphical application may lead to a change, recompile ,test  and deploy for the computational geometric application      
o   This increases coupling between two applications

Solution for this
Breaking up rectangle class into two classes
o   Rectangle class – with area() method
o   RectangleUI class which inherited from rectangle class and define draw() method

Now problem solved and we are not going to violate SRP anymore.
SRP is one of the simplest of the principles and the hardest to achieve.



Opened Closed Principle- OCP

"Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"

This simply means the class /module/ object can’t modify directly but can extend for changes .The behavior of the class can be extended. When extending it should not change or modify old source code.

According to the OCP the code should be easily extensible but it should not do any change in the core implementation.

I will explain this through a real example .Then you can easily understand this principle
Think a carpenter makes a wooden chair for a customer .After few months the customer wants to change that chair for more comfortable manner with a cushion cover.

Carpenter can do this in two ways
1.       Reduce the height and change the shape of the chair .After those modifications put a cushion cover to fix with new shape and height.
a.       In this way he is violating open closed principle by changing the old chair

2.       Create a cushion cover to fix the old chair’s measurements and put it on the chair
a.       In this way he is not violating the principle .He is doing right way

You can see the below code to understand it as software product






class CushionChair :Chair
{
      String color;
      Void PutCover()
      {
            //taking properties of chair and make a cover and put it on the chair
      }
}

Also you can achieve OCP by creating abstract class for chair .Abstract class should be closed for modification and open for extensions. You can do modification on the implementation of concrete class .So the client code can call directly abstract class and when a change comes client code does not need to change anything.

The idea is once we have completed creation of class the implementation code should only be modify for correcting bugs, faults. If new or changed feature require we have to create a new class, that class can use old code through inheriting it.

                Since this post has too much already I will explain next three basic OOD principles in my next blog post.

Hope you can understand SRP and OCP by reading this blog post. This is my first blog post and you are most welcome to comment out regarding this blog post.

                                  

5 comments:

  1. Great work, most developers don't use advantage of Solid principles,I think ,this will help those who no idea of Solid principle.

    ReplyDelete
  2. Thankyou very much nalin and jayanath

    ReplyDelete
  3. Good article with a simple example! Thanks for sharing :)

    ReplyDelete
  4. good post cleanup most developers code

    ReplyDelete