Polymorphism through Interfaces




Polymorphism through Interfaces

Consider another example (see Listing 7.2): IListable defines the members a class needs to support in order for the ConsoleListControl class to display it. As such, any class that implements IListable will have the capability of using the ConsoleListControl to display itself. The IListable interface requires a read-only property, ColumnValues.

Implementing and Using Interfaces

interface IListable
{
  // Return the value of each column
  // in the row.
  string[] ColumnValues
  {
      get;
  }
}
class Contact : PdaItem, IListable
{
  string[]ColumnValues
  {
      get
      {
          // ...
      }
  }
}
class Publication : IListable
{
  string[]ColumnValues
  {
      get
      {
          // ...
      }
  }
}
class Program
{
  public static void Main()
  {
      Contact[] contacts = new Contact[6];
      contacts[0] = new Contact(
          "Dick", "Traci",
          "123 Main St., Spokane, WA  99037",
          "123-123-1234");
      contacts[1] = new Contact(
          "Andrew", "Littman",
          "1417 Palmary St., Dallas, TX 55555",
          "555-123-4567");
      contacts[2] = new Contact(
          "Mary", "Hartfelt",

          "1520 Thunder Way, Elizabethton, PA 44444",
          "444-123-4567");
      contacts[3] = new Contact(
          "John", "Lindherst",
          "1 Aerial Way Dr., Monteray, NH 88888",
          "222-987-6543");
      contacts[4] = new Contact(
          "Pat", "Wilson",
          "565 Irving Dr., Parksdale, FL 22222",
          "123-456-7890");
      contacts[5] = new Contact(
          "Jane", "Doe",
          "123 Main St., Aurora, IL 66666",
          "333-345-6789");

      // Classes are cast implicitly to
      // their supported interfaces
      ConsoleListControl.List(Contact.Headers, contacts);

      Console.WriteLine();

      Publication[] publications = new Publication[3] {
          new Publication("Celebration of Discipline",
              "Richard Foster", 1978),
          new Publication("Orthodoxy",
              "G.K. Chesterton", 1908),
          new Publication(
              "The Hitchhiker's Guide to the Galaxy",
              "Douglas Adam", 1979)
          };
      ConsoleListControl.List(
          Publication.Headers, publications);
  }
}
class ConsoleListControl
{
    public static void List(string[] headers, IListable[] items)
    {
        int[] columnWidths = DisplayHeaders(headers);

        for (int count = 0; count < items.Length; count++)
        {
            string[] values = items[count].ColumnValues;               
            DisplayItemRow(columnWidths, values);
        }
    }

    private static int[] DisplayHeaders(string[] headers)
    {

        // ...
        string tab = string.Empty;
        int[] columnWidths = new int[headers.Length];
        for (int count = 0; count < headers.Length; count++)
        {
            Console.Write(tab + headers[count]);
            if (tab == string.Empty)
            {
                tab = "\t";
            }
            columnWidths[count] = headers[count].Length;
        }
        Console.WriteLine();
        return columnWidths;
    }

    private static void DisplayItemRow(
        int[] columnWidths, string[] values)
    {
        // ...
        string tab = string.Empty;
        for (int count = 0;
            count < values.Length; count++)
        {
            Console.Write(
                "{0}{1,-" + columnWidths[count] + "}",
                tab, values[count]);
            if (tab == string.Empty)
            {
                tab = "\t";
            }
        }
        Console.WriteLine();
    }
}

The results of Listing 7.2 appear in Output 7.1

In Listing 7.2, the ConsoleListControl can display seemingly unrelated classes (Contact and Publication). A displayable class is defined simply by whether it implements the required interface. As a result, the ConsoleListControl.List() method relies on polymorphism to appropriately display whichever set of objects it is passed. Each class has its own implementation of ColumnValues, and converting a class to IListable still allows the particular class's implementation to be invoked.

Output 7.1.

First Name  Last Name   Phone         Address                       
Dick        Traci       123-123-1234  123 Main St., Spokane, WA  99037
Andrew      Littman     555-123-4567  1417 Palmary St., Dallas, TX 55555
Mary        Hartfelt    444-123-4567  1520 Thunder Way, Elizabethton, PA44444
John        Lindherst   222-987-6543  1 Aerial Way Dr., Monteray, NH88888
Pat         Wilson      123-456-7890  565 Irving Dr., Parksdale, FL22222
Jane        Doe         333-345-6789  123 Main St., Aurora, IL 66666
Title                                  Author               Year
Celebration of Discipline              Richard Foster       1978
Orthodoxy                              G.K. Chesterton      1908
The Hitchhiker's Guide To The Galaxy   Douglas Adam         1979