Design Patterns Business Delegate Pattern


The Business Delegate Pattern is used to decouple the presentation layer from the business layer. It is basically used to reduce communication or remote query capabilities for business layer code in presentation layer code. In the business layer we have the following entities.

  • Client - Indicates that the layer code can be JSP, servlet, or UI java code.
  • Business Delegate - An entry class for client entities that provides access to business service methods.
  • LookUp Service - The lookup service object is responsible for obtaining the relevant business implementation and providing access to the business representative object by the business object.
  • Business Service - Business Service Interface. The entity class that implements the business service provides the actual business implementation logic.

Implementation

We will create Client, BusinessDelegate, BusinessService, LookUpService, JMSService And EJBService to represent the various entities in the agent model.

BusinessDelegatePatternDemo, our demo class uses BusinessDelegate and Client to demonstrate the usage of the agent mode.

design Patterns Business Delegate Pattern diagram2

Step 1

Create a BusinessService interface.

BusinessService.java

public interface BusinessService { public void doProcessing(); }

Step 2

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"); } }

Step 3

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(); } } }

Step 4

Create a 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(); } }

Step 5

Create a client.

Client.java

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

Step 1

Create a BusinessService interface.

BusinessService.java

BusinessService.java

public interface BusinessService { public void doProcessing(); }

Step 2

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"); } }

Step 3

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(); } } }

Step 4

Create a 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(); } }

Step 5

Create a client.

Client.java

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

Step 6

Use the BusinessDelegate and Client classes to demonstrate agent mode.

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(); } }

Step 7

Executing the program, outputting results:

Processing task by invoking EJB Service
Processing task by invoking JMS Service





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition