Skip to content

Commit 53a294f

Browse files
Al-assadohbus
andauthored
translated the business delegate pattern into Chinese (iluwatar#1728)
Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
1 parent a506290 commit 53a294f

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

zh/business-delegate/README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
layout: pattern
3+
title: Business Delegate
4+
folder: business-delegate
5+
permalink: /patterns/business-delegate/
6+
categories: Structural
7+
tags:
8+
- Decoupling
9+
---
10+
11+
## 作用
12+
13+
业务委托模式(译者:国内也有翻译成业务代表模式)在表现层和业务层之间增加了一个抽象层。通过使用该模式,我们获得了各层之间的松散耦合,并封装了关于如何定位、连接和与构成应用程序的业务对象进行交互的知识。
14+
15+
## 解释
16+
17+
真实世界的案例
18+
19+
> 一个手机应用程序承诺将现有的任何电影传输到你的手机上。它捕获了用户的搜索关键字内容,并将其传递给业务委托层。业务委托层选择最合适的视频流服务,并从该服务进行视频播放。
20+
21+
简而言之
22+
23+
> 业务委托模式在表现层和业务层之间增加了一个抽象层。
24+
25+
维基百科的解释
26+
27+
> Business delegate is a Java EE design pattern. This pattern is directing to reduce the coupling
28+
> in between business services and the connected presentation tier, and to hide the implementation
29+
> details of services (including lookup and accessibility of EJB architecture). Business delegates
30+
> acts as an adaptor to invoke business objects from the presentation tier.
31+
>
32+
> 业务委托模式是一种 Java EE 设计模式。这种模式旨在减少业务服务和所连接的表现层之间的耦合度,并隐藏服务的实现细节(包括 EJB 架构的查询和可访问性)。业务代表作为一个适配器,从表现层调用业务对象。
33+
34+
**编程示例**
35+
36+
首先,我们实现了一个视频流服务的抽象,和几个具体实现。
37+
38+
```java
39+
public interface VideoStreamingService {
40+
void doProcessing();
41+
}
42+
43+
@Slf4j
44+
public class NetflixService implements VideoStreamingService {
45+
@Override
46+
public void doProcessing() {
47+
LOGGER.info("NetflixService is now processing");
48+
}
49+
}
50+
51+
@Slf4j
52+
public class YouTubeService implements VideoStreamingService {
53+
@Override
54+
public void doProcessing() {
55+
LOGGER.info("YouTubeService is now processing");
56+
}
57+
}
58+
```
59+
60+
接下来,我们实现一个查询服务,用于决定使用哪个视频流服务。
61+
62+
```java
63+
@Setter
64+
public class BusinessLookup {
65+
66+
private NetflixService netflixService;
67+
private YouTubeService youTubeService;
68+
69+
public VideoStreamingService getBusinessService(String movie) {
70+
if (movie.toLowerCase(Locale.ROOT).contains("die hard")) {
71+
return netflixService;
72+
} else {
73+
return youTubeService;
74+
}
75+
}
76+
}
77+
```
78+
79+
业务委托层使用业务查询,将电影播放请求路由到合适的视频流服务。
80+
81+
```java
82+
@Setter
83+
public class BusinessDelegate {
84+
85+
private BusinessLookup lookupService;
86+
87+
public void playbackMovie(String movie) {
88+
VideoStreamingService videoStreamingService = lookupService.getBusinessService(movie);
89+
videoStreamingService.doProcessing();
90+
}
91+
}
92+
```
93+
94+
移动客户端利用业务委托来调用业务层。
95+
96+
```java
97+
public class MobileClient {
98+
99+
private final BusinessDelegate businessDelegate;
100+
101+
public MobileClient(BusinessDelegate businessDelegate) {
102+
this.businessDelegate = businessDelegate;
103+
}
104+
105+
public void playbackMovie(String movie) {
106+
businessDelegate.playbackMovie(movie);
107+
}
108+
}
109+
```
110+
111+
最后,我们展示一下这个示例完整的操作。
112+
113+
```java
114+
public static void main(String[] args) {
115+
116+
// prepare the objects
117+
var businessDelegate = new BusinessDelegate();
118+
var businessLookup = new BusinessLookup();
119+
businessLookup.setNetflixService(new NetflixService());
120+
businessLookup.setYouTubeService(new YouTubeService());
121+
businessDelegate.setLookupService(businessLookup);
122+
123+
// create the client and use the business delegate
124+
var client = new MobileClient(businessDelegate);
125+
client.playbackMovie("Die Hard 2");
126+
client.playbackMovie("Maradona: The Greatest Ever");
127+
}
128+
```
129+
130+
以下是终端输出的内容。
131+
132+
```
133+
21:15:33.790 [main] INFO com.iluwatar.business.delegate.NetflixService - NetflixService is now processing
134+
21:15:33.794 [main] INFO com.iluwatar.business.delegate.YouTubeService - YouTubeService is now processing
135+
```
136+
137+
## 类图
138+
139+
![alt text](../../business-delegate/etc/business-delegate.urm.png "Business Delegate")
140+
141+
## 相关模式
142+
143+
* [Service locator pattern](https://java-design-patterns.com/patterns/service-locator/)
144+
145+
## 适用场景
146+
147+
业务委托模式的适用场景:
148+
149+
* 你希望表现层和业务层之间是松耦合的。
150+
* 你想要协调对多个业务服务的调用。
151+
* 你想要对服务查询、服务调用进行封装。
152+
153+
## 教程
154+
155+
* [Business Delegate Pattern at TutorialsPoint](https://www.tutorialspoint.com/design_pattern/business_delegate_pattern.htm)
156+
157+
## 鸣谢
158+
159+
* [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)
160+
* [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

Comments
 (0)