|
9 | 9 | ---
|
10 | 10 |
|
11 | 11 | ## Intent
|
| 12 | + |
12 | 13 | The Business Delegate pattern adds an abstraction layer between
|
13 | 14 | presentation and business tiers. By using the pattern we gain loose coupling
|
14 | 15 | between the tiers and encapsulate knowledge about how to locate, connect to,
|
15 | 16 | and interact with the business objects that make up the application.
|
16 | 17 |
|
| 18 | +## Explanation |
| 19 | + |
| 20 | +Real world example |
| 21 | + |
| 22 | +> A mobile phone application promises to stream any movie in existence to your phone. It captures |
| 23 | +> the user's search string and passes this on to the business delegate. The business delegate |
| 24 | +> selects the most suitable video streaming service and plays the video from there. |
| 25 | +
|
| 26 | +In Plain Words |
| 27 | + |
| 28 | +> Business delegate adds an abstraction layer between the presentation and business tiers. |
| 29 | +
|
| 30 | +Wikipedia says |
| 31 | + |
| 32 | +> Business delegate is a Java EE design pattern. This pattern is directing to reduce the coupling |
| 33 | +> in between business services and the connected presentation tier, and to hide the implementation |
| 34 | +> details of services (including lookup and accessibility of EJB architecture). Business delegates |
| 35 | +> acts as an adaptor to invoke business objects from the presentation tier. |
| 36 | +
|
| 37 | +**Programmatic Example** |
| 38 | + |
| 39 | +First, we have an abstraction for video streaming services and a couple of implementations. |
| 40 | + |
| 41 | +```java |
| 42 | +public interface VideoStreamingService { |
| 43 | + void doProcessing(); |
| 44 | +} |
| 45 | + |
| 46 | +@Slf4j |
| 47 | +public class NetflixService implements VideoStreamingService { |
| 48 | + @Override |
| 49 | + public void doProcessing() { |
| 50 | + LOGGER.info("NetflixService is now processing"); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +@Slf4j |
| 55 | +public class YouTubeService implements VideoStreamingService { |
| 56 | + @Override |
| 57 | + public void doProcessing() { |
| 58 | + LOGGER.info("YouTubeService is now processing"); |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Then we have a lookup service that decides which video streaming service is used. |
| 64 | + |
| 65 | +```java |
| 66 | +@Setter |
| 67 | +public class BusinessLookup { |
| 68 | + |
| 69 | + private NetflixService netflixService; |
| 70 | + private YouTubeService youTubeService; |
| 71 | + |
| 72 | + public VideoStreamingService getBusinessService(String movie) { |
| 73 | + if (movie.toLowerCase(Locale.ROOT).contains("die hard")) { |
| 74 | + return netflixService; |
| 75 | + } else { |
| 76 | + return youTubeService; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +The business delegate uses a business lookup to route movie playback requests to a suitable |
| 83 | +video streaming service. |
| 84 | + |
| 85 | +```java |
| 86 | +@Setter |
| 87 | +public class BusinessDelegate { |
| 88 | + |
| 89 | + private BusinessLookup lookupService; |
| 90 | + |
| 91 | + public void playbackMovie(String movie) { |
| 92 | + VideoStreamingService videoStreamingService = lookupService.getBusinessService(movie); |
| 93 | + videoStreamingService.doProcessing(); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +The mobile client utilizes business delegate to call the business tier. |
| 99 | + |
| 100 | +```java |
| 101 | +public class MobileClient { |
| 102 | + |
| 103 | + private final BusinessDelegate businessDelegate; |
| 104 | + |
| 105 | + public MobileClient(BusinessDelegate businessDelegate) { |
| 106 | + this.businessDelegate = businessDelegate; |
| 107 | + } |
| 108 | + |
| 109 | + public void playbackMovie(String movie) { |
| 110 | + businessDelegate.playbackMovie(movie); |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +Finally, we can show the full example in action. |
| 116 | + |
| 117 | +```java |
| 118 | + public static void main(String[] args) { |
| 119 | + |
| 120 | + // prepare the objects |
| 121 | + var businessDelegate = new BusinessDelegate(); |
| 122 | + var businessLookup = new BusinessLookup(); |
| 123 | + businessLookup.setNetflixService(new NetflixService()); |
| 124 | + businessLookup.setYouTubeService(new YouTubeService()); |
| 125 | + businessDelegate.setLookupService(businessLookup); |
| 126 | + |
| 127 | + // create the client and use the business delegate |
| 128 | + var client = new MobileClient(businessDelegate); |
| 129 | + client.playbackMovie("Die Hard 2"); |
| 130 | + client.playbackMovie("Maradona: The Greatest Ever"); |
| 131 | + } |
| 132 | +``` |
| 133 | + |
| 134 | +Here is the console output. |
| 135 | + |
| 136 | +``` |
| 137 | +21:15:33.790 [main] INFO com.iluwatar.business.delegate.NetflixService - NetflixService is now processing |
| 138 | +21:15:33.794 [main] INFO com.iluwatar.business.delegate.YouTubeService - YouTubeService is now processing |
| 139 | +``` |
| 140 | + |
17 | 141 | ## Class diagram
|
18 |
| - |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +## Related patterns |
| 146 | + |
| 147 | +* [Service locator pattern](https://java-design-patterns.com/patterns/service-locator/) |
19 | 148 |
|
20 | 149 | ## Applicability
|
| 150 | + |
21 | 151 | Use the Business Delegate pattern when
|
22 | 152 |
|
23 |
| -* you want loose coupling between presentation and business tiers |
24 |
| -* you want to orchestrate calls to multiple business services |
25 |
| -* you want to encapsulate service lookups and service calls |
| 153 | +* You want loose coupling between presentation and business tiers |
| 154 | +* You want to orchestrate calls to multiple business services |
| 155 | +* You want to encapsulate service lookups and service calls |
| 156 | + |
| 157 | +## Tutorials |
| 158 | + |
| 159 | +* [Business Delegate Pattern at TutorialsPoint](https://www.tutorialspoint.com/design_pattern/business_delegate_pattern.htm) |
26 | 160 |
|
27 | 161 | ## Credits
|
28 | 162 |
|
29 | 163 | * [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)
|
| 164 | +* [Core J2EE Patterns: Best Practices and Design Strategies](https://www.amazon.com/gp/product/0130648841/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0130648841&linkId=a0100de2b28c71ede8db1757fb2b5947) |
0 commit comments