Single Dispatch

SingleDispatch.java

public class SingleDispatch 
{
	public void print()
	{
	  System.out.println("Single Dispatch");	
	}

 	public static void main(String[] args) 
	{
	  SingleDispatch objSingleDis = new SingleDispatch();
 	  objSingleDis.print();	
        }
}

Dynamic Dispatch
Dynamic dispatch is the same thing which we do in strategy pattern.The actual method which is called is known at the runtime

Account.java

public interface Account 
{
  public void calculateinterest();
}

SavingsAccount.java

public class SavingsAccount implements Account
{
	@Override
	public void calculateinterest() {
		System.out.println("Intrest is 8%");
	}
}

LoanAccount.java

public class LoanAccount implements Account
{
	@Override
	public void calculateinterest() {
		System.out.println("Intrest is 11.5%");
	}

}

CalculateInterest.java

public class CalculateInterest 
{
	public static void main(String[] args) 
	{
		Account objSavAcc = new SavingsAccount();
		Account objLoanAcc = new LoanAccount();
		
		objSavAcc.calculateinterest();
		objLoanAcc.calculateinterest();
	}
}

What is Multiple Dispatch
Account.java

public class Account 
{	
	public void calculateinterest() {
		System.out.println("Intrest is 11.5%");
	}
	
	public void calculateinterest(int prePayment) {
		System.out.println("Intrest is 11.5% with prePayment");
	}
	
	public void calculateinterest(int prePayment, boolean floatingIntrest) {
		System.out.println("Intrest is 11.5% with floatingIntrest");
	}
}

Now in the above example we have a Account class where the functions are overloaded.Now when the code gets executed then the methods are chosen based on the parameters passed.

public class CalculateInterest 
{
	public static void main(String[] args) 
	{
		Account objAcc = new Account();		
		
		objAcc.calculateinterest();
		objAcc.calculateinterest(23);
		objAcc.calculateinterest(23, true);
	}
}

Output

Intrest is 11.5%
Intrest is 11.5% with prePayment
Intrest is 11.5% with floatingIntrest

Now Java doesn’t supports multiple dispatch as above.The above code is an example of overloading.

Overloading vs Multiple Dispatch
The Difference between overloading and Multiple Dispatch is when the method to be called is decided at compile time then it is Overloading, if the method is decided at runtime then it is multiple dispatch

What is Double Dispatching?
In Double Dispatching the choosing of the method happens dynamically twice.In the below example the method is chosen similar to strategy pattern first time during call of viewReport method and again during choosing which printReport method to be called based on the class type its is called similar to

Staff.java

public interface Staff 
{
	void viewReport(Report objReport);
}

Teacher.java

public class Teacher implements Staff
{
	@Override
	public void viewReport(Report objReport) 
	{
                System.out.println("View Report of Teacher");
		objReport.printReport(this);
	}
}

Principal.java

public class Principal implements Staff
{
	@Override
	public void viewReport(Report objReport) 
	{		
                System.out.println("View Report of Principal");
		objReport.printReport(this);		
	}
}

Report.java

public class Report 
{
	public void printReport(Teacher objTeacher)
	{
		System.out.println("Can print report of her class"); 
	}
	
	public void printReport(Principal objPrincipal)
	{
		System.out.println("Can print report of all the class");
	}
}

ShowReport.java

public class ShowReport 
{
	public static void main(String[] args) 
	{
		Principal objPrincipal = new Principal();
		Teacher objTeacher   = new Teacher();
		objPrincipal.viewReport(new Report());
		objTeacher.viewReport(new Report());
	}
}

Output.java

View Report of Principal
Can print report of all the class
View Report of Teacher
Can print report of her class

In statically typed languages, including Java, the biggest difference between dispatch and overloading is that overloading is based on the static type of parameters (i.e. the choice of which method is actually called is decided at compile-time), while dispatch is based on the dynamic types (i.e. the decision is made at runtime). (Such languages usually don’t support multiple dispatch.)

Another Example of Double Dispatch is Serialization.In Serialization the class which is Serializable calls the methods with itself as an argument.In the below example the writeObject method dispatches the call back to the ObjectOutputStream thus making this a double dispatch. ObjectOutputStream delegates back MuxPrinter the responsibility of writing its state onto stream. By Doing this ObjectOutputStream has decoupled itself from our object objMuxPrt.

public class MuxPrinter implements Serializable
{

}

MuxPrinter objMuxPrt = new MuxPrinter();
ObjectOutputStream oos = new ObjectOutputStream();
oos.writeObject(objMuxPrt);  

Comments are closed.