Skip to content

Commit 7ac468d

Browse files
iluwatarohbus
andauthored
docs: iluwatar#590 refactor and add explanation for business delegate (iluwatar#1686)
Type: docs and refactoring Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
1 parent 794795a commit 7ac468d

14 files changed

+217
-280
lines changed

business-delegate/README.md

Lines changed: 139 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,156 @@ tags:
99
---
1010

1111
## Intent
12+
1213
The Business Delegate pattern adds an abstraction layer between
1314
presentation and business tiers. By using the pattern we gain loose coupling
1415
between the tiers and encapsulate knowledge about how to locate, connect to,
1516
and interact with the business objects that make up the application.
1617

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+
17141
## Class diagram
18-
![alt text](./etc/business-delegate.png "Business Delegate")
142+
143+
![alt text](./etc/business-delegate.urm.png "Business Delegate")
144+
145+
## Related patterns
146+
147+
* [Service locator pattern](https://java-design-patterns.com/patterns/service-locator/)
19148

20149
## Applicability
150+
21151
Use the Business Delegate pattern when
22152

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)
26160

27161
## Credits
28162

29163
* [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)
-23.7 KB
Binary file not shown.

business-delegate/etc/business-delegate.ucls

Lines changed: 0 additions & 136 deletions
This file was deleted.
48.9 KB
Loading

business-delegate/etc/business-delegate.urm.puml

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,42 @@ package com.iluwatar.business.delegate {
55
+ main(args : String[]) {static}
66
}
77
class BusinessDelegate {
8-
- businessService : BusinessService
98
- lookupService : BusinessLookup
10-
- serviceType : ServiceType
119
+ BusinessDelegate()
12-
+ doTask()
13-
+ setLookupService(businessLookup : BusinessLookup)
14-
+ setServiceType(serviceType : ServiceType)
10+
+ playbackMovie(movie : String)
11+
+ setLookupService(lookupService : BusinessLookup)
1512
}
1613
class BusinessLookup {
17-
- ejbService : EjbService
18-
- jmsService : JmsService
14+
- netflixService : NetflixService
15+
- youTubeService : YouTubeService
1916
+ BusinessLookup()
20-
+ getBusinessService(serviceType : ServiceType) : BusinessService
21-
+ setEjbService(ejbService : EjbService)
22-
+ setJmsService(jmsService : JmsService)
17+
+ getBusinessService(movie : String) : VideoStreamingService
18+
+ setNetflixService(netflixService : NetflixService)
19+
+ setYouTubeService(youTubeService : YouTubeService)
2320
}
24-
interface BusinessService {
25-
+ doProcessing() {abstract}
26-
}
27-
class Client {
21+
class MobileClient {
2822
- businessDelegate : BusinessDelegate
29-
+ Client(businessDelegate : BusinessDelegate)
30-
+ doTask()
23+
+ MobileClient(businessDelegate : BusinessDelegate)
24+
+ playbackMovie(movie : String)
3125
}
32-
class EjbService {
26+
class NetflixService {
3327
- LOGGER : Logger {static}
34-
+ EjbService()
28+
+ NetflixService()
3529
+ doProcessing()
3630
}
37-
class JmsService {
31+
interface VideoStreamingService {
32+
+ doProcessing() {abstract}
33+
}
34+
class YouTubeService {
3835
- LOGGER : Logger {static}
39-
+ JmsService()
36+
+ YouTubeService()
4037
+ doProcessing()
4138
}
42-
enum ServiceType {
43-
+ EJB {static}
44-
+ JMS {static}
45-
+ valueOf(name : String) : ServiceType {static}
46-
+ values() : ServiceType[] {static}
47-
}
4839
}
49-
BusinessLookup --> "-ejbService" EjbService
50-
BusinessDelegate --> "-serviceType" ServiceType
51-
Client --> "-businessDelegate" BusinessDelegate
52-
BusinessDelegate --> "-businessService" BusinessService
40+
BusinessLookup --> "-netflixService" NetflixService
41+
BusinessLookup --> "-youTubeService" YouTubeService
42+
MobileClient --> "-businessDelegate" BusinessDelegate
5343
BusinessDelegate --> "-lookupService" BusinessLookup
54-
BusinessLookup --> "-jmsService" JmsService
55-
EjbService ..|> BusinessService
56-
JmsService ..|> BusinessService
44+
NetflixService ..|> VideoStreamingService
45+
YouTubeService ..|> VideoStreamingService
5746
@enduml

0 commit comments

Comments
 (0)