.NET
  Home arrow .NET arrow Page 2 - Understanding 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Understanding Interface-Based Programming
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-06-14

    Table of Contents:
  • Understanding Interface-Based Programming
  • Designing and Factoring Interfaces
  • Factoring Metrics
  • Interfaces in Visual Studio 2005
  • Interface Refactoring

  • 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
     
    Iron Speed
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Understanding Interface-Based Programming - Designing and Factoring Interfaces


    (Page 2 of 5 )

    Syntax aside, how do you go about designing interfaces? How do you know which methods to allocate to which interface? How many members should each interface have? Answering these questions has little to do with .NET and a lot to do with abstract component-oriented analysis and design. An in-depth discussion of how to decompose a system into components and how to discover interface methods and properties is beyond the scope of this book. Nonetheless, this section offers a few pieces of advice to guide you in your interface-design effort.

    Interface Factoring

    An interface is a grouping of logically related methods and properties. What constitutes “logically related” is usually domain-specific. You can think of interfaces as different facets of the same entity. Once you have identified (after a requirements analysis) all the operations and properties the entity supports, you need to allocate them to interfaces. This is called interface factoring. When you factor an interface, always think in terms of reusable elements. In a component-oriented application, the basic unit of reuse is the interface. Would this particular interface factoring yield interfaces that other entities in the system can reuse? What facets of the entity can logically be factored out and used by other entities?

    Suppose you wish to model a dog. The requirements are that the dog be able to bark and fetch and that it have a veterinary clinic registration number and a property for having received shots. You can define theIDoginterface and have different kinds of dogs, such asPoodle andGermanShepherd, implement theIDog interface:

      public interface IDog
     
    {
        void  Fetch();
        void  Bark();
        long  VetClinicNumber{get;set;}
        bool  HasShots{get;set;}
     
    }
      public class Poodle : IDog
      {...}

      public class GermanShepherd : IDog
      {...}

    However, such a composition of theIDoginterface isn’t well-factored. Even though all the interface members are things a dog should support,FetchandBarkare more logically related to each other than toVetClinicNumberandHasShots.Fetch()andBark() involve one facet of the dog, as a living, active entity, whileVetClinicNumberandHasShotsinvolve a different facet, one that relates it as a record of a pet in a veterinary clinic. A better approach is to factor out theVetClinicNumberandHasShotsproperties to a separate interface calledIPet:

      public interface IPet
     
    {
        long  VetClinicNumber{ get; set; }
        bool  HasShots{ get; set; }
     
    }
      public interface IDog
      {
       
    void Fetch();
        void Bark();
      }

    Because the pet facet is independent of the canine facet, other entities (such as cats) can reuse theIPetinterface and support it:

      public interface IPet
      {
        long  VetClinicNumber{ get; set; }
        bool  HasShots{ get; set; }
      }
      public interface IDog
      {
       
    void  Fetch();
       
    void  Bark();
      }
      public interface ICat
      {
       
    void  Purr();
       
    void  CatchMouse();
      }

      public class Poodle : IDog,IPet
      {...}
      public class Siamese : ICat,IPet
      {...}

    This factoring, in turn, allows you to decouple the clinic-management aspect of the application from the actual pet type (be it dogs or cats). Factoring operations and properties into separate interfaces is usually done when there is a weak logical relation between methods. However, identical operations are sometimes found in several unrelated interfaces, and these operations are logically related to their respective interfaces. For example, both cats and dogs need to shed fur and feed their offspring. Logically, shedding is just a dog operation, as is barking, and is also just a cat operation, as is purring. In such cases, you can factor the interfaces into a hierarchy of interfaces instead of separate interfaces:

      public interface IMammal
      {
       
    void ShedFur();
       
    void Lactate();
     
    }
      public interface IDog : IMammal
      {
       
    void  Fetch();
       
    void  Bark();
      }
      public interface ICat : IMammal
      {
       
    void  Purr();
       
    void  CatchMouse();
      }

    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

    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...

    IBM developerWorks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway