locked
when to create interfaces

    Question

  • after learning Interfaces, what they are, and how they work I still can't figure out when I would need to create an interface when designing my application. 

    In Learning C# 3.0 I read the words, "You can create no Interface, one Interface, or many Interfaces when designing your application". This sentence really confuses me.

    From a design approach, when I gather my requirements before I start writing any code, what question should I ask myself in order to determine wether or not I should use a interface??????????

    I'm looking for an answer other then, "you use an interface when you want to insure that the inheriting class will use each method, member, etc. from the interface"

    chuckdawit
    Sunday, February 07, 2010 1:32 AM

Answers

  • Hi,

    When we talk about the word "design" there must be a long stroy to tell, every person has his own understanding of how to design a application, and the interface design should be a part of application, there's no something "if-else" liked rule to tell us when to use interfaces, every person has his own understanding how a application should work.

    But we could have some guidelines, also, this question has been asked for many times, such as this thread:
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/8edbf675-6375-4735-997e-bd7bce58f115/

    This topic is big, people offten write articles to show his own understanding, here are some of them:
    http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_interrfaces03052006095933AM/csharp_interrfaces.aspx

    When you fully understand what is a "Car", you'll be able to decide where to drive it.
    http://msdn.microsoft.com/en-us/library/87d83y5b.aspx


    Here are some commens from MSDN document:
    There are several other reasons why you might want to use interfaces instead of class inheritance:
    Hope this could help.

    Thanks.

    • Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.

    • Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.

    • Interfaces are better in situations in which you do not have to inherit implementation from a base class.

    • Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.

     

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Gnanadurai Friday, February 12, 2010 4:27 AM
    • Marked as answer by witdaj Friday, February 12, 2010 5:02 AM
    Wednesday, February 10, 2010 6:12 AM
  • Hi,

    I add some more points with the above when and where we can use interfaces practically.
    You can use interfaces well in Service Oriented Apps. Not only in Service Oriented Apps but at any point of time you can go for interfaces. But here I'm going say why they best fit in the service world.

    Say I'm a service provider and you are a client consuming my services. Suppose I'm offering you a service that stream videos and images from a remote computer. First no interfaces at this time. I'm giving you a class say Streamer and it has methods to stream video or image from remote computer like below.

    public class Streamer
    {
          public Stream StreamVideo(...)
          {
          }

          public Stream StreamImage(...)
          {
          }
    }


    The above is my service class and now you started to use it. Wherever you need you created instances of my service class like this and consuming it's methods.

    Streamer streamHelper = new Streamer()
    streamHelper.StreamVideo();


    OK.. Now I'm have done some improvements in streaming video and image faster. Say I have created a new Service Class called SuperStreamer. This class also offer you the same set of operations like the previous version but.... faster. Now you are planning to use this Super Streamer in that application. So wherever you used the previous Streamer class you want to comment those lines and instead create instances of my new SuperStreamer class and want to consume it's streaming methods.

    So next time if I created another advanced Streamer again you want to change more things in your application :(

    Now we are going to use interfaces. Say I'm also giving you an interface that you didn't use previously say IStreamer.
    Every streamer i'm creating are implementing this interface. 

    public interface IStreamer
    {
        Stream StreamViseo(...);

        Stream StreamImage(...);


    public class Streamer : IStreamer
    {
    }

    public class SuperStreamer : IStreamer
    {
    }


    So in your application now you are using like this,

    IStreamer streamer = StreamServiceFactory.GetInstance();

    the StreamServiceFactory is a class that creates the instance of either Streamer or SuperStreamer (instantiation is controlled in a single place).

    So if you use IStreamer instead of directly Streamer or SuperStreamer. Tomorrow if i create another advanced streamer you don't need to change your code :)

     So if the ServiceProvider I'm and client you communicates through an interface whenever the underlying implementation provided by me changes you need to change your code. Interface as it's name acts like a plug that joins two devices just giving a flexibility of changing one not affecting the other.

    Finally.. whenever you are going to use a component provided by a service provider say a library that interacts with database or a tool that interacts with a COM component better use interfaces.

    Dnana
    (Mark as answer if this clarifies your doubts)

    • Marked as answer by witdaj Friday, February 12, 2010 5:01 AM
    Wednesday, February 10, 2010 6:16 AM

All replies

  • When building small apps as a single developer you can usually get away with limited use of interfaces.  The larger and more complicated your project becomes, the more likely you will want to use interfaces.  This is especially true if you work on a team.

    Additionally, many code "patterns" call for the use of interfaces.

    At a 30k foot view, you can think of an interface as a contract that things (classes, people) will abide by.  This contract allows you a great deal of flexibility.

    For example, if I am working on a class and I tell you my class will abide by a given contract (implement a given interface) then you can take that interface and use it when creating your class.  You might even create your own testing implementation of my class that does nothing as long as it also abides by our contract.  All the while I can keep working on my class, or leave it for later and you don't have to worry about it.

    jon.stromer.galley
    • Marked as answer by Tim Li Wednesday, February 10, 2010 3:09 AM
    • Unmarked as answer by witdaj Wednesday, February 10, 2010 5:32 AM
    Sunday, February 07, 2010 4:10 AM
  • Hello chuck,

    Interfaces are just like what they are in the real world, when you need to communicate with the external world or, say, you wanna expose some thing to external, you could make use of the interface.

    You could try to understand this concert from a sample here:

    http://msdn.microsoft.com/en-us/library/bb608614.aspx

    Above link demonstrate a scenario of a interact between Office VBA and managed code, and in this case the author use a interface exposeed a method in order to make it could be called in Office VBA.

    Hope this helps.

    Thanks.
     

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Wednesday, February 10, 2010 3:09 AM
    • Unmarked as answer by witdaj Wednesday, February 10, 2010 5:32 AM
    Monday, February 08, 2010 5:45 AM
  • what I'm looking for is a logical decision. an if-else statement I guess...
    When I have my requirements and before I start designing I need to say, "I need an interface here at this point in my application or no I don't need an interface".
    What is the question I can ask myself when making that decision? How do I know?

    chuckdawit
    Wednesday, February 10, 2010 5:36 AM
  • Hi,

    When we talk about the word "design" there must be a long stroy to tell, every person has his own understanding of how to design a application, and the interface design should be a part of application, there's no something "if-else" liked rule to tell us when to use interfaces, every person has his own understanding how a application should work.

    But we could have some guidelines, also, this question has been asked for many times, such as this thread:
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/8edbf675-6375-4735-997e-bd7bce58f115/

    This topic is big, people offten write articles to show his own understanding, here are some of them:
    http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_interrfaces03052006095933AM/csharp_interrfaces.aspx

    When you fully understand what is a "Car", you'll be able to decide where to drive it.
    http://msdn.microsoft.com/en-us/library/87d83y5b.aspx


    Here are some commens from MSDN document:
    There are several other reasons why you might want to use interfaces instead of class inheritance:
    Hope this could help.

    Thanks.

    • Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.

    • Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.

    • Interfaces are better in situations in which you do not have to inherit implementation from a base class.

    • Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.

     

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Gnanadurai Friday, February 12, 2010 4:27 AM
    • Marked as answer by witdaj Friday, February 12, 2010 5:02 AM
    Wednesday, February 10, 2010 6:12 AM
  • Hi,

    I add some more points with the above when and where we can use interfaces practically.
    You can use interfaces well in Service Oriented Apps. Not only in Service Oriented Apps but at any point of time you can go for interfaces. But here I'm going say why they best fit in the service world.

    Say I'm a service provider and you are a client consuming my services. Suppose I'm offering you a service that stream videos and images from a remote computer. First no interfaces at this time. I'm giving you a class say Streamer and it has methods to stream video or image from remote computer like below.

    public class Streamer
    {
          public Stream StreamVideo(...)
          {
          }

          public Stream StreamImage(...)
          {
          }
    }


    The above is my service class and now you started to use it. Wherever you need you created instances of my service class like this and consuming it's methods.

    Streamer streamHelper = new Streamer()
    streamHelper.StreamVideo();


    OK.. Now I'm have done some improvements in streaming video and image faster. Say I have created a new Service Class called SuperStreamer. This class also offer you the same set of operations like the previous version but.... faster. Now you are planning to use this Super Streamer in that application. So wherever you used the previous Streamer class you want to comment those lines and instead create instances of my new SuperStreamer class and want to consume it's streaming methods.

    So next time if I created another advanced Streamer again you want to change more things in your application :(

    Now we are going to use interfaces. Say I'm also giving you an interface that you didn't use previously say IStreamer.
    Every streamer i'm creating are implementing this interface. 

    public interface IStreamer
    {
        Stream StreamViseo(...);

        Stream StreamImage(...);


    public class Streamer : IStreamer
    {
    }

    public class SuperStreamer : IStreamer
    {
    }


    So in your application now you are using like this,

    IStreamer streamer = StreamServiceFactory.GetInstance();

    the StreamServiceFactory is a class that creates the instance of either Streamer or SuperStreamer (instantiation is controlled in a single place).

    So if you use IStreamer instead of directly Streamer or SuperStreamer. Tomorrow if i create another advanced streamer you don't need to change your code :)

     So if the ServiceProvider I'm and client you communicates through an interface whenever the underlying implementation provided by me changes you need to change your code. Interface as it's name acts like a plug that joins two devices just giving a flexibility of changing one not affecting the other.

    Finally.. whenever you are going to use a component provided by a service provider say a library that interacts with database or a tool that interacts with a COM component better use interfaces.

    Dnana
    (Mark as answer if this clarifies your doubts)

    • Marked as answer by witdaj Friday, February 12, 2010 5:01 AM
    Wednesday, February 10, 2010 6:16 AM
  • Hello,

     

    I am writing to check the status of the issue on your side. Could you please let me know if the suggestion works for you or not? If you have any questions or concerns, please feel free to let me know. I will be more than happy to be of assistance.


     

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by witdaj Friday, February 12, 2010 5:01 AM
    • Unmarked as answer by witdaj Friday, February 12, 2010 5:01 AM
    Friday, February 12, 2010 2:53 AM
  • Hi Dnana,
    Thanks for the reply.
    Can you give me more explanation on:

    IStreamer streamer = StreamServiceFactory.GetInstance();

    the StreamServiceFactory is a class that creates the instance of either Streamer or SuperStreamer (instantiation is controlled in a single place).


    I don't understand what the StreamServiceFactory class is. Is it a factory pattern? Can you give me a full example?
    Can you also give me an example of the GetInstance()?

    thanks. I really appreciate it.

    chuckdawit
    Friday, February 12, 2010 5:35 AM
  • Hi,

    StreamServiceFactory is a factory and GetInstance() is the public() creates instances of the Streamer or SuperStreamer. Wherever you want to create instances use the StreamServiceFactory. So later if you need  SuperStreamer need to change only the GetInstance(). 

    public class StreamServiceFactory
    {
          public static IStreamer GetInstance()
          {
                  return new Streamer();
          }
    }
    Regards
    Dnana
    Monday, February 15, 2010 6:08 AM