Design Patterns - SPLessons

Business Delegate Pattern

Chapter 31

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Business Delegate Pattern

Business Delegate Pattern

shape Description

Business Delegate Pattern is utilized to decouple presentation level and business level. It is essentially use to lessen correspondence or remote query usefulness to business level code in presentation level code. In business level we’ve taking after substances. Customer – Presentation level code might be JSP, servlet code. Utilize a Business Delegate to decrease coupling between presentation-level customers and business administrations. The Business Delegate shrouds the basic execution points of interest of the business administration, for example, query and get to subtle elements of the EJB engineering.

Business Delegate Pattern goes about as a client side business reflection it gives a deliberation to, and in this way shrouds, the execution of the business administrations. Utilizing a Business Delegate decreases the coupling between presentation-level customers and the framework’s business administrations. Contingent upon the execution procedure, the Business Delegate may shield customers from conceivable unpredictability in the usage of the business administration API. Conceivably, this diminishes the quantity of changes that must be made to the presentation-level customer code when the business administration API or its fundamental execution changes.

Interface strategies in the Business Delegate may at present require alteration if the fundamental business administration API changes. As a matter of fact, however, it is more probable that progressions will be made to the business benefit as opposed to the Business Delegate.

shape Example

Following is an example to understand the Business Delegate Pattern.
BusinessService.java

public interface BusinessService {
   public void doProcessing();
}

Where BusinessService is the interface.
EJBService.java

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

Where EJB service is the concrete Service class and that is implementing the interface BusinessService.
JMSService.java

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

Where JMS service is the concrete Service class and that is implementing the interface BusinessService.
BusinessLookUp.java

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

Here created the Business Lookup Service and written the logic’s for both concrete classes. The BusinessDelegate utilizes a LookupService for finding the business administration. The business administration is utilized to summon the business techniques in the interest of the customer. The Get ID technique demonstrates that the BusinessDelegate can get a String form of the handle for the business administration and return it to the customer as a String. The customer can utilize the String rendition of the handle at a later time to reconnect to the business administration it was utilizing when it got the handle. This system will dodge new queries, since the handle is fit for reconnecting to its business administration occasion.
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();		
   }
}

Here created the Business delegate, Another advantage is that the delegate may store results and references to remote business administrations. Storing can altogether enhance execution, since it limits superfluous and possibly expensive round excursions over the system
Client.java

public class Client {	
   BusinessDelegate businessService;
   public Client(BusinessDelegate businessService){
      this.businessService  = businessService;
   }
   public void doTask(){		
      businessService.doTask();
   }
}

Here just created the constructor to the class, where this keyword is used to refer the current object.
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();
   }
}

A Business Delegate Pattern utilizes a part called the Lookup Service. The Lookup Service is in charge of concealing the basic usage points of interest of the business administration query code. The Lookup Service might be composed as a major aspect of the Delegate, however we suggest that it be executed as a different segment, as delineated in the Service Locator design.
At the point when the Business Delegate is utilized with a Session Facade, regularly there is a coordinated relationship between the two. This balanced relationship exists since rationale that may have been embodied in a Business Delegate identifying with its association with numerous business administrations (making a one-to-numerous relationship) will frequently be calculated once again into a Session Facade.
Output
Now compile the code result will be as follows.

SPLESSON - Processing task by invoking EJB Service
SPLESSON - Processing task by invoking JMS Service

Summary

shape Key Points

  • The BusinessDelegate’s part is to give control and insurance to the business administration.
  • The LookupService is used to encapsulate the implementation details of BusinessService lookup.