Agent mode (Business Delegate Pattern)

Agent Mode (Business Delegate Pattern) for decoupling layer and business layer. It is basically to reduce communication or business layer remote query function code indicates the code layer. In the business layer, we have the following entities. The client (Client) - presentation code may be a JSP, servlet, or UI java code. Business representatives (Business Delegate) - an entry class entity to provide for the client, which provides access to business service method. Query Service (LookUp ..


Agent Mode (Business Delegate Pattern) for decoupling layer and business layer. It is basically to reduce communication or business layer remote query function code indicates the code layer. In the business layer, we have the following entities.

  • The client (Client)  - presentation code may be a JSP, servlet, or UI java code.

  • Business representatives (Business Delegate)  - an entry class entity to provide for the client, which provides access to business service method.

  • Query Service (LookUp Service)  - Find service object is responsible for obtaining the relevant business implementation, and provide access to business objects representative objects.

  • Business services (Business Service)  - business service interface. Entity class implements the business service, offers a practical realization of business logic.

achieve

We will create the  Client , BusinessDelegate , the BusinessService , LookupService , JMSService  and  EJBService  to represent various entities agent mode.

BusinessDelegatePatternDemo , our demonstration classes use  BusinessDelegate  and  Client  to demonstrate the usage patterns of business representatives.

business_delegate_pattern_uml_diagram.jpg

step 1

Creating BusinessService interface.

BusinessService.java

public interface BusinessService {
   public void doProcessing();
}

Step 2

Create an entity class service.

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

Step 5

Create a client.

Student.java

public class Client {
	
   BusinessDelegate businessService;

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

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

Step 6

Use BusinessDelegate and Client classes to demonstrate the 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

Verify the output.

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

Guess you like

Origin www.cnblogs.com/benbenhan/p/12396501.html