Click here to Skip to main content
Click here to Skip to main content

An even conciser definition of Interfaces

By , 18 Jan 2013
Rate this:
Please Sign up or sign in to vote.

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.

interface IGrowable
{
  Grow();
}    
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:

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.

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)

About the Author

adamn909
Software Developer (Senior)
Israel Israel
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 18 Jan 2013
Article Copyright 2013 by adamn909
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid