Latest web development tutorials

Agent mode

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

  • Client (Client) - presentation layer code can be JSP, servlet, or UI java code.
  • Agent (Business Delegate) - a class entry for the client entity, which provides access to the business service method.
  • Query Service (LookUp Service) - Find service object is responsible for obtaining the relevant business to achieve, and provides access to the business objects representative objects.
  • Business services (Business Service) - business service interface.Entity class that implements the business service, providing actual business logic to achieve.

achieve

We will create theClient, BusinessDelegate, BusinessService, LookUpService,JMSService andEJBServiceto represent the business model on behalf of various entities.

BusinessDelegatePatternDemo,our demonstration class to demonstrate the use ofBusinessDelegateandClientagent mode usage.

Agent model UML diagram

step 1

Create BusinessService interface.

BusinessService.java

public interface BusinessService {
   public void doProcessing ();
}

Step 2

Create 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 class 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 output.

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