May 27, 2021 Design mode
The Business Delegate Pattern is used to decouple the representation and business layers. I t is basically used to reduce the ibility of communication or remote querying of business layer code in the representing layer code. We have the following entities in the business tier.
We'll create Client, BusinessDelegate, BusinessService, LookUpService, JMSService, and EJBService to represent the various entities in the business representative model.
BusinessDelegate PotternDemo, our demo class uses BusinessDelegate and Client to demonstrate the use of business representation patterns.
Create a BusinessService interface.
BusinessService.java
public interface BusinessService { public void doProcessing(); }
Create an entity service class.
EJBService.java
public class EJBService implements BusinessService { @Override public void doProcessing() { System.out.println("Processing task by invoking EJB Service"); } }
JMSService.java
public class JMSService implements BusinessService { @Override public void doProcessing() { System.out.println("Processing task by invoking JMS Service"); } }
Create a business query service.
BusinessLookUp.java
public class BusinessLookUp { public BusinessService getBusinessService(String serviceType){ if(serviceType.equalsIgnoreCase("EJB")){ return new EJBService(); }else { return new JMSService(); } } }
Create a business representative.
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(); } }
Create a client.
Client.java
public class Client { BusinessDelegate businessService; public Client(BusinessDelegate businessService){ this.businessService = businessService; } public void doTask(){ businessService.doTask(); } }
Use the BusinessDelegate and Client classes to demonstrate the business representative model.
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(); } }
Verify the output.
Processing task by invoking EJB Service Processing task by invoking JMS Service