Business Delegate Pattern

Business Delegate Pattern is used to decouple presentation tier and business tier. It is basically used to reduce communication or remote lookup functionality to business tier code in presentation tier code. In business tier there are following entities :

  • Client – Presentation tier code may be JSP, Servlet or UI java code
  • Business Delegate – single entry point class for client entities to provide access to Business Service methods
  • Lookup Service – Lookup service object is responsible to get relative business implementation and provide business object access to business delegate object
  • Business Service – Business Service interface. Concrete classes implement this business service to provide actual business implementation logic

An example contains Client, BusinessDelegate, BusinessService, LookUpService, JMSService and EJBService representing various entities of Business Delegate patterns. BusinessDelegateDemo shows the use of pattern.

Business Delegate Pattern UML Diagram

Step 1 : Create BusinessService interface

public interface BusinessService {
void doProcessing();
}

Step 2 : Create concrete Service classes

public class EJBService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking EJB Service");
}
}

public class JMSService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking JMS Service");
}
}

Step 3 : Create Business LookUp Service

public class BusinessLookUp {
public BusinessService getBusinessService(String serviceType) {
if (serviceType.equalsIgnoreCase("EJB")) {
return new EJBService();
} else {
return new JMSService();
}
}
}

Step 4 : Create Business Delegate

public class BusinessDelegate {
private BusinessLookUp loopUpService = new BusinessLookUp();
private BusinessService businessService;
private String serviceType;

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

public void doTask() {
businessService = loopUpService.getBusinessService(serviceType);
businessService.doProcessing();
}
}

Step 5 : Create Client

public class Client {
BusinessDelegate businessDelegate;

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

public void doTask() {
businessDelegate.doTask();
}
}

Step 6 : Use BusinessDelegate and Client classes to demonstrate Business Delegate pattern

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();
}
}

The output will be :

Processing task by invoking EJB Service
Processing task by invoking JMS Service

Leave a Reply

Your email address will not be published. Required fields are marked *