Click here to Skip to main content
11,639,126 members (66,664 online)
Click here to Skip to main content

Liskov principle implementation

, 24 May 2013 CPOL 6.5K 31 2
Rate this:
Please Sign up or sign in to vote.
Simple example of "Liskov principle"

Introduction

Let's assume there is type S which get derived from type T, i.e., type S is derived type of parent type T. Type S contains all [public and protected] functionality of the type T and it contains more functionality of its own.

E.g. you can use all of you parent’s money and you can have you own money.

Then according to Liskov principle object of type T may substitute object of type S without altering any of the desirable properties of that program.

E.g. In the financial deal your money may replace by your parent’s money.

Background

Let me start my story.

Man evolution starts and the first man learns to make fire then learns farming and then starts developing factories that start destroying nature by pollution but there are few men that refuse to learn the evolution of destroying nature, they just learn to fire and farming.

Now I want to make a list of all men and show only those who are bad, i.e., they learn pollution. 

Let’s start implementing our above mentioned scenario.

We have two abstractions: first is Evolution including the good evolution: learn to fire and farming, and second abstraction including all the good evolution plus it has one bad evolution of learning to pollute.

Now consider we have thousands of men out of which a few thousand learn bad evolution as well as good evolution they are of derived type IBadEvolution and refuse to learn the bad evolution, they just learn the evolution they are of base type IEvolution.

Now we want to build a list of all men that learn Evolution of type IEvolution

Then we want to display the list of all men that learn evolution as well as bad evolution from the list of evolution, i.e., substitute IBadEvolution for IEvolution.

Using the code 

Let's build our base type Evolution abstraction:

public interface IEvolution 
{ 
    string Name { get; set; } 
    void LearnToFire(); 
    void LearnToFarming(); 
} 

Let's build our derived type Bad Evolution as an abstraction, it contains all the evolutions as well as bad evolution.

public interface IBadEvolution : IEvolution
{
    void LearToPollution();
} 

For simplicity we just consider one man that learns only good evolution, i.e., refuse to learn bad evolution. 

public class SomeGoodMan:IEvolution
{
    public string Name { get; set; }
    public SomeGoodMan()
    {
        Name = "I am 1st good man ";
    }
    public void LearnToFire()
    {
        Console.WriteLine(Name + "learn to fire.");
    }
    public void LearnToFarming()
    {
        Console.WriteLine(Name + "learn to farming.");
    }
} 

We consider two men that learn good evolution as well as bad evolution.

/// <summary>
///1st bad man
/// </summary>
public class BadMan1:IBadEvolution
{
    public string Name { get; set; }
    public BadMan1()
    {
        Name ="I am First bad man ";
    }
    
    public void LearToPollution()
    {
        Console.WriteLine(Name + "learn to polution.");
    }
    public void LearnToFire()
    {
        Console.WriteLine(Name + "learn to fire.");
    }
    public void LearnToFarming()
    {
        Console.WriteLine(Name + "learn to farming.");
    }

} 
/// <summary>
/// N th bad man
/// </summary>
public class BadManN:IBadEvolution
{
    public string Name { get; set; }
    public BadManN()
    {
        Name = "I am nth bad man ";
    }
    
    public void LearToPollution()
    {
        Console.WriteLine(Name + "learn to polution.");
    }
    public void LearnToFire()
    {
        Console.WriteLine(Name + "learn to fire.");
    }
    public void LearnToFarming()
    {
        Console.WriteLine(Name + "learn to farming.");
    }
}

Let's build the list of persons that learn evolution and by using substitution principle let's display only those that learn bad evolution. 

public class DisplayEvolution
    {
        static void Main(string[] args)
        {
            //Let me add all population of humans to the base 
            List<IEvolution> evolution = new List<IEvolution>();
            evolution.Add(new BadMan1());
            evolution.Add(new BadManN());
            evolution.Add(new SomeGoodMan());

            //Show only the list of bad mans
            //Lets substitube base type for the derived type for lsp
            var listOfBadEvolution = evolution.OfType<IBadEvolution>();
            foreach (IBadEvolution badEvolution in listOfBadEvolution)
            {
                badEvolution.LearnToFire();
                badEvolution.LearnToFarming();
                badEvolution.LearToPollution();
            }
            Console.ReadLine();
        }
    } 

So we end up with our simple example of Liskov principle. 

Points of Interest 

I learned form this exercise that we can avoid the violence of "Liskov Principle" by identifying abstraction in our project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amey K Bhatkar
Web Developer Lionbridge
India India
Amey K Bhatkar, a “Microsoft .Net” Web Developer.
I am programmer by will and profession.
I have completed my MCA in 2011 and join software industry.
Presently I am working with Lion Bridge Technologies in Mumbai - India

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 2 Pin
Oshtri Deka12-Jun-13 0:34
memberOshtri Deka12-Jun-13 0:34 
GeneralMy vote of 2 Pin
Klaus Luedenscheidt24-May-13 18:16
memberKlaus Luedenscheidt24-May-13 18:16 
GeneralMy vote of 2 Pin
Oron Mizrachi16-Apr-13 12:41
memberOron Mizrachi16-Apr-13 12:41 
SuggestionYou did not copy enough text from Wikipedia... Pin
Andreas Gieriet16-Apr-13 9:07
memberAndreas Gieriet16-Apr-13 9:07 
GeneralRe: You did not copy enough text from Wikipedia... Pin
Amey K Bhatkar24-May-13 0:16
professionalAmey K Bhatkar24-May-13 0:16 
QuestionWhere is the Liskov principle demonstration code?? Pin
AdolfoPerez16-Apr-13 8:02
memberAdolfoPerez16-Apr-13 8:02 
AnswerRe: Where is the Liskov principle demonstration code?? Pin
Amey K Bhatkar24-May-13 0:17
professionalAmey K Bhatkar24-May-13 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 24 May 2013
Article Copyright 2013 by Amey K Bhatkar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid