Interfaces


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Interfaces

  1. #1
    o11011 Guest

    Interfaces

    Would someone please give me an example of real world use of interfaces.
    Here is what is confusing me. Say I have one dll that has one class called
    Messenger and one function called SendMessage. If I suspect that the
    implementation of SendMessage may change should I include an interface
    (IMessenger) into the dll that defines the SendMessage function? Then have
    the class implement it? If I do then should a client call the dll like this:

    ....
    IMessenger obj = new Messenger();
    obj.SendMessage();
    ....

    Now if I add a new class (Messenger2) to the dll that implements the
    IMyClass interface with different implementation than MyClass, I will still
    have to change all my client code to:

    ....
    IMyClass obj = new Messenger2();
    obj.SendMessage();
    ....

    If I am using interfaces correctly I fail to see how they are useful. Any
    help would be appreciated.



  2. #2
    MarkN Guest

    Re: Interfaces


    You should use interfaces or abstract or super classes when ever possible.
    You just need to figure out which is the right one to use.

    Here is a really world example - I am doing this. I need to create functions
    that add, multiply, divide, concat, convert, etc. At design time (not coding
    but a GUI tool that allows someone to assign parameters to the functions)
    I care what the function does. At run time I don't. I want to be able to
    loop through all the functions at run time and just say Function.Evaluate()
    and get an object back. I also need to add functions in the future that
    I currently don't know about or don't want to implement till needed.

    So I have an interface called Function (actually an abstract class but don't
    let that distract you) that has an Evaluate Method that returns an Object.

    The AddFunction implements this method and takes to numerical objects and
    adds them and returns a numerical Object.

    The ConcatFunction takes to Objects and Concats them and returns a String
    Object.

    And so on.

    Does this help? I have more. What is really cool is that these functions
    also implement another interface and they are treated as a completely different
    object type. This would have been impossible to do or at least involve completely
    crazy coding in a non OO language - even VB.

    Mark




    "o11011" <o11011@hotmail.com> wrote:
    >Would someone please give me an example of real world use of interfaces.
    >Here is what is confusing me. Say I have one dll that has one class called
    >Messenger and one function called SendMessage. If I suspect that the
    >implementation of SendMessage may change should I include an interface
    >(IMessenger) into the dll that defines the SendMessage function? Then have
    >the class implement it? If I do then should a client call the dll like this:
    >
    >....
    >IMessenger obj = new Messenger();
    >obj.SendMessage();
    >....
    >
    >Now if I add a new class (Messenger2) to the dll that implements the
    >IMyClass interface with different implementation than MyClass, I will still
    >have to change all my client code to:
    >
    >....
    >IMyClass obj = new Messenger2();
    >obj.SendMessage();
    >....
    >
    >If I am using interfaces correctly I fail to see how they are useful. Any
    >help would be appreciated.
    >
    >



  3. #3
    o11011 Guest

    Re: Interfaces

    Some sample code might help clear this up.Is my example a good use of
    interface? Is yes then how do I write the client so I don't have to change
    it when I add the new class?

    Thanks so far it is getting clearer.

    "MarkN" <m@n.com> wrote in message news:3c920c6c$1@10.1.10.29...
    >
    > You should use interfaces or abstract or super classes when ever possible.
    > You just need to figure out which is the right one to use.
    >
    > Here is a really world example - I am doing this. I need to create

    functions
    > that add, multiply, divide, concat, convert, etc. At design time (not

    coding
    > but a GUI tool that allows someone to assign parameters to the functions)
    > I care what the function does. At run time I don't. I want to be able to
    > loop through all the functions at run time and just say

    Function.Evaluate()
    > and get an object back. I also need to add functions in the future that
    > I currently don't know about or don't want to implement till needed.
    >
    > So I have an interface called Function (actually an abstract class but

    don't
    > let that distract you) that has an Evaluate Method that returns an Object.
    >
    > The AddFunction implements this method and takes to numerical objects and
    > adds them and returns a numerical Object.
    >
    > The ConcatFunction takes to Objects and Concats them and returns a String
    > Object.
    >
    > And so on.
    >
    > Does this help? I have more. What is really cool is that these functions
    > also implement another interface and they are treated as a completely

    different
    > object type. This would have been impossible to do or at least involve

    completely
    > crazy coding in a non OO language - even VB.
    >
    > Mark
    >
    >
    >
    >
    > "o11011" <o11011@hotmail.com> wrote:
    > >Would someone please give me an example of real world use of interfaces.
    > >Here is what is confusing me. Say I have one dll that has one class

    called
    > >Messenger and one function called SendMessage. If I suspect that the
    > >implementation of SendMessage may change should I include an interface
    > >(IMessenger) into the dll that defines the SendMessage function? Then

    have
    > >the class implement it? If I do then should a client call the dll like

    this:
    > >
    > >....
    > >IMessenger obj = new Messenger();
    > >obj.SendMessage();
    > >....
    > >
    > >Now if I add a new class (Messenger2) to the dll that implements the
    > >IMyClass interface with different implementation than MyClass, I will

    still
    > >have to change all my client code to:
    > >
    > >....
    > >IMyClass obj = new Messenger2();
    > >obj.SendMessage();
    > >....
    > >
    > >If I am using interfaces correctly I fail to see how they are useful. Any
    > >help would be appreciated.
    > >
    > >

    >




  4. #4
    Eric Gunnerson Guest

    Re: Interfaces

    You use interfaces when you're not sure what classes you're going to use at
    runtime, but you do know how you're going to use them.

    For example, the frameworks support the IComparable interface, which has a
    CompareTo() method in this. Because they've used an interface, both array
    and ArrayList can now have a generic Sort() function; it just takes whatever
    object it has, casts it to IComparable, and calls the function.



    --
    This posting is provided "AS IS" with no warranties, and confers no rights.

    "o11011" <o11011@hotmail.com> wrote in message news:3c920571$1@10.1.10.29...
    > Would someone please give me an example of real world use of interfaces.
    > Here is what is confusing me. Say I have one dll that has one class called
    > Messenger and one function called SendMessage. If I suspect that the
    > implementation of SendMessage may change should I include an interface
    > (IMessenger) into the dll that defines the SendMessage function? Then have
    > the class implement it? If I do then should a client call the dll like

    this:
    >
    > ...
    > IMessenger obj = new Messenger();
    > obj.SendMessage();
    > ...
    >
    > Now if I add a new class (Messenger2) to the dll that implements the
    > IMyClass interface with different implementation than MyClass, I will

    still
    > have to change all my client code to:
    >
    > ...
    > IMyClass obj = new Messenger2();
    > obj.SendMessage();
    > ...
    >
    > If I am using interfaces correctly I fail to see how they are useful. Any
    > help would be appreciated.
    >
    >




  5. #5
    Zane Thomas [.NET MVP] Guest

    Re: Interfaces


    I would suggest getting a book on Design Patterns (or search the web for
    'GOF design patterns) - study the Abstract Factory and Factory Method
    discussions for a solid background in the use of interfaces.


    --
    Turn on, tune in, download.
    zane@mabry.com

  6. #6
    o11011 Guest

    Re: Interfaces

    Thank you all.

    "o11011" <o11011@hotmail.com> wrote in message news:3c920571$1@10.1.10.29...
    > Would someone please give me an example of real world use of interfaces.
    > Here is what is confusing me. Say I have one dll that has one class called
    > Messenger and one function called SendMessage. If I suspect that the
    > implementation of SendMessage may change should I include an interface
    > (IMessenger) into the dll that defines the SendMessage function? Then have
    > the class implement it? If I do then should a client call the dll like

    this:
    >
    > ...
    > IMessenger obj = new Messenger();
    > obj.SendMessage();
    > ...
    >
    > Now if I add a new class (Messenger2) to the dll that implements the
    > IMyClass interface with different implementation than MyClass, I will

    still
    > have to change all my client code to:
    >
    > ...
    > IMyClass obj = new Messenger2();
    > obj.SendMessage();
    > ...
    >
    > If I am using interfaces correctly I fail to see how they are useful. Any
    > help would be appreciated.
    >
    >




  7. #7
    Michael Welch Guest

    Re: Interfaces

    o11011,

    "o11011" <o11011@hotmail.com> wrote in message news:3c92d7e3$1@10.1.10.29...
    > Thank you all.
    >


    In addition to the responses you already got, another example for using
    interfaces is the remoting problem you asked about.

    Michael



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center