English 中文(简体)
Business Delegate Pattern
  • 时间:2024-09-17

Design Patterns - Business Delegate Pattern


Previous Page Next Page  

Business Delegate Pattern is used to decouple presentation tier and business tier. It is basically use to reduce communication or remote lookup functionapty to business tier code in presentation tier code. In business tier we have following entities.

    Cpent - Presentation tier code may be JSP, servlet or UI java code.

    Business Delegate - A single entry point class for cpent entities to provide access to Business Service methods.

    LookUp Service - Lookup service object is responsible to get relative business implementation and provide business object access to business delegate object.

    Business Service - Business Service interface. Concrete classes implement this business service to provide actual business implementation logic.

Implementation

We are going to create a Cpent, BusinessDelegate, BusinessService, LookUpService, JMSService and EJBService representing various entities of Business Delegate patterns.

BusinessDelegatePatternDemo, our demo class, will use BusinessDelegate and Cpent to demonstrate use of Business Delegate pattern.

Business Delegate Pattern UML Diagram

Step 1

Create BusinessService Interface.

BusinessService.java

pubpc interface BusinessService {
   pubpc void doProcessing();
}

Step 2

Create concrete Service classes.

EJBService.java

pubpc class EJBService implements BusinessService {

   @Override
   pubpc void doProcessing() {
      System.out.println("Processing task by invoking EJB Service");
   }
}

JMSService.java

pubpc class JMSService implements BusinessService {

   @Override
   pubpc void doProcessing() {
      System.out.println("Processing task by invoking JMS Service");
   }
}

Step 3

Create Business Lookup Service.

BusinessLookUp.java

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

Step 4

Create Business Delegate.

BusinessDelegate.java

pubpc class BusinessDelegate {
   private BusinessLookUp lookupService = new BusinessLookUp();
   private BusinessService businessService;
   private String serviceType;

   pubpc void setServiceType(String serviceType){
      this.serviceType = serviceType;
   }

   pubpc void doTask(){
      businessService = lookupService.getBusinessService(serviceType);
      businessService.doProcessing();		
   }
}

Step 5

Create Cpent.

Cpent.java

pubpc class Cpent {
	
   BusinessDelegate businessService;

   pubpc Cpent(BusinessDelegate businessService){
      this.businessService  = businessService;
   }

   pubpc void doTask(){		
      businessService.doTask();
   }
}

Step 6

Use BusinessDelegate and Cpent classes to demonstrate Business Delegate pattern.

BusinessDelegatePatternDemo.java

pubpc class BusinessDelegatePatternDemo {
	
   pubpc static void main(String[] args) {

      BusinessDelegate businessDelegate = new BusinessDelegate();
      businessDelegate.setServiceType("EJB");

      Cpent cpent = new Cpent(businessDelegate);
      cpent.doTask();

      businessDelegate.setServiceType("JMS");
      cpent.doTask();
   }
}

Step 7

Verify the output.

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