Once you start reading up on the basics of Software design patterns you would come across the concept of "Programming to an interface". What programming to an interface does is that, it lets your code change its behavior with least amount of re write. To demonstrate this, we will follow the evolution of code from beginner level code with no code reuse to the point where we program to an interface.
Code with no reuse
Consider the code below.
You would notice that the code in Method Move() is repeated. Duplicate code is a big no no!
Reusing Code by applying Inheritance
using System;
In the above code we have reused code by placing it in the parent class. Now what if we have to add a Boat class and a Ship class to this and both of them inherit from the class Vehicles?
But a Boat doesn't have wheels! Instead it needs Turn Rotor in its Move method. Do you see how hard it is to extend the code when we apply inheritance? If we don't apply inheritance at all, we can have extensible code, but then we would also end up with repeating code.
So how do we re use code and make the code extensible at the same time? The answer is programming to an interface.
Program to an Interface
Code with no reuse
Consider the code below.
using System;
class EntryCLass
{
static void Main(string[] args)
{
MotorCycle mcycle = new
MotorCycle();
mcycle.Move();//Displays Turn Wheels
}
}
public abstract class Vehicles
{
}
public class Cars : Vehicles
{
public void Move()
{
Console.WriteLine("Turn
Wheels");
}
}
public class MotorCycle : Vehicles
{
public void Move()
{
Console.WriteLine("Turn
Wheels");
}
}
You would notice that the code in Method Move() is repeated. Duplicate code is a big no no!
Reusing Code by applying Inheritance
using System;
class EntryCLass
{
static void Main(string[] args)
{
MotorCycle mcycle = new
MotorCycle();
mcycle.Move();//Displays Turn Wheels
}
}
public abstract class Vehicles
{
public void Move()
{
Console.WriteLine("Turn
Wheels");
}
}
public class Cars : Vehicles
{
}
public class MotorCycle : Vehicles
{
}
using System;
class EntryCLass
{
static void Main(string[] args)
{
MotorCycle mcycle = new
MotorCycle();
mcycle.Move();//Displays Turn Wheels
Boat myBoat=new Boat();
myBoat.Move();//Displays Turn Wheels
Ship myShip = new Ship();
myShip.Move();//Displays Turn Wheels
}
}
public abstract class Vehicles
{
public void Move()
{
Console.WriteLine("Turn
Wheels");
}
}
public class Cars : Vehicles
{
}
public class MotorCycle : Vehicles
{
}
public class Boat : Vehicles
{
}
public class Ship : Vehicles
{
}
using System;
class EntryCLass
{
static void Main(string[] args)
{
IMove ImoveWheels = new
MoveByTurningWheels();
IMove ImoveRotor = new
MoveByTurningRotor();
MotorCycle mcycle = new
MotorCycle(ImoveWheels);
mcycle.Move();//Displays Turn Wheels
Boat myBoat = new Boat(ImoveRotor);
myBoat.Move();//Displays Turn Rotor
Ship myShip = new Ship(ImoveRotor);
myShip.Move();//Displays Turn Rotor
}
}
public interface IMove
{
void Move();
}
public class MoveByTurningWheels : IMove
{
public void Move()
{
Console.WriteLine("Turn
Wheels");
}
}
public class MoveByTurningRotor : IMove
{
public void Move()
{
Console.WriteLine("Turn
Rotor");
}
}
public abstract class Vehicles
{
public abstract void Move();
}
public class Car : Vehicles
{
IMove ImoveInternal;
public Car(IMove
Im)
{
ImoveInternal = Im;
}
public override void Move()
{
ImoveInternal.Move();
}
}
public class MotorCycle : Vehicles
{
IMove ImoveInternal;
public MotorCycle(IMove
Im)
{
ImoveInternal = Im;
}
public override void Move()
{
ImoveInternal.Move();
}
}
public class Boat : Vehicles
{
IMove ImoveInternal;
public Boat(IMove
Im)
{
ImoveInternal = Im;
}
public override void Move()
{
ImoveInternal.Move();
}
}
public class Ship : Vehicles
{
IMove ImoveInternal;
public Ship(IMove
Im)
{
ImoveInternal = Im;
}
public override void Move()
{
ImoveInternal.Move();
}
}
Programming to an interface is a way to achieve composition. Its called composition because a class is composed of other classes.
No comments:
Post a Comment
Comments will appear once they have been approved by the moderator