YouTip LogoYouTip

Business Delegate Pattern

# Business Delegate Pattern ## Business Delegate Pattern The Business Delegate Pattern is used to decouple the presentation layer from the business layer. It is primarily used to reduce communication or remote lookup functionality for business layer code in the presentation layer code. In the business layer, we have the following entities. * **Client** - The 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. * **LookUp Service** - The lookup service object is responsible for obtaining the relevant business implementation and providing the business object with access to the business delegate object. * **Business Service** - The business service interface. The entity class that implements this business service provides the actual business implementation logic. * * * ## Summary ### Intent Abstract and encapsulate the access logic of an application, thereby providing the presentation layer with access to the business logic layer. ### Main Problem Solved * Solves 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 Cases * When a clear separation between the presentation layer and the business logic layer is needed in a multi-tier application. ### Implementation Approach * **Business Delegate Interface**: Defines methods for accessing business logic. * **Business Delegate Implementation**: Implements the business delegate interface, encapsulating the logic for calling the business logic layer. * **Business Service**: The interface or class of the business logic layer, containing business operations. ### Key Code * **Business Delegate Interface**: Declares methods for accessing business logic. * **Business Delegate Implementation**: Implements the interface, containing code to call the business service. * **Business Service**: The concrete implementation of the business logic. ### Application Example * **Web Application**: The web layer acts as the presentation layer, accessing the backend service layer through the business delegate. ### Advantages * **Decouples Presentation and Business Logic Layers**: The business delegate acts as an intermediate layer, reducing coupling. * **Centralizes Access Logic**: Simplifies 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 abstraction layers. ### Usage Suggestions * Consider using the Business Delegate Pattern when developing multi-tier applications that require a clear separation between the presentation layer and the business logic layer. ### Notes * The business delegate should be as simple as possible and avoid containing complex logic. ### Main Roles Involved * **Business Delegate Interface**: * Declares methods for accessing business logic. * **Business Delegate Implementation**: * Implements the business delegate interface, encapsulating calls to the business service. * **Business Service**: * The interface or class of the business logic layer, containing the actual business operations. * **Presentation Layer**: * The front end of the application, accessing business logic through the business delegate. * **Business Logic Layer**: * Contains the core business logic of the application. The Business Delegate Pattern helps maintain the flexibility and maintainability of an application by providing an abstraction layer to access business logic. * * * ## Implementation We will create _Client_, _BusinessDelegate_, _BusinessService_, _LookUpService_, _JMSService_, and _EJBService_ to represent the various entities in the Business Delegate Pattern. The _BusinessDelegatePatternDemo_ class uses _BusinessDelegate_ and _Client_ to demonstrate the usage of the Business Delegate Pattern. ![Image 1: UML diagram of the Business Delegate Pattern](#) ## Step 1 Create the BusinessService interface. ## BusinessService.java public interface BusinessService{public void doProcessing(); } ## Step 2 Create the concrete service classes. ## 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 the business lookup 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 the output will be: Processing task by invoking EJB ServiceProcessing task by invoking JMS Service
← Composite Entity PatternMvc Pattern β†’