-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserverTest.java
More file actions
265 lines (232 loc) · 7.46 KB
/
Copy pathObserverTest.java
File metadata and controls
265 lines (232 loc) · 7.46 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package Observer;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;
/**
* 一个观察者模式的demo:
* 比如一个公司有很多部门,比如行政部门,技术部门,测试部门,但是这些部门都要受董事会的管辖治理,
* 各个部门要遵照董事会的命令来办事,这里各个部门就相当于观察者,董事会相当于被观察者。
* @author dell
*
*/
public class ObserverTest{
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
//1.新建一个董事会
Directorate ob = new Directorate();
//2.董事会创建各个部门
Observer executive = new Executive();
Observer hr = new HR();
Observer itu_d = new ITU_D();
Observer td = new TD();
ob.addObserver(executive);
ob.addObserver(hr);
ob.addObserver(itu_d);
ob.addObserver(td);
//3.董事会开始运转:发布给各个部门的指令
ob.setIndicationForExecutive("提前开始在"+(new Date().getMonth()+2)+"月份上市的准备工作");
ob.setIndicationForHR("招到100个人");
ob.setIndicationForITUD("完成各个产品的扫尾工作");
ob.setIndicationForTD("开发完成多少,测试多少");
//3.通知各个部门
ob.notifyObservers(new Date().getMonth()+1+"月份的工作");
//4.也可以单独对一个部门发布命令
ob.setIndicationForITUD("这个月开发一个新的人力管理系统");
ob.notifyObservers("");
}
}
/**
* @Component:被观察者->董事会
* 职能:对各个部门发布指示,充当被观察者的角色
* 继承自java.util.Observable类
* @author btp
*
*/
class Directorate extends Observable{
/*
* 给行政部门的指示
*/
private String indicationForExecutive;
/*
* 给人力部门的指示
*/
private String indicationForHR;
/*
* 给开发部门的指示
*/
private String indicationForITUD;
/*
* 给测试部门的指示
*/
private String indicationForTD;
public Directorate() {}
public Directorate(String indicationForExecutive, String indicationForHR, String indicationForITUD,
String indicationForTD) {
super();
this.indicationForExecutive = indicationForExecutive;
this.indicationForHR = indicationForHR;
this.indicationForITUD = indicationForITUD;
this.indicationForTD = indicationForTD;
}
public String getIndicationForExecutive() {
return indicationForExecutive;
}
public void setIndicationForExecutive(String indicationForExecutive) {
this.indicationForExecutive = indicationForExecutive;
}
public String getIndicationForHR() {
return indicationForHR;
}
public void setIndicationForHR(String indicationForHR) {
this.indicationForHR = indicationForHR;
}
public String getIndicationForITUD() {
return indicationForITUD;
}
public void setIndicationForITUD(String indicationForITUD) {
this.indicationForITUD = indicationForITUD;
}
public String getIndicationForTD() {
return indicationForTD;
}
public void setIndicationForTD(String indicationForTD) {
this.indicationForTD = indicationForTD;
}
/*
* 重写父类的通知各个部门的方法
* @see java.lang.Object#toString()
*/
public void notifyObservers(Object arg) {
System.out.println(this.toString());
//表示新的指示已经拟好,可以通知各个部门了
this.setChanged();
super.notifyObservers(arg);
}
@Override
public String toString() {
return new SimpleDateFormat("yyyy年MM月dd日").format(new Date())+"董事会的指令 [行政部门:(" + indicationForExecutive + "), 人力资源部门:(" + indicationForHR
+ "), 开发部门(" + indicationForITUD + "), 测试部门(" + indicationForTD + ")";
}
}
/**
* @Component:观察者 -> 行政部门
* 职能:在董事会的指示下,完成公司行政部分的工作
* 实现了java.util.Observer接口
* @author btp
*/
class Executive implements Observer{
private String nextStepJob;
public String getNextStepJob() {
return nextStepJob;
}
public void setNextStepJob(String nextStepJob) {
this.nextStepJob = nextStepJob;
}
@Override
public void update(Observable o, Object arg) {
Directorate myDirectorate = null;
if(null != o && o instanceof Directorate) {
myDirectorate = (Directorate)o;
//只有命令改变的时候才重新接受新的命令
if(null == nextStepJob||(null != nextStepJob && !nextStepJob.equals(myDirectorate.getIndicationForExecutive()))) {
nextStepJob = myDirectorate.getIndicationForExecutive();
String nextStepJob = "行政部门"+arg+":"+myDirectorate.getIndicationForExecutive();
System.out.println("行政部门收到指令(" + nextStepJob +")");
}
}else {
throw new RuntimeException("行政部门找不到自己的董事会,无法正常工作");
}
}
}
/**
* @Component:观察者 -> 人力资源部门
* 职能:在董事会的指示下,完成公司招聘相关的工作
* 实现了java.util.Observer接口
* @author btp
*/
class HR implements Observer{
private String nextStepJob;
public String getNextStepJob() {
return nextStepJob;
}
public void setNextStepJob(String nextStepJob) {
this.nextStepJob = nextStepJob;
}
@Override
public void update(Observable o, Object arg) {
Directorate myDirectorate = null;
if(null != o && o instanceof Directorate) {
myDirectorate = (Directorate)o;
//只有命令改变的时候才重新接受新的命令
if(null == nextStepJob || (null != nextStepJob && !nextStepJob.equals(myDirectorate.getIndicationForHR()))) {
nextStepJob = myDirectorate.getIndicationForHR();
String nextStepJob = "人力资源部门"+arg+":"+myDirectorate.getIndicationForHR();
System.out.println("人力资源部门收到指令(" + nextStepJob +")");
}
}else {
throw new RuntimeException("人力资源部门找不到自己的董事会,无法正常工作");
}
}
}
/**
* @Component:观察者 -> 开发部门
* 职能:在董事会的指示下,完成公司开发工作
* 实现了java.util.Observer接口
* @author btp
*/
class ITU_D implements Observer{
private String nextStepJob;
public String getNextStepJob() {
return nextStepJob;
}
public void setNextStepJob(String nextStepJob) {
this.nextStepJob = nextStepJob;
}
@Override
public void update(Observable o, Object arg) {
Directorate myDirectorate = null;
if(null != o && o instanceof Directorate) {
myDirectorate = (Directorate)o;
//只有命令改变的时候才重新接受新的命令
if(null == nextStepJob || (null != nextStepJob && !nextStepJob.equals(myDirectorate.getIndicationForITUD()))) {
nextStepJob = myDirectorate.getIndicationForITUD();
String nextStepJob = "开发部门"+arg+":"+myDirectorate.getIndicationForITUD();
System.out.println("开发部门收到指令(" + nextStepJob +")");
}
}else {
throw new RuntimeException("开发部门找不到自己的董事会,无法正常工作");
}
}
}
/**
* @Component:观察者 -> 测试部门
* 职能:在董事会的指示下,完成公司产品的测试工作
* 实现了java.util.Observer接口
* @author btp
*/
class TD implements Observer{
private String nextStepJob;
public String getNextStepJob() {
return nextStepJob;
}
public void setNextStepJob(String nextStepJob) {
this.nextStepJob = nextStepJob;
}
@Override
public void update(Observable o, Object arg) {
Directorate myDirectorate = null;
if(null != o && o instanceof Directorate) {
myDirectorate = (Directorate)o;
//只有命令改变的时候才重新接受新的命令
if(null == nextStepJob ||(null != nextStepJob && !nextStepJob.equals(myDirectorate.getIndicationForTD()))) {
nextStepJob = myDirectorate.getIndicationForTD();
String nextStepJob = "测试部门"+arg+":"+myDirectorate.getIndicationForTD();
System.out.println("测试部门收到指令(" + nextStepJob +")");
}
}else {
throw new RuntimeException("测试部门找不到自己的董事会,无法正常工作");
}
}
}