Java agent mode

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.

BusinessService.java

public interface BusinessService {// Create Interface BusinessService
    
    public void doProcessing ();
    
}

EJBService.java

public class EJBService implements BusinessService {// create the entity class service
    
    @Override
    public void doProcessing () {
        System.out.println ( "Processing Task by Invoking the EJB Service");
    }
    
}

JMSService.java

public class JMSService implements BusinessService {
    
    @Override
    public void doProcessing() {
        System.out.println("Processing task by invoking JMS Service");
    }
    
}

BusinessLookUp.java

public class BusinessLookUp {// Create a business inquiry service
    
    public the BusinessService getBusinessService (String serviceType) {
        IF (serviceType.equalsIgnoreCase ( "the EJB")) {
            return new new EJBService ();
        } the else {
            return new new JMSService ();
        }
    }
    
}

BussinessDelegate.java

public class BussinessDelegate { //创建业务代表
    
    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();
    }
    
}

Client.java

public class Client { //创建客户端
    
    BussinessDelegate bussinessDelegate;
    
    public Client(BussinessDelegate bussinessDelegate) {
        this.bussinessDelegate = bussinessDelegate;
    }
    
    public void doTask() {
        bussinessDelegate.doTask();
    }
    
}

BusinessDelegatePatternDemo.java

public class BusinessDelegatePatternDemo {// use BusinessDelegate and Client Agent class to illustrate the model
    
    public static void main (String [] args) {
        
        BussinessDelegate bussinessDelegate new new BussinessDelegate = ();
        bussinessDelegate.setServiceType ( "the EJB");
        
        Client Client Client new new = ( bussinessDelegate);
        client.doTask ();
        
        bussinessDelegate.setServiceType ( "the JMS");
        client.doTask ();
        
    }
    
}

 

 

Guess you like

Origin blog.csdn.net/walkerwqp/article/details/91865977