a DotNetStyling - .Net World by Armen Ayvazyan : Explicit Interface Implementation
Welcome to DotNetStyling Sign in | Join | Help
Add to Technorati Favorites Add to Google Reader or Homepage Add to My AOL Subscribe in FeedLounge Subscribe in Bloglines Add to Excite MIX Add to flurry Add to Pageflakes Subscribe in NewsGator Online


Explicit Interface Implementation

Theory

Explicit interface implementation helps to ensure that user can use methods define by a given interface using by correct interface reference.

 

Illustration

Lets say we have to build a motherboard which contains a memory slot and a slot for PCI Express card. It is also important for us to get access to each of its parts and for example to get information about voltage used by each slot.

Our first step is to create an interfaces which are the models for appropriate slots.

interface IDDRMemorySlot
{

      /// <summary>

      /// Gets voltage used by memory.

      /// </summary>

      void GetVoltage();

}

interface IPCIExpressCardSlot
{

      /// <summary>

      /// Gets voltage used by video card.

      /// </summary>

      void GetVoltage();

}

As you can see both interfaces contain member with the same signature: GetVoltage().

Let's create class representing motherboard. As you can guess MotherBoard will be inherited by those interfaces.

 

class MotherBoard : IPCIExpressCardSlot, IDDRMemorySlot

{

}

 

Well, now we are facing the problems:

  • Which interface GetVoltage() member belongs to after its implementation?
  • How can we get information about voltage used by each device from motherboard object?
  • What if we would like to have similar information about the voltage used by motherboard itself.

 

This is the moment when Explicit Interface Implementation technique can help us to solve those problems.

 

class MotherBoard : IPCIExpressCardSlot, IDDRMemorySlot

{

      /// <summary>

      /// Gets voltage used by video card.

      /// </summary>

      void IPCIExpressCardSlot.GetVoltage()

      {

            Console.WriteLine("PCI Express card needs 12V.");

            Console.WriteLine();

      }

     

      /// <summary>

      /// Gets voltage used by memory.

      /// </summary>

      void IDDRMemorySlot.GetVoltage()

      {

            Console.WriteLine("Memory slot needs 3V energy.");

            Console.WriteLine();

      }

 

      /// <summary>

      /// Gets voltage used by motherboard.

      /// </summary>

      public void GetVoltage()

      {

            Console.WriteLine("Motherboard needs 60V");

            Console.WriteLine();

      }

}

If you take a look, we are implementing three methods with similar signature (GetVoltage) but two of them are followed by theirs interface name: this is an explicit interface implementation. You can get access to those methods only if you cast class by appropriate interface. The method without followed interface name belongs to MotherBoard class.

 

static void Main(string[] args)

{

      MotherBoard motherBoard = new MotherBoard();

           

      //Voltage used by motherboard.

      motherBoard.GetVoltage();

 

      //Voltage value used by video card.

      IPCIExpressCardSlot videoCardSlot = motherBoard;

      videoCardSlot.GetVoltage();

 

      //Voltage value used by memory

      IDDRMemorySlot memorySlot = motherBoard;

      memorySlot.GetVoltage();

 

      Console.ReadKey();     

}

 

Output shows that we got access to all three methods implemented in the class. When we need to get information about the motherboard we simply call GetVoltage() method of object. In case if we need information about its particular part, we just need to cast it to appropriate interface and call its GetVoltage() method.

 

image

 

 

Technorati Tags: , ,

kick it on DotNetKicks.com Digg!

Posted: 29. října 2007 1:03 by Armen Ayvazyan
Filed under: ,

Comments

New Comments to this post are disabled