Business Delegate Pattern – Core J2EE Patterns

The Business Delegate Pattern is one of the Core Java EE design patterns. It is used in order to decouple or reduce the coupling between the presentation tier and business services. It is also required to hide the details of implementation of the services, meaning it is needed to remove the function of lookup in the business tier code within the presentation tier code.

In order to call the business object which are present in the presentation tier, the business delegates tend to act as adapters. The structure of the business delegate pattern is such that, there is a client who invokes a request and connects with the business delegate object which uses the business service. The lookup service creates the business service that the business delegate access.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.

Class Diagram for Business Delegate Pattern

Let’s see the following class diagram for the business delegate pattern.

Business Delegate Pattern
Use a Business Delegate to encapsulate access to a business service. The Business Delegate hides the implementation details of the business service, such as lookup and access mechanisms.

The body of business tier contains following elements; business delegates, lookup service, business service. All of these elements or the objects have certain tasks to perform and certain problems to cater.

Business Delegate

The business delegate object is responsible for providing protection and control to the business tier. This object provides two types of contractures- with an ID or without an ID. The first type of request represents the business delegate with an ID while another one represents it without an ID. Meanwhile ID is considered to be a reference in the form of a string to a remote object. These objects might include; EJBObject or EJBHome.

Business delegate calls the service from the lookup service and initializes without an ID. Generally, the implementation served by the Service Locator which is responsible for returning the service factory, like EBJHome. The business delegate calls the service factory so that it would locate a business service, create it or remove it.

The business delegate objects tens to reconnect with the business service using the ID string when it is initialized with an ID reference. In this way, the business delegate prevents the details of implementation underneath (like lookup) from the client. Apart from this, the client of presentation tier does not directly communicate with the business session. Instead of doing that, it communicates with the business delegate object.

Lookup Service

Lookup service is used in order to locate the business service. The business delegate object tends to use it, while it encases the details of implementation of business service lookup.

Business Service

It is a component of the business tier. For example, a JMS component or an enterprise bean. These are responsible for providing the needed service to the client.

Advantages of using Business Delegate

There are certain advantages of using business delegate pattern. Some of them are listed below:

  • It removes the coupling between business tier and presentation tier, which in turn, improves the manageability of the system.
  • The pattern implements thread recognition and failure recovery.
  • It provides the business tier, a safer, uniform and simpler interface as a communication medium in order for them to serve their clients better.
  • It translates business service exceptions by hiding the details of underlying implementations form the client.

Sample Implementation of Business Delegate

Let’s see the following Sample Implementation of Business Delegate.

Step 1: Let’s create BusinessService Interface.

BusinessService.java

/**
 * 
 */
package com.doj.patterns.j2ee.businessdelegate;

/**
 * @author Dinesh.Rajput
 *
 */
public interface BusinessService {
	
	void processExecution();
}

Step 2: Let’s create concrete Service classes.

EJBService.java

/**
 * 
 */
package com.doj.patterns.j2ee.businessdelegate;

/**
 * @author Dinesh.Rajput
 *
 */
public class EJBService implements BusinessService {

	@Override
	public void processExecution() {
		System.out.println("Executing task by invoking EJB Service");
	}

}

JMSService.java

/**
 * 
 */
package com.doj.patterns.j2ee.businessdelegate;

/**
 * @author Dinesh.Rajput
 *
 */
public class JMSService implements BusinessService {

	@Override
	public void processExecution() {
		System.out.println("Executig task by invoking JMS Service");
	}

}

Step 3: Let’s create Business Lookup Service.

BusinessLookUp.java

/**
 * 
 */
package com.doj.patterns.j2ee.businessdelegate;

/**
 * @author Dinesh.Rajput
 *
 */
public class BusinessLookUp {
	
	public BusinessService getBusinessService(String serviceType){
		   
		if(serviceType.equalsIgnoreCase("EJB")){
			return new EJBService();
		}else {
			return new JMSService();
		}
	}
}

Step 4: Let’s create Business Delegate.

BusinessDelegate.java

/**
 * 
 */
package com.doj.patterns.j2ee.businessdelegate;

/**
 * @author Dinesh.Rajput
 *
 */
public class BusinessDelegate {
	
	private BusinessLookUp  lookupService;
	
	private String serviceType;
	
	
	public BusinessDelegate() {
		super();
		lookupService = new BusinessLookUp();
	}

	public void setServiceType(String serviceType){
		this.serviceType = serviceType;
	}

	public void runProcess(){
		BusinessService businessService = lookupService.getBusinessService(serviceType);
		businessService.processExecution();		
	}
}

Step 5: Let’s create Client class.

Client.java

/**
 * 
 */
package com.doj.patterns.j2ee.businessdelegate;

/**
 * @author Dinesh.Rajput
 *
 */
public class Client {
	
	BusinessDelegate businessService;

	public Client(BusinessDelegate businessService){
		this.businessService  = businessService;
	}

	public void runProcess(){		
		businessService.runProcess();
	}
}

Step 6: Let’s use BusinessDelegate and Client classes to demonstrate Business Delegate pattern.

BusinessDelegatePatternDemo.java

/**
 * 
 */
package com.doj.patterns.j2ee.businessdelegate;

/**
 * @author Dinesh.Rajput
 *
 */
public class BusinessDelegatePatternDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		BusinessDelegate businessDelegate = new BusinessDelegate();
		businessDelegate.setServiceType("EJB");

		Client client = new Client(businessDelegate);
		client.runProcess();

		businessDelegate.setServiceType("JMS");
		client.runProcess();
	}

}

Step 7: Let’s run this demo class and verify the output.

Executing task by invoking EJB Service
Executig task by invoking JMS Service

Previous
Next