Sunday, 8 September 2013

Business Delegate Design Pattern - Implementation

🚀 Write Clean, Robust Java Code!

Subscribe to Ram N Java for clean architectural patterns, Java EE best practices, and code walkthroughs!

🔔 Subscribe to Ram N Java

Practical Implementation of the Business Delegate Pattern

The Business Delegate Design Pattern plays a vital role in enterprise Java development. It serves as an abstraction layer between the presentation layer (UI, Servlets, or Controllers) and the underlying business service implementations. By isolating lookup logic and network communication, the delegate ensures your front-end code remains simple, maintainable, and completely decoupled from backend service shifts.

Core Components in Implementation

  • BusinessService (Interface): The common interface specifying business capabilities (e.g., doProcessing()).
  • Concrete Business Services: Distinct implementations fulfilling business operations (such as EJBService, JMSService, or modern REST clients).
  • Business Lookup / Service Locator: The utility responsible for obtaining the appropriate business service instance dynamically based on service type.
  • BusinessDelegate: The single contact point for client code. It interfaces with the lookup service, invokes target business operations, and handles exceptions cleanly.
  • Client: The presentation tier component that requests business tasks strictly through the BusinessDelegate.

Step-by-Step Execution Walkthrough

  1. Client Invocation: The client instantiates or injects the BusinessDelegate and sets the desired service type.
  2. Service Resolution: The delegate asks the lookup service to return the corresponding BusinessService instance.
  3. Execution Delegation: The delegate invokes the business operation on the resolved service and forwards any necessary data.
  4. Exception Translation: If network or low-level lookup failures occur, the delegate catches them and throws application-level exceptions back to the presentation tier.

Implementation Best Practices

  • Cache Service References: Always cache resolved remote references inside the delegate or lookup service to reduce expensive network round-trips.
  • Protect Presentation Code: Never propagate low-level system exceptions (e.g., JNDI, RMI, or network exceptions) directly to the client layer.
  • Use Interfaces Consistently: Ensure concrete services implement a well-defined business interface to allow seamless dependency injection and easier unit testing.

Recommended Next Videos

Broaden your enterprise design pattern expertise with these related tutorials from our channel:


Click here to watch in Youtube : https://www.youtube.com/watch?v=qBGmTIRAlBM

Click the below Image to Enlarge
Business Delegate Design Pattern - Implementation - Class Diagram


















BusinessService.java

public interface BusinessService
{
public void doProcessing();
}

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");

}

}

BusinessLookUp.java

public class BusinessLookUp
{
public BusinessService getBusinessService( String serviceType )
{
if( serviceType.equalsIgnoreCase("EJB") )
{
return new EJBService();
}
else if( serviceType.equalsIgnoreCase("JMS") )
{
return new JMSService();
}
return null;
}
}

BusinessDelegate.java

public class BusinessDelegate
{
private BusinessLookUp  lookupService = new BusinessLookUp();
private BusinessService businessService;

public void doTask( String serviceType )
{
businessService = lookupService.getBusinessService(serviceType);

System.out.println(businessService.toString() + " : Got business service object after do the look up");

businessService.doProcessing();
}
}


Client.java

public class Client
{

public static void main( String[] args )
{
BusinessDelegate businessDelegate = new BusinessDelegate();
System.out.println("Invoke the business delegate by passing service type as EJB");
businessDelegate.doTask("EJB");
System.out.println("");
System.out.println("Invoke the business delegate by passing service type as JMS");

businessDelegate.doTask("JMS");

}

}

Output

Invoke the Service Locator by passing service type as EJB
EJBService@42719c : Got business service object after do the look up
Processing task by invoking EJB Service

Invoke the Service Locator by passing service type as JMS
JMSService@119298d : Got business serve object after do the look up
Processing task by invoking JMS Service

2 comments:

  1. Nice blog, Thank you for sharing this helpful content with us. If you are looking free guest posting site for backlinks, Here is an updated list of the best guest blog SEO opportunities. Even newer SEO sites allow paid guest posting, with most of these sites offering free guest writing opportunities.
    For more info visit us:- Guest posting Site List

    ReplyDelete
  2. Thank you for this clear and concise explanation of the Business Delegate design pattern! Your breakdown of its structure and benefits, especially in decoupling presentation-tier clients from business services, makes the concept easy to grasp. It’s a helpful reminder of how essential design patterns are for creating scalable and maintainable applications. Do you have any examples or scenarios where implementing the Business Delegate pattern significantly improved a project’s architecture? Thanks again for sharing your expertise

    Epicforce Tech

    ReplyDelete

Tutorials