Open In App

C# – IDumpable Interface

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to implement IDumpable interface through C#. The IDumpable interface is just a simple interface which has a Dump() method and public property. Every class that wishes to implement the IDumpable interface has to implement the Dump() method and can make use of the public property of interface to manage the execution of the code.

Let’s understand more in detail with a real-time example.

Let’s say you are driving a car and you would like to give a command in order to know whether a car needs to drive on Petrol/Diesel/CNG/Electricity. Here we will use the Dump() method to decide based on what command we would like to drive a car. Command will be given at run time through public interface property. 

First, we will be defining Enums for different commands. DriveCommand enum will have 4 commands defined on which car will drive.

internal enum DriveCommand
{ 
     PETROL, 
     DIESEL, 
     CNG, 
     ELECTRIC 
}

Now let us define our IDumpable interface which will consist of the Dump() method and a public property called Command of type DriveCommand enum.

public interface IDumpable
{
    DriveCommand Command 
    { 
       get; 
       set; 
    }
    void Dump();
}

Now, let us create Car classes and inherit the IDumpable interface.

public class BMW : IDumpable
{
    private DriveCommand _command;
    private int _speed;

    public DriveCommand Command
    {
        get { return _command; }
        set { _command = value; }
    }

    public BMW(int Speed)
    {
        this._speed = Speed;
    }

    public void Dump()
    {
        Console.WriteLine("I AM DRIVING BMW");
        Console.WriteLine("Command : " + _command);

        if (_command == DriveCommand.PETROL)
        {
            Console.WriteLine("BMW IS NOW DRIVING ON " + _command +
             " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
        }
        else
        {
             Console.WriteLine("BMW IS NOT COMPATIBLE
              TO DRIVE ON " + _command + "\n");
        }
    }
}

The BMW class consists of 2 private variables.

  1. _command : This is going to be a command given by the user to class BMW.
  2. _speed : Which is assigned via a parameterized constructor. This is going to be the speed at which Car will drive.

The Dump() method consists of our main logic to decide how the car will drive. As BMW is only driving on Petrol, it will check whether the given command is Petrol or not. If the command is Petrol, the method will prompt a message that the car is driving on Petrol at a given speed. But if the command is not petrol, then the method will prompt a message that the Car is not compatible to drive on the given command.

Let’s add a few more car classes with different driving modes.

public class TRUCK : IDumpable
{
      private DriveCommand _command;
      private int _speed;

      public DriveCommand Command
      {
          get { return _command; }
          set { _command = value; }
      }

      public TRUCK(int Speed)
      {
          this._speed = Speed;
      }

      public void Dump()
      {
          Console.WriteLine("I AM DRIVING TRUCK");
          Console.WriteLine("Command : " + _command);

          if (_command == DriveCommand.DIESEL)
          {
              Console.WriteLine("TRUCK IS NOW DRIVING ON " + 
              _command + " WITH SPEED OF " + _speed.ToString() 
              + " KM/HR" + "\n");
          }
          else
          {
              Console.WriteLine("TRUCK IS NOT COMPATIBLE 
              TO DRIVE ON " + _command + "\n");
          }
      }
 }

public class TUCSON : IDumpable
{
      private DriveCommand _command;
      private int _speed;

      public DriveCommand Command
      {
          get { return _command; }
          set { _command = value; }
      }

      public TUCSON(int Speed)
      {
          this._speed = Speed;
      }

      public void Dump()
      {
          Console.WriteLine("I AM DRIVING TUCSON");
          Console.WriteLine("Command : " + _command);

          if (_command == DriveCommand.PETROL 
          || _command == DriveCommand.ELECTRIC)
          {
              Console.WriteLine("TUCSON IS NOW DRIVING ON " + _command 
              + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
          }
          else
          {
              Console.WriteLine("TUCSON IS NOT 
              COMPATIBLE TO DRIVE ON " + _command + "\n");
          }
      }
}

public class WAGONR : IDumpable
{
      private DriveCommand _command;
      private int _speed;

      public DriveCommand Command
      {
          get { return _command; }
          set { _command = value; }
      }

      public WAGONR(int Speed)
      {
          this._speed = Speed;
      }

      public void Dump()
      {
          Console.WriteLine("I AM DRIVING WAGONR");
          Console.WriteLine("Command : " + _command);

          if (_command == DriveCommand.PETROL || _command == DriveCommand.CNG)
          {
              Console.WriteLine("WAGONR IS NOW DRIVING ON " + _command 
              + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
          }
          else
          {
              Console.WriteLine("WAGONR IS NOT
               COMPATIBLE TO DRIVE ON " + _command + "\n");
          }
      }
}

Let’s summarize all our classes and on what command they can run.

  • Class BMW: Petrol
  • Class Truck: Diesel
  • Class Tucson: Petrol & Electric
  • Class WagonR: Petrol & CNG

Now, let us call each Car’s Dump() method and see how it works. Before we call Dump() method, we will be passing speed to via parameterized constructor and Command to public property of interface.

static void Main(string[] args)
{
     IDumpable Car = new BMW(90);

     Car.Command = DriveCommand.PETROL;
     Car.Dump();

     Car.Command = DriveCommand.DIESEL;
     Car.Dump();

     Car = new TRUCK(100);

     Car.Command = DriveCommand.DIESEL;
     Car.Dump();

     Car = new TUCSON(80);

     Car.Command = DriveCommand.PETROL;
     Car.Dump();

     Car.Command = DriveCommand.ELECTRIC;
     Car.Dump();

     Car = new WAGONR(50);

     Car.Command = DriveCommand.CNG;
     Car.Dump();

     Console.Read();
}

Example:

C#




using System;
 
namespace Demo {
internal class Program {
    internal enum DriveCommand {
        PETROL,
        DIESEL,
        CNG,
        ELECTRIC
    }
 
    public interface IDumpable {
        DriveCommand Command
        {
            get;
            set;
        }
        void Dump();
    }
 
    public class BMW : IDumpable {
        private DriveCommand _command;
        private int _speed;
 
        public DriveCommand Command
        {
            get { return _command; }
            set { _command = value; }
        }
 
        public BMW(int Speed) { this._speed = Speed; }
 
        public void Dump()
        {
            Console.WriteLine("I AM DRIVING BMW");
            Console.WriteLine("Command : " + _command);
 
            if (_command == DriveCommand.PETROL) {
                Console.WriteLine(
                    "BMW IS NOW DRIVING ON " + _command
                    + " WITH SPEED OF " + _speed.ToString()
                    + " KM/HR"
                    + "\n");
            }
            else {
                    Console.WriteLine("BMW IS NOT COMPATIBLE TO
                                      DRIVE ON " + _command + "\n");
            }
        }
    }
 
    public class TRUCK : IDumpable {
        private DriveCommand _command;
        private int _speed;
 
        public DriveCommand Command
        {
            get { return _command; }
            set { _command = value; }
        }
 
        public TRUCK(int Speed) { this._speed = Speed; }
 
        public void Dump()
        {
            Console.WriteLine("I AM DRIVING TRUCK");
            Console.WriteLine("Command : " + _command);
 
            if (_command == DriveCommand.DIESEL) {
                Console.WriteLine(
                    "TRUCK IS NOW DRIVING ON " + _command
                    + " WITH SPEED OF " + _speed.ToString()
                    + " KM/HR"
                    + "\n");
            }
            else {
                Console.WriteLine(
                    "TRUCK IS NOT COMPATIBLE TO DRIVE ON "
                    + _command + "\n");
            }
        }
    }
 
    public class TUCSON : IDumpable {
        private DriveCommand _command;
        private int _speed;
 
        public DriveCommand Command
        {
            get { return _command; }
            set { _command = value; }
        }
 
        public TUCSON(int Speed) { this._speed = Speed; }
 
        public void Dump()
        {
            Console.WriteLine("I AM DRIVING TUCSON");
            Console.WriteLine("Command : " + _command);
 
            if (_command == DriveCommand.PETROL
                || _command == DriveCommand.ELECTRIC) {
                Console.WriteLine(
                    "TUCSON IS NOW DRIVING ON " + _command
                    + " WITH SPEED OF " + _speed.ToString()
                    + " KM/HR"
                    + "\n");
            }
            else {
                    Console.WriteLine("TUCSON IS NOT COMPATIBLE TO DRIVE
                                      ON " + _command + "\n");
            }
        }
    }
 
    public class WAGONR : IDumpable {
        private DriveCommand _command;
        private int _speed;
 
        public DriveCommand Command
        {
            get { return _command; }
            set { _command = value; }
        }
 
        public WAGONR(int Speed) { this._speed = Speed; }
 
        public void Dump()
        {
            Console.WriteLine("I AM DRIVING WAGONR");
            Console.WriteLine("Command : " + _command);
 
            if (_command == DriveCommand.PETROL
                || _command == DriveCommand.CNG) {
                Console.WriteLine(
                    "WAGONR IS NOW DRIVING ON " + _command
                    + " WITH SPEED OF " + _speed.ToString()
                    + " KM/HR"
                    + "\n");
            }
            else {
                    Console.WriteLine("WAGONR IS NOT COMPATIBLE TO DRIVE
                                      ON " + _command + "\n");
            }
        }
    }
 
    static void Main(string[] args)
    {
        IDumpable Car = new BMW(90);
 
        Car.Command = DriveCommand.PETROL;
        Car.Dump();
 
        Car.Command = DriveCommand.DIESEL;
        Car.Dump();
 
        Car = new TRUCK(100);
 
        Car.Command = DriveCommand.DIESEL;
        Car.Dump();
 
        Car = new TUCSON(80);
 
        Car.Command = DriveCommand.PETROL;
        Car.Dump();
 
        Car.Command = DriveCommand.ELECTRIC;
        Car.Dump();
 
        Car = new WAGONR(50);
 
        Car.Command = DriveCommand.CNG;
        Car.Dump();
 
        Console.Read();
    }
}
}


Output:

 



Last Updated : 25 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads