Click here to Skip to main content
15,894,405 members
Articles / Programming Languages / C#

An even conciser definition of Interfaces

Rate me:
Please Sign up or sign in to vote.
4.82/5 (7 votes)
18 Jan 2013CPOL3 min read 7.5K   10   2
A very brief explanation of Interfaces.

Introduction

A very brief explanation of Interfaces for beginners. 

Background 

If you're new to Interfaces then you are probably having difficulty getting your head around them. Once you get it however, a whole new world opens up in software design. This is a brief tutorial explaining why they are useful. 

Definition  

Instead of a formal definition it's easier to say that Interfaces can be simply described in two words, 'Group Behavior'.  

Why are Interfaces useful? 

Well from the definition above they can be useful in two ways: 

  1. To group objects by a common behavior. 
  2. To implement the desired behavior on a group of objects.  

Example  

We see an ancient example of this principle in the Midrash 2000 years ago. 

There is not a single blade of grass on earth below that does not have an angel in the heavens above that strikes it and says to it: 'Grow!

Of course the Master of the Universe would be a great programmer also Smile | :)  

We see here that in our universe every plant has a common behavior, they grow. The 'angel' function iterates through every Growable object in the universe and calls the Grow function. We can implement this basic code.

C#
interface IGrowable
{
  Grow();
}    
C#
List<IGrowable> plants = Universe.TypeOf<IGrowable>.ToList();
foreach (plant in plants)
{
  plant.Grow();
}  

This is good programming, each object that implements the IGrowable interface must define how it grows itself, so we can call Grow() and be guaranteed that it works. 

Now you may ask why can't we just use a plain Plant object in our example? Well not just plants grow, also animals, people, bacteria, problems, leaks, baldness etc. We can't use inheritance because none of these objects have anything in common with each other, other than they all grow. So if our Angel of Growth of Plants is also the Angel of Growth of Bald Spots then he can still do his job. Then again maybe it's an Angel of Death iterating through my hair, telling each strand to die??!

It doesn't matter the is point grouping by behavior makes coding a lot easier.  

A More Complex Example

I'm going to extend my very concise definition of Interfaces to show how powerful they can be. Let's use the Visitor Pattern:

I have one woman and three people in a room; Doctor, Husband and a Homeless stranger. A woman is comprised of body parts, each of which can be a combination of being touched, stroked or kissed, let's say.

Body Parts (Class) 
Arm 
Hair 
Eye 
Teeth 

ITouchable (Interface) 
Arm 
Hair 
Teeth 

IStrokeable (Interface) 
Arm 
Hair  

IKissable (Interface) 
Arm 

These are all the 'visited' objects. Now let's define our 'visitor' objects.

Human (object), IKisser, IStroker, IToucher (Interfaces)  
Doctor 
Husband 
Homeless stranger 

Now let's build the pattern:

C#
foreach (Human human in Room)
{
  foreach (ITouchable bodypart in Woman.BodyParts.TypeOf<ITouchable>())
{ 
  bodypart.TouchedBy(human); 
}  
 foreach (IStrokable bodypart in Woman.BodyParts.TypeOf<IStrokable>())
{ 
  bodypart.StrokedBy(human); 
}   
foreach (IKissable bodypart in Woman.BodyParts.TypeOf<IKissable>())
{ 
  bodypart.KissedBy(human); 
} 
} 

Here we are using Interfaces to group the different behavior actions by the 'visitors', now we need to define how these behaviors are received by the visited, i.e., the woman.

C#
public class Arm : BodyPart, ITouchable, IStrokable, IKissable
{
  public void TouchedBy(IToucher toucher)
  {
  if (toucher is Doctor)
   {
{
    Console.WriteLine("Your hands are cold"); 
  } 
else if (toucher is Husband)
{    
 
  Console.WriteLine("Not now, I have a headache"); 
} 
else if (toucher is HomelessStranger) 
{
  Console.WriteLine("Please don't touch me,"); 
} 
} 
public void StrokedBy(IStroker stroker) 
{ 
if (stroker is Doctor)
{
  Console.WriteLine("What are you doing?"); 
} 
else if (stroker is Husband)
{
  Console.WriteLine("Not now, I have a headache"); 
} 
else if (stroker is HomelessStranger) 
{
  Console.WriteLine("Get off!"); 
}  
}
public void KissedBy(IKissable kisser)
{
 if (kisser is Doctor)
{
  Console.WriteLine("What the hell are you doing?!"); 
} 
else if (kisser is Husband)
{
  Console.WriteLine("Not now, I have a headache"); 
} 
else if (kisser is HomelessStranger) 
{
  Console.WriteLine("Police!!!!!"); 
}   
}  
}  

This kind of pattern would be useful for a compliance system for example, you can easily separate the checking system from the objects that need checking, which is good. You can also see how easily it would be to add another person to the room without breaking the system.

Conclusion 

Interfaces allows us to define the system by how the components behave with each other, this allows flexibility and robustness, very important in OOP. 

License

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


Written By
Software Developer (Senior)
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
_groo_20-Oct-14 2:03
_groo_20-Oct-14 2:03 
Using the is keyword to distinguish types is exactly how OOP should not be applied.

The last example is not a correctly applied visitor pattern. Proper OOP code should differentiate its functionality through different class implementations. Any code snippet which has a large switch-case (or a bunch of ifs) to differentiate classes based on their type is almost certainly a code smell (you are essentially using a typecode - you could as well be using an enum), and this example is a perfect example of a maintenance nightmare: adding a new type of Human will require you to go through all methods in order to add a new if clause to them.
GeneralMy vote of 5 Pin
johannesnestler20-Oct-14 1:48
johannesnestler20-Oct-14 1:48 

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

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