Introduction
Achieving polymorphic behavior by implementing interfaces is a well known pattern. I am also putting some thoughts and code to demonstrate the same. This article clearly demonstrates the use of interfaces and their importance when they are implement explicitly.
Background
Few of my colleagues and developers have been questioning me when I suggest that they use interfaces to create the programming model. Why should we write extra lines of code in creating an interface, then implementing it in a class while we can directly use the class without writing the interface.
So, if anyone has the same question, probably this post can answer it.
Using the Code
Simply download the code, compile and execute it.
In this sample application, I picked cricket as a domain and created few of the interfaces representing behaviors of the players.
Below are the interfaces:
public interface IBatsman
{ int BatingOrder { get; set; }
double StrikRate { get; set; }
int HighestScore { get; set; }
int Centuries { get; set; }
void StartBating();
}
public interface IBowler { int WicketCount { get; set; }
double AvgBallingSpeed { get; set; }
int BowlingCategory { get; set; }
void StartBowling();
}
public interface IAllRounder : IBatsman , IBowler
{ int StumpingCount { get; set; }
int CatchCount { get; set; }
void ChooseRole();
}
public interface ICricketer : IAllRounder , IBowler , IBatsman {
int ICCRank { get; set; }
void PlayCricket();
}
Here is the Cricket class which implements all of these interfaces. Since every cricketer has some other properties also which are not covered in these interfaces, I am defining such properties in the class itself, i.e. name (defining only one for example).
<pre> public class Cricketer : ICricketer , IAllRounder , IBowler , IBatsman
{
#region Constructor
public Cricketer() { }
#endregion
#region Properties
public string Name { get; set; }
#endregion
#region Method
public void ParticipateInTournament()
{ }
#endregion
#region ICricketer Members
int ICricketer.ICCRank { get; set; }
void ICricketer.PlayCricket(){}
#endregion
#region IAllRounder Members
int IAllRounder.StumpingCount { get; set; }
int IAllRounder.CatchCount { get; set; }
void IAllRounder.ChooseRole(){}
#endregion
#region IBatsman Members
int IBatsman.BatingOrder { get; set; }
double IBatsman.StrikRate { get; set; }
int IBatsman.HighestScore { get; set; }
int IBatsman.Centuries { get; set; }
void IBatsman.StartBating(){}
#endregion
#region IBowler Members
int IBowler.WicketCount { get; set; }
double IBowler.AvgBallingSpeed { get; set; }
int IBowler.BowlingCategory { get; set; }
void IBowler.StartBowling() {}
#endregion
}
So, the model is ready to be used and the client application has to instantiate the class referring to the correct interface.
The beauty is that the object of the same class will have different properties/methods based on the interface used.
Here is the example code snippet:
class Program
{
static void Main(string[] args)
{
IBowler Murlidharan = new Cricketer();
Murlidharan.WicketCount =300;
Murlidharan.StartBowling();
IBatsman SachinTendulkar = new Cricketer();
SachinTendulkar.BatingOrder = 10;
SachinTendulkar.StartBating();
IAllRounder SorabGanguly = new Cricketer();
SorabGanguly.ChooseRole();
SorabGanguly.WicketCount = 15;
SorabGanguly.StartBowling();
SorabGanguly.BatingOrder = 10;
SorabGanguly.StartBating();
ICricketer Kaipldev = new Cricketer();
Kaipldev.ICCRank = 10;
Kaipldev.PlayCricket();
Kaipldev.ChooseRole();
Kaipldev.WicketCount = 15;
Kaipldev.StartBowling();
Kaipldev.BatingOrder = 10;
Kaipldev.StartBating();
Cricketer AnyCricketer = new Cricketer();
AnyCricketer.ParticipateInTournament();
AnyCricketer.Name = "Vinod";
}
}
Points of Interest
It was real fun writing this code snippet. Hope it will be useful for others too.