- Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathJ2eeServiceLocatorPatternDemo.java
More file actions
Latest commit
127 lines (107 loc) · 3.83 KB
/
Copy pathJ2eeServiceLocatorPatternDemo.java
File metadata and controls
127 lines (107 loc) · 3.83 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。
服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。
服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
缓存(Cache) - 缓存存储服务的引用,以便复用它们。
客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。*/
importjava.util.ArrayList;
importjava.util.List;
publicinterfaceService {
publicStringgetName();
publicvoidexecute();
}
classService1implementsService {
publicvoidexecute(){
System.out.println("Executing Service1");
}
@Override
publicStringgetName() {
return"Service1";
}
}
classService2implementsService {
publicvoidexecute(){
System.out.println("Executing Service2");
}
@Override
publicStringgetName() {
return"Service2";
}
}
classInitialContext {
publicObjectlookup(StringjndiName){
if(jndiName.equalsIgnoreCase("SERVICE1")){
System.out.println("Looking up and creating a new Service1 object");
returnnewService1();
}elseif (jndiName.equalsIgnoreCase("SERVICE2")){
System.out.println("Looking up and creating a new Service2 object");
returnnewService2();
}
returnnull;
}
}
classCache {
privateList<Service> services;
publicCache(){
services = newArrayList<Service>();
}
publicServicegetService(StringserviceName){
for (Serviceservice : services) {
if(service.getName().equalsIgnoreCase(serviceName)){
System.out.println("Returning cached "+serviceName+" object");
returnservice;
}
}
returnnull;
}
publicvoidaddService(ServicenewService){
booleanexists = false;
for (Serviceservice : services) {
if(service.getName().equalsIgnoreCase(newService.getName())){
exists = true;
}
}
if(!exists){
services.add(newService);
}
}
}
classServiceLocator {
privatestaticCachecache;
static {
cache = newCache();
}
publicstaticServicegetService(StringjndiName){
Serviceservice = cache.getService(jndiName);
if(service != null){
returnservice;
}
InitialContextcontext = newInitialContext();
Serviceservice1 = (Service)context.lookup(jndiName);
cache.addService(service1);
returnservice1;
}
}
publicclassJ2eeServiceLocatorPatternDemo {
publicstaticvoidmain(String[] args) {
Serviceservice = ServiceLocator.getService("Service1");
service.execute();
service = ServiceLocator.getService("Service2");
service.execute();
service = ServiceLocator.getService("Service1");
service.execute();
service = ServiceLocator.getService("Service2");
service.execute();
}
}
//j2ee 服务定位器模式
/*
Looking up and creating a new Service1 object
Executing Service1
Looking up and creating a new Service2 object
Executing Service2
Returning cached Service1 object
Executing Service1
Returning cached Service2 object
Executing Service2
*/