I was doing some code review. I saw something interesting Ā– Liskov
implementation. [for more info, check out
http://en.wikipedia.org/wiki/Liskov_substitution_principle]

Here is the sample implementation of the same.

namespace ToTest
{
    class Liskov
    {
        public Liskov()
        {
            //do nothing
        }

        public virtual int Add(int one,int two)
        {
            return one + two;
        }
    }

    class DerievedLiskov :Liskov
    {
        public DerievedLiskov()
        {
            //do nothing
        }

        /// <summary>
        /// <para>to add the number to itself.For Example - Add(4,0) will
result 8</para>
        /// </summary>
        /// <param name="one"></param>
        /// <param name="two"></param>
        /// <returns></returns>
        public override int Add(int one, int two)
        {
            if (two == 0)
            {
                return one + one;
            }
            else
            {
                return base.Add(one, two);
            }
        }

        public int Add(int one)
        {
            return one + one;
        }
    }
}

Now, I know there is no validation here. But the point is about
substitution principle. Here we have Add method in the base class. An
override in derived class will substitute it.

SO if we want to change the behavior of Add method such that it will
accept only one integer and will return the result as int added to itself
[i.e. 3+3=6],we have two options as coded in the DerievedLiskov. Which one
is beter from oo design point?

Thanks.

Abhi

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to