Browser docs

Business Delegate

Intent

The Business Delegate pattern adds an abstraction layer between presentation and business tiers. By using the pattern we gain loose coupling between the tiers and encapsulate knowledge about how to locate, connect to, and interact with the business objects that make up the application.

Explanation

Real world example

A mobile phone application promises to stream any movie in existence to your phone. It captures the user’s search string and passes this on to the business delegate. The business delegate selects the most suitable video streaming service and plays the video from there.

In Plain Words

Business delegate adds an abstraction layer between the presentation and business tiers.

Wikipedia says

Business delegate is a Java EE design pattern. This pattern is directing to reduce the coupling in between business services and the connected presentation tier, and to hide the implementation details of services (including lookup and accessibility of EJB architecture). Business delegates acts as an adaptor to invoke business objects from the presentation tier.

Programmatic Example

First, we have an abstraction for video streaming services and a couple of implementations.

 1public interface VideoStreamingService {
 2  void doProcessing();
 3}
 4
 5@Slf4j
 6public class NetflixService implements VideoStreamingService {
 7  @Override
 8  public void doProcessing() {
 9    LOGGER.info("NetflixService is now processing");
10  }
11}
12
13@Slf4j
14public class YouTubeService implements VideoStreamingService {
15  @Override
16  public void doProcessing() {
17    LOGGER.info("YouTubeService is now processing");
18  }
19}

Then we have a lookup service that decides which video streaming service is used.

 1@Setter
 2public class BusinessLookup {
 3
 4  private NetflixService netflixService;
 5  private YouTubeService youTubeService;
 6
 7  public VideoStreamingService getBusinessService(String movie) {
 8    if (movie.toLowerCase(Locale.ROOT).contains("die hard")) {
 9      return netflixService;
10    } else {
11      return youTubeService;
12    }
13  }
14}

The business delegate uses a business lookup to route movie playback requests to a suitable video streaming service.

 1@Setter
 2public class BusinessDelegate {
 3
 4  private BusinessLookup lookupService;
 5
 6  public void playbackMovie(String movie) {
 7    VideoStreamingService videoStreamingService = lookupService.getBusinessService(movie);
 8    videoStreamingService.doProcessing();
 9  }
10}

The mobile client utilizes business delegate to call the business tier.

 1public class MobileClient {
 2
 3  private final BusinessDelegate businessDelegate;
 4
 5  public MobileClient(BusinessDelegate businessDelegate) {
 6    this.businessDelegate = businessDelegate;
 7  }
 8
 9  public void playbackMovie(String movie) {
10    businessDelegate.playbackMovie(movie);
11  }
12}

Finally, we can show the full example in action.

 1  public static void main(String[] args) {
 2
 3    // prepare the objects
 4    var businessDelegate = new BusinessDelegate();
 5    var businessLookup = new BusinessLookup();
 6    businessLookup.setNetflixService(new NetflixService());
 7    businessLookup.setYouTubeService(new YouTubeService());
 8    businessDelegate.setLookupService(businessLookup);
 9
10    // create the client and use the business delegate
11    var client = new MobileClient(businessDelegate);
12    client.playbackMovie("Die Hard 2");
13    client.playbackMovie("Maradona: The Greatest Ever");
14  }

Here is the console output.

21:15:33.790 [main] INFO com.iluwatar.business.delegate.NetflixService - NetflixService is now processing
21:15:33.794 [main] INFO com.iluwatar.business.delegate.YouTubeService - YouTubeService is now processing

Class diagram

alt text

Applicability

Use the Business Delegate pattern when

  • You want loose coupling between presentation and business tiers
  • You want to orchestrate calls to multiple business services
  • You want to encapsulate service lookups and service calls

Tutorials

Credits