0

In many books or articles u may see such a definition about interface :an interface is a "contract" or an agreement between the consumer (caller) and the provider (callee).but Unfortunately there is no Clear example that describes what is caller class or what is callee class and show how they could communicate with each other through interface.

from this point of view I am confused about The Terms caller(consumer) and callee (provider), I just know that we define an interface and a class Implements that Interface . is the implementor class considerd as caller if so what about callee , how callee uses the interface , could any one describe this terms clarely and give a clear example about that .

any help would be highly appreciated .

siamak
  • 669
  • 1
  • 10
  • 28

3 Answers3

2

Who implements an interface is a callee, cause he provides an implementation of interface. Consumer is tat one who uses an object of the callee, so they call it caller.

EDIT

pulbic interface IPlugin
{
    double Calculate(double d1, double d2);
}

public class WebConnectPlugin: IPlugin
{
   public double Calculate(double d1, double d2){ // some code}
}

public class DBConnectPlugin: IPlugin
{
   public double Calculate(double d1, double d2){ // some code}
}

and somewhere in the code:

public class CallerIDE
{
   IPlugin plugin= null; 


   public void DoSomething()
   {
      contractor = GetPlugin();
      double value = contractor.Calculate(10.3456, -3.546456);
   }

   private IPlugin GetPlugin()
   {
      return new WebConnectPlugin();

      return new DBConnectPlugin(); //based on some logic
   }

}
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I got the idea , but please tell me why u didnt use the interface as a parameter for a method(Dosomething) in caller class ? , I think using the interface as a private member or as a parameter makes different in type of the relationship between caller class and calle class , could someone explain this ? – siamak Apr 09 '12 at 05:59
  • I wouldn't make them different. The concept is the same. You *use* interface either it parameter or member of a class, that makes no difference. – Tigran Apr 09 '12 at 07:52
  • no , idont agree with u ,I think when u use an instance of o class into another class , there is a composition relationship between these two classes, but when u pass an instance of a class to a method of another class ,there is a dependency relationship. – siamak Apr 09 '12 at 08:46
  • @siamak: we are talking about here *not* about relationship but caller/calee pattern. If we would talk about relationship, I would agree with you. – Tigran Apr 09 '12 at 09:00
  • I mean that , in most of Caller callee relationships , its very natural to use the interface reference as a parameter for a method , but I am wondering in which cases we must use the interface reference as a member of the caller class ? could u please give an example like @Dhananjay ,thanks – siamak Apr 09 '12 at 13:19
  • @siamak: the most frequent use is plugins management, like in my edited post. Check it out. – Tigran Apr 09 '12 at 14:49
  • I made a gooogling , that s it , but it would be great if u could introduce some links around the topic "Plug in Management" for reading more , anyway thanks. – siamak Apr 11 '12 at 11:44
  • 1
    @siamak: ah, it's too big topic, and there is no a "silver bullet" (one solution, everything have to be adupted to concrete project), but you can start to have a look on: 1) [Implementing Plugin Architecture](http://drdobbs.com/cpp/184403942) 2) [So Question](http://stackoverflow.com/questions/323202/designing-extensible-software-plugin-architecture) 3)[Plugins in C#](http://www.codeproject.com/Articles/6334/Plug-ins-in-C) – Tigran Apr 11 '12 at 12:05
2

SCHOOL is place where many INDIVIDUALS come and study. Every INDIVIDUAL has different way of learning.

SCHOOL has one rule : any INDIVIDUAL coming in must be LEARNABLE.

SCHOOL is sure that if an INDIVIDUAL is not LEARNABLE then it can not teach them and hence
they cant study.

Every INDIVIDUAL implements ILEARNABLE interface

public  class INDIVIDUAL : ILEARNABLE //this is provider class as it provides implementation of interface.
{

    LEARN()
     {
        //WAY OF LEARNING IS MENTIONED HERE...
     }
}

School teaches them through a method called Teach()

class SCHOOL  // This is consumer class -
{
   void Teach (ILEARNABLE  anyone)
   {
     ...     
     anyone.LEARN();
     ... some code...
   }

}

Here school does not have to worry about who is individual as long as they have implemented ilearnable interface.

Dhananjay
  • 3,673
  • 2
  • 22
  • 20
  • very nice example , but please clear that when do u need to use the interface as a parameter for a method in the customer class? and when do u need to use it , as a private member in the customer class ?you know using the interface in different places makes different relationship between provider class and customer class.thanks – siamak Apr 09 '12 at 06:14
  • @siamak : 1> Interface as Private Member ::: Private members are used to store a state of an object. e.g. Let's say your School class does lot many things than just teach e.g. ConductExam(). So Declare Priavte member as List AllLearnedones; and whenever you TEACH them then add them in the list like : AllLearnedones.Add(Individual1); AllLearnedones.Add(Individual2) etc.. Then in the Method called ConductExam() just use this private member where you will find all your learned individual. – Dhananjay Apr 09 '12 at 06:48
  • it was hard to choose the best answer ,anyway thanks for the help :) – siamak Apr 12 '12 at 21:02
  • ok.. no worries. As long as your concerns are cleared it does not matter which one you mark as correct. Thanks. – Dhananjay Apr 13 '12 at 02:21
0

The caller would be the code that calls the thing that implements the interface. The Callee would be an object that implements an interface. Although people rarely use this terminology.

Here's an example in (java-inspired) pseudocode:

interface readable {
    function read();
}

//callee
class book implements readable {
    function read() {
        print this.text;
    }
    //other implementation code goes here
}

//caller
define readable b = new book();
b.read();
alexg
  • 3,015
  • 3
  • 23
  • 36