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.
Let’s see the following class diagram for the 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.
also read:
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.
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 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.
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.
There are certain advantages of using business delegate pattern. Some of them are listed below:
Let’s see the following Sample Implementation of Business Delegate.
BusinessService.java
/** * */ package com.doj.patterns.j2ee.businessdelegate; /** * @author Dinesh.Rajput * */ public interface BusinessService { void processExecution(); }
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"); } }
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(); } } }
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(); } }
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(); } }
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(); } }
Executing task by invoking EJB Service Executig task by invoking JMS Service
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…