Business Delegate Pattern

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

  • ClientThe presentation layer code can be JSP, servlet, or UI Java code.
  • Business Delegate- An entry class provided for client entities, which provides access to business service methods.
  • Query Service (LookUp Service)The lookup service object is responsible for obtaining the relevant business implementation and providing business objects with access to the business delegate object.
  • Business Service- Business service interface. The entity class that implements this business service provides the actual business implementation logic.

summary

intent

Abstract and encapsulate the application's access logic, thereby providing the presentation layer with access to the business logic layer.

Main Problem Solved

  • Solve the coupling problem between the presentation layer and the business logic layer, allowing the presentation layer to indirectly access the business logic layer through the business delegate.

Use Case

  • When there is a need to clearly separate the presentation layer and the business logic layer in a multi-layer application.

Implementation Approach

  • Business Delegate Interface: Define methods for accessing business logic.
  • Business Delegate Implementation: Implement the business delegate interface and encapsulate the logic for calling the business logic layer.
  • Business Service: Interface or class of the business logic layer, containing business operations.

Key code

  • Business Delegate Interface: Declare methods for accessing business logic.
  • Business Delegate Implementation: Implement the interface, containing code that calls the business service.
  • Business Service: Concrete implementation of business logic.

Application example

  • Web Application: The Web layer acts as the presentation layer and accesses the backend service layer through the business delegate.

Advantages

  • Decouple the presentation layer from the business logic layer: The business delegate acts as an intermediate layer to reduce coupling.
  • Centralized Access Logic: Simplify the presentation layer code by centralizing access logic in the business delegate.
  • Easy to Maintain and Extend: When adding new business logic access, only the business delegate needs to be modified.

Disadvantages

  • May Increase Complexity: For simple applications, it may add unnecessary layers of abstraction.

Usage suggestions

  • When developing multi-layer applications and needing to provide a clear separation between the presentation layer and the business logic layer, consider using the Business Delegate Pattern.

Notes

  • The business delegate should be as concise as possible and avoid containing complex logic.

Several main roles involved

  • Business Delegate Interface:

    • Declare methods for accessing business logic.
  • Business Delegate Implementation:

    • Implement the business delegate interface and encapsulate calls to business services.
  • Business Service:

    • The interface or class of the business logic layer, containing actual business operations.
  • Presentation Layer:

    • The front end of the application accesses business logic through the business delegate.
  • Business Logic Layer (Business Logic Layer):

    • Contains the core business logic of the application.

The Business Delegate Pattern provides an abstraction layer to access business logic, helping to maintain the flexibility and maintainability of the application.


Implementation

We will createClient、BusinessDelegate、BusinessService、LookUpService、JMSServiceandEJBServiceto represent the various entities in the Business Delegate Pattern.

BusinessDelegatePatternDemoClass usageBusinessDelegateandClientTo demonstrate the usage of the Business Delegate pattern.

业务代表模式的 UML 图

Step 1

Create the BusinessService interface.

BusinessService.java

public interface BusinessService { public void doProcessing(); }

Step 2

Create an entity service class.

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 query 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 the Business Delegate.

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 the Client.

Client.java

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

Step 6

Use the BusinessDelegate and Client classes to demonstrate the Business Delegate pattern.

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

Execute the program and output the results:

Processing task by invoking EJB Service
Processing task by invoking JMS Service
other extensions