-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathServices.java
More file actions
38 lines (30 loc) · 958 Bytes
/
Services.java
File metadata and controls
38 lines (30 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package effectivejava.chapter2;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
//不可实例化的类,用于服务注册和访问
public class Services {
private Services() {
}
// map service name to Service
private static final Map<String, Provider> providers = new ConcurrentHashMap<String, Provider>();
private static final String DEFAULT_PROVIDER_NAME = "<def>";
// provider registration API
public static void registerDefaultProvider(Provider p) {
registerProvider(DEFAULT_PROVIDER_NAME, p);
}
public static void registerProvider(String name, Provider p) {
providers.put(name, p);
}
// Service access API
public Service newInstance() {
return newInstance(DEFAULT_PROVIDER_NAME);
}
public Service newInstance(String name) {
// 通过服务提供者获得服务
Provider p = providers.get(name);
if (p == null)
throw new IllegalArgumentException("No provider registered with "
+ name);
return p.newService();
}
}