Design mode - business representative mode

Keywords: JSP Java

Above (design mode visitor mode): https://blog.csdn.net/qq_16498553/article/details/106934637

catalog

background

What is the business representative model?

Role:

advantage:

Disadvantages:

What can the business representative model do?

Business representative model class diagram

Source code download: https://gitee.com/hong99/design-model/issues/I1IMES

Implementation code

result

Source code download: https://gitee.com/hong99/design-model/issues/I1IMES

last

background

Generally, there is a person in charge in each department of the enterprise. If there is anything that needs to be directly taken over by the person in charge, the person in charge can play an external exposure role and find the person in charge directly.

What is the business representative model?

Business Delegate Pattern is used to decouple presentation layer and business layer. It is basically used to reduce the communication or remote query function of business layer code in the representation layer code. In the business layer we have the following entities.

Role:

client: the presentation layer can be html, jsp, servlet or java UI 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 the business object 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.

advantage:

Low coupling and high flexibility: reduce the interdependence between systems;

High cohesion: if there is a problem, it's not known from the outside. It's only the interface that's to blame, so it's good to deal with these problems from the inside.

Disadvantages:

It does not conform to the opening and closing principle, and the addition, deletion and modification may affect the original function and process;

What can the business representative model do?

One of the main solutions of the business representative mode is to directly transfer the business to the business representative for calling. Of course, all the internal interfaces are exposed to the business representative. Through the unified operation of the business representative, it plays a role that the user will not directly face the internal system but face it.

Personal understanding:

This mode is similar to the door-to-door mode, with one person in charge facing the customer and then operating.

Business representative model class diagram

Source code download: https://gitee.com/hong99/design-model/issues/I1IMES

Implementation code

/**
 * @Auther: csh
 * @Date: 2020/6/23 17:08
 * @Description: Business Delegate
 */
public class BusinessDelegate {
    private BusinessLookUp lookUp = new BusinessLookUp();
    private BusinessService businessService;
    private String serviceType;

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

    public void doTask(){
        businessService = lookUp.getBusinessService(serviceType);
        businessService.doProcessing();
    }
}
/**
 *
 * Function Description: abstract business interface
 *
 * @param:
 * @return:
 * @auther: csh
 * @date: 2020/6/23 18:30
 */
public interface BusinessService {
   public void doProcessing();
}
/**
 *
 * Function Description: lookup service
 *
 * @param:
 * @return:
 * @auther: csh
 * @date: 2020/6/23 18:30
 */
public class BusinessLookUp {
   public BusinessService getBusinessService(String serviceType){
      if(serviceType.equalsIgnoreCase("EJB")){
         return new EJBService();
      }else {
         return new JMSService();
      }
   }
}

 

/**
 *
 * Function Description: Business Service
 *
 * @param:
 * @return:
 * @auther: csh
 * @date: 2020/6/23 18:30
 */
public class EJBService implements BusinessService {
 
   @Override
   public void doProcessing() {
      System.out.println("Processing task by invoking EJB Service");
   }
}

 

/**
 *
 * Function Description: Business Service
 *
 * @param:
 * @return:
 * @auther: csh
 * @date: 2020/6/23 18:30
 */
public class JMSService implements BusinessService {
 
   @Override
   public void doProcessing() {
      System.out.println("Processing task by invoking JMS Service");
   }
}

 

/**
 * @Auther: csh
 * @Date: 2020/6/23 17:09
 * @Description: Customer service (client)
 */
public class Client {

    BusinessDelegate businessDelegate;

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

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

 

/**
 * @Auther: csh
 * @Date: 2020/6/23 17:10
 * @Description:Call business by specific business representative
 */
public class Test {
    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();
    }
}

result

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

Source code download: https://gitee.com/hong99/design-model/issues/I1IMES

last

The business representative mode is very similar to the facade mode. It is also composed of a person in charge in front of the user, while the back-end interface is only exposed to the person in charge. Of course, once the same problem center node has a problem, the whole system will be GG, although it has a high cohesion function.

Posted by rifts on Wed, 24 Jun 2020 19:37:48 -0700