Monday, January 6, 2014

Object oriented design principles II

Hi all,Sorry I could not continue my blog post for a long time since I was too busy.So today I got a holiday and I thought to continue my writing .
I have explained about two basic object oriented principles in my first blog post.Hope you could understand both principles well.Now I am going to explain about a remaining  principle I have mentioned on my earlier post .

Liskov Substitution Principle -LSP
Objects in program module should be replaceable with instance of their sub-types without altering the correctness of that program.
  
Derived types must be completely substitutable  for their sub-types .


When designing and developing a program module we always follow basic OOP concepts as much as possible.Every time we design a program we create a class hierarchy for the module.That means we use inheritance (parent child relationships) .So we create Parent classes , sub classes to do our functions.

In liskov substitution principle says if a program module use a base class ,then the reference to a base class (instance of base class) can be replaceable with a instance of sub class without effecting the functionality of the program. In simply it says object instance of base class should be replaceable with its a instance of sub-type without doing any changes .

I will explain this with a well known example .This explains violation of liskov substitution principle.

There are two classes called Rectangle and Square.Square is a sub class of Rectangle .So its inherited from Rectangle.And there is an another class called RectangleFactory. There is a method to returning a Rectangle object.As u can see we are going to use Factory design pattern here.


class Rectangle
    {
        protected int bWidth;
        protected int bHeight;
        public void setWidth(int width)
        {
            bWidth = width;
        }

        public void setHeight(int height)
        {
            bHeight = height;
        }

        public int getWidth()
        {
            return bWidth;
        }

        public int getHeight()
        {
            return bHeight;
        }

        public int getArea()
        {
            return bWidth * bHeight;
        }
    }

    class Square : Rectangle
    {
        //In sqare width=height
        public void setWidth(int width)
        {
            bWidth = width;
            bHeight = width;
        }

        public void setHeight(int height)
        {
            bHeight = height;
            bWidth = height;
        }
    }

    static class RectangleFactory
    {
        public static Rectangle getRectangle()
        {
            return new Square();
        }
    }

    //Test class for testing the example

    class Test
    {
        public void testMethod()
        {
            Rectangle rec = RectangleFactory.getRectangle();
            rec.setHeight(5);
            rec.setWidth(10);
            int area = rec.getArea();
            Console.WriteLine("Area of rectangle is " + area);
        }
    }

From this program you will expect answer as "Area of rectangle is 50" .But this will give you 100 instead of 50.That means this will violate LSP. Because Square is a sub-type of Rectangle but its not a replaceable with rectangle object without changing the code.
As LSP says,
"we must make sure that new derived classes are extending the base classes without changing their behavior"

But in above program we changed the setWidth() and setHeight() methods in sub class. That's why we are getting wrong result and violate Lisskov substitution principle.

I hope you guys understand the idea behind liskov substitution principle And i hope u will think about this principle when you going to develop a system.You have to try  not to violate these principles.Its not a must but its a best practice in object oriented programming.




5 comments: