.NET
  Home arrow .NET arrow Page 3 - Interface-Based Programming
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Interface-Based Programming
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2007-05-24

    Table of Contents:
  • Interface-Based Programming
  • Interface Implementation
  • Explicit Interface Implementation
  • Working with Interfaces

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Interface-Based Programming - Explicit Interface Implementation


    (Page 3 of 4 )

    The way of implementing an interface shown in the previous section is called implicit interface implementation, because a public method with a name and signature that match those of an interface method is implicitly assumed to be an implementation of that interface method.

    Example 3-3 demonstrates a simple technique that allows server developers to enforce the separation of the interface from the implementation. The server implementing the interface can actually prevent clients from accessing the interface methods directly by using explicit interface implementation. Implementing an interface explicitly means qualifying each interface member name with the name of the interface that defines it.

    Example 3-3.  Explicitly implementing an interface

    public interface IMyInterface
    {
      void Method1();
      void Method2();
    }
    public class MyClass : IMyInterface
    {

      void IMyInterface.Method1()
      {...}
      void IMyInterface.Method2()
      {...}
      //Other methods and members
    }

    Note that the interface members must be implicitly defined as private at the class’s scope; you can’t use any explicit access modifiers on them, includingprivate. The only way clients can invoke the methods of explicitly implemented interfaces is by accessing them via the interface:

      IMyInterface obj;
      obj = new MyClass();
      obj.Method1();

    To explicitly implement an interface in Visual Basic 2005, you need to explicitly set the method access toPrivate, as in Example 3-4.

    Example 3-4.  Explicitly implementing an interface in Visual Basic 2005

    Public Interface IMyInterface
      Sub Method1()
      Sub Method2()
    End Interface

    Public Class SomeClass
      Implements IMyInterface

      Private Sub Method1() Implements IMyInterface.Method1
      ...
      End Sub

      Private Sub Method2() Implements IMyInterface.Method2
      ...
      End Sub
    End Class

    You should avoid mixing and matching explicit and implicit interface implementations, as in the following fragment:

      //Avoid mixing and matching:
      public interface IMyInterface
      {
       
    void Method1();
       
    void Method2();
      }
      public class MyClass : IMyInterface
      {
       
    void IMyInterface.Method1()
        {...}
        public void Method2()
        {...}
        //Other methods and members
      }

    Although .NET lets you mix and match implementation methods, for consistency, you should avoid it. Such mix and match forces the client to adjust its references depending on whether a particular method is accessible via an interface or directly via the object.


    Assemblies with Interfaces Only

    Because interfaces can be implemented by multiple components, it’s good practice to put them in a separate assembly from that of the implementing components. Maintaining a separate assembly that contains only interfaces allows concurrent development of the server and the client, once the two parties have agreed on the interfaces. Such assemblies also extend the separation of interface from implementation to the code-packaging units.

     


    More .NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming .NET Components, Second...
     

    Buy this book now. This article is excerpted from chapter three of Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Check it out today at your favorite bookstore. Buy this book now.

    .NET ARTICLES

    - Introducing .NET
    - Building Business Objects for an Application
    - Methods for an Application`s Business Logic ...
    - Properties of an Application`s Business Logi...
    - Classes and Properties in an Application`s B...
    - Organizing Code for the Business Logic Layer...
    - Building the Business Logic Layer
    - Completing a Business Layer with Windows Wor...
    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek