Callback Operation By Delegate Or Interface

Introduction

A "callback" is a term that refers to a coding design pattern. In this design pattern executable code is passed as an argument to other code and it is expected to call back at some time. This callback can be synchronous or asynchronous. So, in this way large piece of the internal behavior of a method from the outside of a method can be controlled. It is basically a function pointer that is being passed into another function.

Delegate is a famous way to implement Callback in C#.  But, it can also be implemented by Interface. I will explain Callback by Delegate and Interface one by one. 

Callback by Delegate 

Delegate provides a way to pass a method as argument to other method. To create a Callback in C#, function address will be passed inside a variable. So, this can be achieved by using Delegate.

The following is an example of Callback by Delegate

public delegate void TaskCompletedCallBack(string taskResult);  
public class CallBack  
{  
    public void StartNewTask(TaskCompletedCallBack taskCompletedCallBack)  
    {  
        Console.WriteLine("I have started new Task.");  
        if (taskCompletedCallBack != null)  
            taskCompletedCallBack("I have completed Task.");  
    }  
}
public class CallBackTest  
{  
    public void Test()  
    {  
        TaskCompletedCallBack callback = TestCallBack;   
        CallBack testCallBack = new CallBack();  
        testCallBack.StartNewTask(callback);  
    }    
    public void TestCallBack(string result)  
    {  
        Console.WriteLine(result);  
    }  
}
static void Main(string[] args)  
{     
    CallBackTest callBackTest = new CallBackTest();  
    callBackTest.Test();  
    Console.ReadLine();  
}

Output

I have started new Task.

I have completed Task.

Delegate is a good way to implement Callback. But, you could use Interface for this. Because, suppose you have two methods - one for the success and another for the error and these methods will use Callback, so if you will use Delegate you will have to take two Delegates. 

If you need more than one Callback method then Callback mechanism with the use of Delegate doesn't makes sense. So, the use of Interface provides flexible and well-performing Callback mechanism for this scenario. 

Callback by Interface

Use an Interface to provide Callback mechanism. It provides flexible and well-performing Callback mechanism. The following code example will elaborate the Callback mechanism with the help of Interface:

public interface IMeeting  
{  
    void ShowAgenda(string agenda);  
    void EmployeeAttendedMeeting(string employee);  
    void EmployeeLeftMeeting(string employee);  
}
public class Meeting : IMeeting  
{  
    public void ShowAgenda(string agenda)  
    {  
        Console.WriteLine("Agenda Details: " + agenda);  
    }  

    public void EmployeeAttendedMeeting(string employee)  
    {  
        Console.WriteLine("Employee Attended Meeting: " + employee);  
    }  
  
    public void EmployeeLeftMeeting(string employee)  
    {  
        Console.WriteLine("Employee Left Meeting: " + employee);  
    }  
}
public class MeetingRoom  
{  
    private string message;  
    public MeetingRoom(string message)  
    {  
        this.message = message;  
    }  

    public void StartMeeting(IMeeting meeting)  
    {  
        // Its a callback  
        if (meeting != null) meeting.ShowAgenda(message);  
  
    }  
} 
public class MeetingExecution  
{  
    public void PerformMeeting()  
    {  
        IMeeting meeging = new Meeting();  
        MeetingRoom meetingStarted = new MeetingRoom("Code Quality Improvement.");  
        meetingStarted.StartMeeting(meeging);  
    }  
}
static void Main(string[] args)  
{  
    MeetingExecution meetingExecution = new MeetingExecution();  
    meetingExecution.PerformMeeting();  
            
    Console.ReadLine();  
}

Output

Agenda Details: Code Quality Improvement.

Conclusion

Delegate is a great way to implement Callback operation. But, interface is good for the implementation of Callback in different scenario which I have mentioned above. Please read carefully and understand the code snippets. I have also attached code files. Writing code is good but code with proper design approach increases the quality of code. So, before writing any code always try to follow some design approach. Your code will be awesome.


Similar Articles