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 .

4

3 回答 3

2

实现接口的人是被调用者,因为他提供了接口的实现。消费者是使用被调用者对象的人,所以他们称之为调用者

编辑

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}
}

在代码中的某处:

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
   }

}
于 2012-04-07T07:39:15.340 回答
2

学校是许多个人来学习的地方。每个人都有不同的学习方式。

学校有一个规则:任何进来的个人都必须是可学习的。

学校确信,如果一个人不是可学习的,那么它就不能教他们,因此
他们就不能学习。

每个 INDIVIDUAL 都实现 ILEARNABLE 接口

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

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

学校通过一种叫做 Teach() 的方法教他们

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

}

这里学校只要实现了 ilearnable 接口就不用担心谁是个体。

于 2012-04-07T18:28:27.080 回答
0

调用者将是调用实现接口的事物的代码。被调用者将是一个实现接口的对象。尽管人们很少使用这个术语。

这是(java-inspired)伪代码中的一个示例:

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();
于 2012-04-07T07:38:19.303 回答