The Business Representative pattern (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 the presentation layer code. We have the following entities in the business layer.
客户端(Client) The presentation 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.
业务服务(Business Service) -Business service interface. The entity class of the business service is implemented and the actual business implementation logic is provided.
6.29.1. Realize ¶
We will create Client 、 BusinessDelegate 、 BusinessService 、 LookUpService 、 JMSService And EJBService To represent the various entities in the business representative model.
BusinessDelegatePatternDemo Class usage BusinessDelegate And Client To demonstrate the use of the business representative model.
6.29.2. Step 1 ¶
Create a BusinessService interface.BusinessService.java ¶
publicinterfaceBusinessService{publicvoiddoProcessing();}
6.29.3. Step 2 ¶
Create an entity service class.EJBService.java ¶
publicclassEJBServiceimplementsBusinessService{@OverridepublicvoiddoProcessing(){System.out.println("Processing
task by invoking EJB Service");}}
JMSService.java ¶
publicclassJMSServiceimplementsBusinessService{@OverridepublicvoiddoProcessing(){System.out.println("Processing
task by invoking JMS Service");}}
6.29.4. Step 3 ¶
Create a business query service.BusinessLookUp.java ¶
publicclassBusinessLookUp{publicBusinessServicegetBusinessService(StringserviceType){if(serviceType.equalsIgnoreCase("EJB")){returnnewEJBService();}else{returnnewJMSService();}}}
6.29.5. Step 4 ¶
Create a business representative.BusinessDelegate.java ¶
publicclassBusinessDelegate{privateBusinessLookUplookupService=newBusinessLookUp();privateBusinessServicebusinessService;privateStringserviceType;publicvoidsetServiceType(StringserviceType){this.serviceType=serviceType;}publicvoiddoTask(){businessService=lookupService.getBusinessService(serviceType);businessService.doProcessing();}}
6.29.6. Step 5 ¶
Create a client.Client.java ¶
publicclassClient{BusinessDelegatebusinessService;publicClient(BusinessDelegatebusinessService){this.businessService=businessService;}publicvoiddoTask(){businessService.doTask();}}
6.29.7. Step 6 ¶
Use the BusinessDelegate and Client classes to demonstrate the business representative pattern.BusinessDelegatePatternDemo.java ¶
publicclassBusinessDelegatePatternDemo{publicstaticvoidmain(String[]args){BusinessDelegatebusinessDelegate=newBusinessDelegate();businessDelegate.setServiceType("EJB");Clientclient=newClient(businessDelegate);client.doTask();businessDelegate.setServiceType("JMS");client.doTask();}}
6.29.8. Step 7 ¶
Execute the program and output the result:
Processing task by invoking EJB Service
Processing task by invoking JMS Service