Business Delegate Design Pattern in Java

The Business Delegate pattern adds an abstraction layer between presentation and business tiers. By using the pattern we gain loose coupling between the tiers and encapsulate knowledge about how to locate, connect to, and interact with the business objects that make up the application.
Business Delegate Pattern is used to decouple presentation tier and business tier.
Let's discuss how Business Delegate Pattern decouples presentation tier and business tier with a class diagram and source code.
This pattern is divided into a number of sections for simplicity like problem, forces, solution etc.

Table of contents
Problem
Forces
Solution
Structure - Class Diagram, Sequence Diagram
Participants and Responsibilities
Implementation
Consequences
Applicability
References

Problem

(Problem section describes the design issues faced by the developer)
You want to hide clients from the complexity of remote communication with business service components.

Forces

(This section describes Lists the reasons and motivations that affect the problem and the solution. The list of forces highlights the reasons why one might choose to use the pattern and provides a justification for using the pattern)
  • You want to access the business-tier components from your presentation-tier components and clients, such as devices, web services, and rich clients.
  • You want to minimize coupling between clients and the business services, thus hiding the underlying implementation details of the service, such as lookup and access.
  • You want to avoid unnecessary invocation of remote services.
  • You want to translate network exceptions into application or user exceptions.
  • You want to hide the details of service creation, reconfiguration, and invocation retries from the clients.

Solution

(Here solution section describes the solution approach briefly and the solution elements in detail)
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.

Structure

Let's use UML class diagram to show the basic structure of the solution and the UML Sequence diagram in this section present the dynamic mechanisms of the solution.

Class Diagram

Sequence Diagram

Participants and Responsibilities

BusinessDelegate - The BusinessDelegate’s role is to provide control and protection for the business service.
ServiceLocator - The ServiceLocator encapsulates the implementation details of locating a BusinessService component.
BusinessService - The BusinessService is a business-tier component, such as an enterprise bean, that is being accessed by the client.
Client - Client can be JSP or Servlet or UI code

Implementation

(This section includes source code implementations and code listings for the patterns)
Let's refer the above class diagram and write source code to define Business Delegate Pattern.
Let's create source code step by step with reference to the class diagram.
Step 1: Create BusinessService Interface.
Interface for Business service implementations like JMSService and EJBService.
public interface BusinessService {
  void doProcessing();
}
Step 2 : Create concrete Service classes.
This is Service EJB implementation.
public class EJBService implements BusinessService {
  @Override
  public void doProcessing() {
    System.out.println("EJBService is now processing");
  }
} 
//This is Service JMS implementation.

public class JMSService implements BusinessService {
  @Override
  public void doProcessing() {
    System.out.println("JMSService is now processing");
  }
} 
//Service Email implementation.

public class EmailService implements BusinessService {
  @Override
  public void doProcessing() {
    System.out.println("EmailService is now processing");
  }
}
Step 3 : Create Business Lookup Service.
Class for performing service lookups.
This class acts as ServiceLocator encapsulates the implementation details of locating BusinessService components.
public class BusinessLookup {

 private EjbService ejbService;

 private JmsService jmsService;

 private EmailService emailService;

 /**
  * @param serviceType
  *            Type of service instance to be returned.
  * @return Service instance.
  */
 public BusinessService getBusinessService(ServiceType serviceType) {
  if (serviceType.equals(ServiceType.EJB)) {
   return ejbService;
  } else if (serviceType.equals(ServiceType.JMS)) {
   return jmsService;
  } else {
   return emailService;
  }
 }

 public void setJmsService(JmsService jmsService) {
  this.jmsService = jmsService;
 }

 public void setEjbService(EjbService ejbService) {
  this.ejbService = ejbService;
 }

 public void setEmailService(EmailService emailService) {
  this.emailService = emailService;
 }
}
Step 4: Create Business Delegate.
BusinessDelegate separates the presentation and business tiers.
public class BusinessDelegate {

  private BusinessLookup lookupService;
  private BusinessService businessService;
  private ServiceType serviceType;

  public void setLookupService(BusinessLookup businessLookup) {
    this.lookupService = businessLookup;
  }

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

  public void doTask() {
    businessService = lookupService.getBusinessService(serviceType);
    businessService.doProcessing();
  }
}
 Enumeration of service types

public enum ServiceType {
  EJB, JMS,EMAIL;
}
Step 5: Create Client. The client utilizes BusinessDelegate to call the business tier.
public class Client {

  private 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.
In this example, the client utilizes a business delegate to execute a task. The Business Delegate then selects the appropriate service and makes the service call.
public class App {

  /**
   * Program entry point.
   *
   * @param args command line args
   */
  public static void main(String[] args) {

    BusinessDelegate businessDelegate = new BusinessDelegate();
    BusinessLookup businessLookup = new BusinessLookup();
    businessLookup.setEjbService(new EjbService());
    businessLookup.setJmsService(new JmsService());
    businessLookup.setEmailService(new EmailService());

    businessDelegate.setLookupService(businessLookup);
    businessDelegate.setServiceType(ServiceType.EJB);

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

    businessDelegate.setServiceType(ServiceType.JMS);
    client.doTask();
    
    businessDelegate.setServiceType(ServiceType.EMAIL);
    client.doTask();
  }
}
The source code of this pattern is available on GitHub.

Applicability

Use the Business Delegate pattern when
  • you want loose coupling between presentation and business tiers
  • you want to orchestrate calls to multiple business services
  • you want to encapsulate service lookups and service calls

References

Related Patterns

Comments