Business Delegate Pattern
The Business Delegate Pattern is used to decouple the presentation layer and the business layer. It is essentially used to reduce communication or remote lookup functionality in the presentation layer code. In the business layer, we have the following entities.
- Client - The presentation layer code can be JSP, servlet, or UI java code.
- Business Delegate - A single entry point class for the client entities to provide access to the business service methods.
- Lookup Service - The lookup service object is responsible for obtaining the relevant business implementation and providing business object access to the business delegate object.
- Business Service - The business service interface. Concrete classes implement this business service to provide the actual business implementation logic.
Implementation
We will create Client, BusinessDelegate, BusinessService, LookupService, JMSService, and EJBService to represent the various entities in the Business Delegate Pattern.
The BusinessDelegatePatternDemo class uses BusinessDelegate and Client to demonstrate the usage of the Business Delegate Pattern.
Step 1
Create the BusinessService interface.
BusinessService.java
public interface BusinessService {
public void doProcessing();
}
Step 2
Create the entity service classes.
EJBService.java
public class EJBService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking EJB Service");
}
}
JMSService.java
public class JMSService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking JMS Service");
}
}
Step 3
Create the business lookup service.
BusinessLookUp.java
public class BusinessLookUp {
public BusinessService getBusinessService(String serviceType){
if(serviceType.equalsIgnoreCase("EJB")){
return new EJBService();
}else {
return new JMSService();
}
}
}
Step 4
Create the business delegate.
BusinessDelegate.java
public class BusinessDelegate {
private BusinessLookUp lookupService = new BusinessLookUp();
private BusinessService businessService;
private String serviceType;
public void setServiceType(String serviceType){
this.serviceType = serviceType;
}
public void doTask(){
businessService = lookupService.getBusinessService(serviceType);
businessService.doProcessing();
}
}
Step 5
Create the client.
Client.java
public class Client {
BusinessDelegate businessService;
public Client(BusinessDelegate businessService){
this.businessService = businessService;
}
public void doTask(){
businessService.doTask();
}
}
Step 6
Use the BusinessDelegate and Client classes to demonstrate the Business Delegate Pattern.
BusinessDelegatePatternDemo.java
public class BusinessDelegatePatternDemo {
public static void main(String[] args) {
BusinessDelegate businessDelegate = new BusinessDelegate();
businessDelegate.setServiceType("EJB");
Client client = new Client(businessDelegate);
client.doTask();
businessDelegate.setServiceType("JMS");
client.doTask();
}
}
Step 7
Execute the program, output results:
Processing task by invoking EJB Service
Processing task by invoking JMS Service