<p>The <strong>Business Delegate</strong> Pattern is one of the<strong> <a href="https://dineshonjava.com/core-j2ee-patterns-best-design-practices/">Core Java EE design patterns</a>.</strong> 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.</p>
<p>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.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput"><br />
<img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://i0.wp.com/dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern.jpg?resize=243%2C300&;ssl=1" alt="Spring-5-Design-Pattern" width="243" height="300" /></a></div>
</div>
<h2>Class Diagram for Business Delegate Pattern</h2>
<p>Let&#8217;s see the following class diagram for the business delegate pattern.</p>
<p><img class="aligncenter size-full wp-image-4049" src="https://dineshonjava.com/wp-content/uploads/2018/01/Business-Delegate.gif" alt="Business Delegate Pattern" width="566" height="245" /><br />
<em><strong>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.</strong></em></p>
<div style="text-align: left;">
<blockquote>
<p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/service-locator/" href="https://dineshonjava.com/service-locator/">Service Locator Pattern</a></li>
<li><a title="https://dineshonjava.com/session-facade/" href="https://dineshonjava.com/session-facade/">Session Facade Pattern</a></li>
<li><a title="https://dineshonjava.com/business-object/" href="https://dineshonjava.com/business-object/">Business Object Pattern</a></li>
<li><a title="https://dineshonjava.com/composite-entity-pattern/" href="https://dineshonjava.com/composite-entity-pattern/">Composite Entity Pattern</a></li>
<li><a title="https://dineshonjava.com/transfer-object/" href="https://dineshonjava.com/transfer-object/">Transfer Object Pattern</a></li>
</ul>
</blockquote>
</div>
<p>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.</p>
<h3>Business Delegate</h3>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<h3>Lookup Service</h3>
<p>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.</p>
<h3>Business Service</h3>
<p>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.</p>
<h2>Advantages of using Business Delegate</h2>
<p>There are certain advantages of using business delegate pattern. Some of them are listed below:</p>
<ul>
<li>It removes the coupling between business tier and presentation tier, which in turn, improves the manageability of the system.</li>
<li>The pattern implements thread recognition and failure recovery.</li>
<li>It provides the business tier, a safer, uniform and simpler interface as a communication medium in order for them to serve their clients better.</li>
<li>It translates business service exceptions by hiding the details of underlying implementations form the client.</li>
</ul>
<h2>Sample Implementation of Business Delegate</h2>
<p>Let&#8217;s see the following Sample Implementation of Business Delegate.</p>
<h3>Step 1: Let&#8217;s create BusinessService Interface.</h3>
<p><strong>BusinessService.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.j2ee.businessdelegate; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public interface BusinessService { 
	 
	void processExecution(); 
} 
 
</pre>
<h3>Step 2: Let&#8217;s create concrete Service classes.</h3>
<p><strong>EJBService.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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"); 
	} 
 
} 
 
</pre>
<p><strong>JMSService.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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"); 
	} 
 
} 
 
</pre>
<h3>Step 3: Let&#8217;s create Business Lookup Service.</h3>
<p><strong>BusinessLookUp.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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(); 
		} 
	} 
} 
 
</pre>
<h3>Step 4: Let&#8217;s create Business Delegate.</h3>
<p><strong>BusinessDelegate.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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();		 
	} 
} 
 
</pre>
<h3>Step 5: Let&#8217;s create Client class.</h3>
<p><strong>Client.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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(); 
	} 
} 
 
</pre>
<h3>Step 6: Let&#8217;s use BusinessDelegate and Client classes to demonstrate Business Delegate pattern.</h3>
<p><strong>BusinessDelegatePatternDemo.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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(); 
	} 
 
} 
 
</pre>
<h3>Step 7: Let&#8217;s run this demo class and verify the output.</h3>
<pre class="highlight">Executing task by invoking EJB Service 
Executig task by invoking JMS Service 
 
</pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/core-j2ee-patterns-best-design-practices/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/service-locator/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4046'});
});
</script>
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…