Interfaces:Working with Interfaces

image_pdfimage_print

   


/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 10 - InterfacesWorking with Interfaces
// copyright 2000 Eric Gunnerson
using System;

public class WorkingwithInterfaces
{
    public static void Main()
    {
        DiagramObject[] dArray = new DiagramObject[100];
        
        dArray[0] = new DiagramObject();
        dArray[1] = new TextObject("Text Dude");
        dArray[2] = new TextObject("Text Backup");
        
        // array gets initialized here, with classes that
        // derive from DiagramObject. Some of them implement
        // IScalable.
        
        foreach (DiagramObject d in dArray)
        {
            if (d is IScalable)
            {
                IScalable scalable = (IScalable) d;
                scalable.ScaleX(0.1F);
                scalable.ScaleY(10.0F);
            }
        }
    }
}

interface IScalable
{
    void ScaleX(float factor);
    void ScaleY(float factor);
}
public class DiagramObject
{
    public DiagramObject() {}
}
public class TextObject: DiagramObject, IScalable
{
    public TextObject(string text)
    {
        this.text = text;
    }
    // implementing IScalable.ScaleX()
    public void ScaleX(float factor)
    {
        Console.WriteLine("ScaleX: {0} {1}", text, factor);
        // scale the object here.
    }
    
    // implementing IScalable.ScaleY()
    public void ScaleY(float factor)
    {
        Console.WriteLine("ScaleY: {0} {1}", text, factor);
        // scale the object here.
    }
    
    private string text;
}

           
          


A safe method of determining whether a class implements a particular interface

image_pdfimage_print

   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
//  ISample.cs - Demonstrates a safe method of determining whether a class
//               implements a particular interface
//               Compile this program with the following command line:
//                   C:>csc isample.cs
//
namespace nsInterfaceSample
{
    using System;
    public class InterfaceSample
    {
        static public void Main ()
        {
// Declare an instance of the clsSample class
            clsSample samp = new clsSample();
//  Test whether clsSample supports the IDisposable interface
            if (samp is IDisposable)
            {
//  If true, it is safe to call the Dispose() method
                IDisposable obj = (IDisposable) samp;
                obj.Dispose ();
            }
        }
    }
    class clsSample : IDisposable
    {
//  Implement the IDispose() function
        public void Dispose ()
        {
            Console.WriteLine ("Called Dispose() in clsSample");
        }
    }
}


           
          


Overriding Interfaces: Tester Overriding InterfacesAs

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace OverridingInterfaces
 {
     interface IStorable
     {
         void Read();
         void Write();
     }

     interface ITalk
     {
         void Talk();
         void Read();
     }


     // Modify Document to also implement ITalk
     class Document : IStorable, ITalk
     {
         // the document constructor
         public Document(string s)
         {
             Console.WriteLine(
                 "Creating document with: {0}", s);
         }

         // Implicit implementation
         public virtual void Read()
         {
             Console.WriteLine(
                 "Document Read Method for IStorable");
         }

         public void Write()
         {
             Console.WriteLine(
                 "Document Write Method for IStorable");
         }

         // Explicit implementation
         void ITalk.Read()
         {
             Console.WriteLine("Implementing ITalk.Read");
         }

         public void Talk()
         {
             Console.WriteLine("Implementing ITalk.Talk");
         }
     }

    public class TesterOverridingInterfacesAs
    {
       [STAThread]
       static void Main()
       {
           // Create a Document object
           Document theDoc = new Document("Test Document");
           IStorable isDoc = theDoc as IStorable;
           if (isDoc != null)
           {
               isDoc.Read();
           }

           // Cast to an ITalk interface
           ITalk itDoc = theDoc as ITalk;
           if (itDoc != null)
           {
               itDoc.Read();
           }

           theDoc.Read();
           theDoc.Talk();
       }
    }
 }

           
          


Overriding Interfaces

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace OverridingInterfaces
 {
     interface IStorable
     {
         void Read();
         void Write();
     }

     // Simplify Document to implement only IStorable
     class Document : IStorable
     {
         // the document constructor
         public Document(string s)
         {
             Console.WriteLine(
                 "Creating document with: {0}", s);
         }

         // Make read virtual
         public virtual void Read()
         {
             Console.WriteLine(
                 "Document Read Method for IStorable");
         }

         // NB: Not virtual!
         public void Write()
         {
             Console.WriteLine(
                 "Document Write Method for IStorable");
         }
     }

     // Derive from Document
     class Note : Document
     {
         public Note(string s):
             base(s)
         {
             Console.WriteLine(
                 "Creating note with: {0}", s);
         }

         // override the Read method
         public override void Read()
         {
             Console.WriteLine(
                 "Overriding the Read method for Note!");
         }

         // implement my own Write method
         public new void Write()
         {
             Console.WriteLine(
                 "Implementing the Write method for Note!");
         }
     }

     public class OverridingInterfacesTester
     {
         public void Run()
         {
             // Create a Document object
             Document theNote = new Note("Test Note");

             // direct call to the methods
             theNote.Read();
             theNote.Write();

             Console.WriteLine("
");

             // cast the Document to IStorable
             IStorable isNote = theNote as IStorable;
             if (isNote != null)
             {
                 isNote.Read();
                 isNote.Write();
             }
             Console.WriteLine("
");

             // create a note object
             Note note2 = new Note("Second Test");

             // directly call the methods
             note2.Read();
             note2.Write();
             Console.WriteLine("
");

             // Cast the note to IStorable
             IStorable isNote2 = note2 as IStorable;
             if (isNote != null)
             {
                 isNote2.Read();
                 isNote2.Write();
             }


         }

         static void Main()
         {
             OverridingInterfacesTester t = new OverridingInterfacesTester();
             t.Run();
         }
     }
 }

           
          


Interface casting

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace InterfaceDemo
 {
     interface IStorable
     {
         void Read();
         void Write(object obj);
         int Status { get; set; }

     }

     // here's the new interface
     interface ICompressible
     {
         void Compress();
         void Decompress();
     }


     // Document implements both interfaces
     class Document : IStorable
     {
         // the document constructor
         public Document(string s)
         {
             Console.WriteLine("Creating document with: {0}", s);

         }

         // implement IStorable
         public void Read()
         {
             Console.WriteLine(
                 "Implementing the Read Method for IStorable");
         }

         public void Write(object o)
         {
             Console.WriteLine(
                 "Implementing the Write Method for IStorable");
         }

         public int Status
         {
             get { return status; }
             set { status = value; }
         }

         // hold the data for IStorable's Status property
         private int status = 0;
     }


    public class TesterInterfaceDemoCasting
    {

       [STAThread]
       static void Main()
       {
           Document doc = new Document("Test Document");

           // only cast if it is safe
           if (doc is IStorable)
           {
               IStorable isDoc = (IStorable) doc;
               isDoc.Read();
           }
           else
           {
               Console.WriteLine("Could not cast to IStorable");
           }

           // this test will fail
           if (doc is ICompressible)
           {
               ICompressible icDoc = (ICompressible) doc;
               icDoc.Compress();
           }
           else
           {
               Console.WriteLine("Could not cast to ICompressible");
           }       }
    }
 }

           
          


Interface demo: implements two interfaces

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace InterfaceDemo
 {
     interface IStorable
     {
         void Read();
         void Write(object obj);
         int Status { get; set; }

     }

     // here's the new interface
     interface ICompressible
     {
         void Compress();
         void Decompress();
     }


     // Document implements both interfaces
     class Document : IStorable, ICompressible
     {
         // the document constructor
         public Document(string s)
         {
             Console.WriteLine("Creating document with: {0}", s);

         }

         // implement IStorable
         public void Read()
         {
             Console.WriteLine(
                 "Implementing the Read Method for IStorable");
         }

         public void Write(object o)
         {
             Console.WriteLine(
                 "Implementing the Write Method for IStorable");
         }

         public int Status
         {
             get { return status; }
             set { status = value; }
         }

         // implement ICompressible
         public void Compress()
         {
             Console.WriteLine("Implementing Compress");
         }
 
         public void Decompress()
         {
             Console.WriteLine("Implementing Decompress");
         }


         // hold the data for IStorable's Status property
         private int status = 0;
     }


    public class TesterInterfaceDemo2
    {
       public void Run()
       {
           Document doc = new Document("Test Document");
           doc.Status = -1;
           doc.Read();
           doc.Compress();
           Console.WriteLine("Document Status: {0}", doc.Status);
       }

       [STAThread]
       static void Main()
       {
          TesterInterfaceDemo2 t = new TesterInterfaceDemo2();
          t.Run();
       }
    }
 }

           
          


Interface demo

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace InterfaceDemo
 {
     // define the interface
     interface IStorable
     {
         void Read();
         void Write(object obj);
         int Status { get; set; }

     }

     // create a Document class that implements the IStorable interface
     class Document : IStorable
     {
         public Document(string s)
         {
             Console.WriteLine("Creating document with: {0}", s);
         }

         // implement the Read method
         public void Read()
         {
             Console.WriteLine(
                 "Implementing the Read Method for IStorable");
         }

         // implement the Write method
         public void Write(object o)
         {
             Console.WriteLine(
                 "Implementing the Write Method for IStorable");
         }
         // implement the property
         public int Status
         {
             get{ return status; }
             set{ status = value; }
         }

         // store the value for the property
         private int status = 0;
     }

    public class TesterInterfaceDemo
    {
       public void Run()
       {
           Document doc = new Document("Test Document");
           doc.Status = -1;
           doc.Read();
           Console.WriteLine("Document Status: {0}", doc.Status);
       }

       [STAThread]
       static void Main()
       {
          TesterInterfaceDemo t = new TesterInterfaceDemo();
          t.Run();
       }
    }
 }