Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i have read a article using "interface as callback".
https://www.c-sharpcorner.com/UploadFile/1c8574/delegate-used-for-callback-operation/

then I see that many occasion, Api provides the interface for consumer. I wonder if they are usually working like this way? I think there are 4 steps
here is my code (please correct my comment if i misunderstand, thanks!)

first there is a Api
class Api
    {
        //Api request passing interface object into method      
        public void StartNewTask(IFurtherProcess Interface_Obj_Feedback_from_user)    
        {
            Console.WriteLine("started task");

          //I am Api , I only tell you I started a task , below further process is up to consumer.

            Interface_Obj_Feedback_from_user.process(); // process is sometime Adding, sometime Subtract 
           
        }

    }
    interface IFurtherProcess
    {   
        void process();       //step 1 : Api declare a Interface with method
    }


then there is Class to implement the interface by Consumer

public class ProcessClass1 : IFurtherProcess      //step 2: Api Consumer design class to implement the interface;
    {
        
        public void process()
        {
            Console.WriteLine("add");
            
        }
    }
    public class ProcessClass2 : IFurtherProcess      //step 2: Api Consumer design alternative class implement the interface;
    {

        public void process()
        {
            Console.WriteLine("sub");
            
        }
    }


then there is a ApiConsumer Class:

class ApiConsumer    //created by consumer
   {
       public IFurtherProcess _ifp=null;
           public ApiConsumer(IFurtherProcess ifp)   //leave it for main to choose what ifp is ?
       {
           this._ifp = ifp;

       }
      public void CallApiMethod()
       {
           Api cs = new Api();
           cs.StartNewTask(_ifp);
       }

   }


finally, it is the main:

static void Main(string[] args)
      {
          ApiConsumer apiConsumer = new ApiConsumer(new ProcessClass2());  // Step 4:i choose Process2 here!
          apiConsumer.CallApiMethod();
          Console.ReadLine();
      }


What I have tried:

furthermore, it seems to have too many layer. Is it so called "wrapper" what i am doing above? main->apiconsumer->api .
Posted
Updated 16-May-18 17:32pm
v2
Comments
Richard MacCutchan 17-May-18 3:12am    
You should ask the person who wrote the article.
F-ES Sitecore 17-May-18 4:47am    
Like any of these things you would use them if it fits your requirements. It's effectively a different way of implementing events. In your specific implementation you could probably get away with the Api class and just invoke the interface methods in "CallApiMethod".

Also remember this is for you to supply code that executes *after* the API has done its work, so in your add\subtract example ProcessClass1\2 won't be doing the actual adding and subtracting, those classes will be for any work that needs done after the API has done its work, so this might be logging, updating the display, possibly starting a second task and so on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900