Skip to content

Commit 582fd3e

Browse files
committed
added business delegate
1 parent 73baa11 commit 582fd3e

File tree

9 files changed

+96
-3
lines changed

9 files changed

+96
-3
lines changed

README_cn.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
the License.
5959
```
6060

61-
[runoob.com]:<https://www.tutorialspoint.com/design_pattern/index.htm>
62-
[]:<http://www.runoob.com/design-pattern/design-pattern-tutorial.html>
61+
[tutorialspoint]:<https://www.tutorialspoint.com/design_pattern/index.htm>
62+
[runoob.com]:<http://www.runoob.com/design-pattern/design-pattern-tutorial.html>
6363
[English]:<https://github.com/Catherine22/DesignPattern/blob/master/README.md>
6464
[LazyInitializingSingleton]:<https://github.com/Catherine22/DesignPattern/blob/master/src/com/catherine/singleton/LazyInitializingSingleton.java>
6565
[SafeLazyInitializingSingleton]:<https://github.com/Catherine22/DesignPattern/blob/master/src/com/catherine/singleton/SafeLazyInitializingSingleton.java>

src/com/catherine/Main.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import com.catherine.builder.OldStyleRobotBuilder;
1414
import com.catherine.builder.Robot;
1515
import com.catherine.builder.RobotDirector;
16+
import com.catherine.business_delegate.BusinessDelegate;
17+
import com.catherine.business_delegate.Client;
18+
import com.catherine.business_delegate.ServiceType;
1619
import com.catherine.chain_of_responsibility.DebugLogger;
1720
import com.catherine.chain_of_responsibility.ErrorLogger;
1821
import com.catherine.chain_of_responsibility.Logger;
@@ -107,10 +110,21 @@ public static void main(String[] args) {
107110
// testStrategy();
108111
// testTemplate();
109112
// testVisitor();
110-
testMVC();
113+
// testMVC();
114+
testBusinessDelegate();
111115

112116
}
113117

118+
private static void testBusinessDelegate() {
119+
BusinessDelegate bd = new BusinessDelegate(ServiceType.EJB);
120+
Client client = new Client(bd);
121+
client.createTask();
122+
123+
bd = new BusinessDelegate(ServiceType.JMS);
124+
client = new Client(bd);
125+
client.createTask();
126+
}
127+
114128
private static void testMVC() {
115129
RetrieveCouponFromDB rc = new RetrieveCouponFromDB();
116130
rc.downloadCoupons();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.catherine.business_delegate;
2+
3+
import com.catherine.business_delegate.ServiceType;
4+
5+
/**
6+
* 作为入口,用来访问BusinessService
7+
*
8+
* @author Catherine
9+
*
10+
*/
11+
public class BusinessDelegate {
12+
private ServiceType type;
13+
14+
public BusinessDelegate(ServiceType type) {
15+
this.type = type;
16+
}
17+
18+
public void createTask() {
19+
BusinessLookUp blu = new BusinessLookUp();
20+
BusinessService service = blu.getBusinessService(type);
21+
service.bindService();
22+
}
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.catherine.business_delegate;
2+
3+
public class BusinessLookUp {
4+
5+
public BusinessService getBusinessService(ServiceType type) {
6+
if (type == ServiceType.EJB)
7+
return new EJBService();
8+
else
9+
return new JMSService();
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.catherine.business_delegate;
2+
3+
public interface BusinessService {
4+
public void bindService();
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.catherine.business_delegate;
2+
3+
public class Client {
4+
BusinessDelegate bd;
5+
6+
public Client(BusinessDelegate bd) {
7+
this.bd = bd;
8+
}
9+
10+
public void createTask() {
11+
bd.createTask();
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.catherine.business_delegate;
2+
3+
public class EJBService implements BusinessService {
4+
5+
@Override
6+
public void bindService() {
7+
System.out.println("bind EJBService");
8+
9+
}
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.catherine.business_delegate;
2+
3+
public class JMSService implements BusinessService {
4+
5+
@Override
6+
public void bindService() {
7+
System.out.println("bind JMSService");
8+
9+
}
10+
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.catherine.business_delegate;
2+
3+
public enum ServiceType {
4+
EJB, JMS
5+
}

0 commit comments

Comments
 (0)