-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ObserverActivity.java
102 lines (87 loc) · 4.01 KB
/
ObserverActivity.java
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
package com.example.jingbin.designpattern.observer;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.example.jingbin.designpattern.R;
import com.example.jingbin.designpattern.app.AppConstant;
import com.example.jingbin.designpattern.app.EMTagHandler;
import com.example.jingbin.designpattern.databinding.ActivityObserverBinding;
import com.example.jingbin.designpattern.observer.classs.ObjectFor3D;
import com.example.jingbin.designpattern.observer.classs.ObserverUser1;
import com.example.jingbin.designpattern.observer.classs.ObserverUser2;
import com.example.jingbin.designpattern.observer.javautil.Observer1;
import com.example.jingbin.designpattern.observer.javautil.SubjectFor3d;
import com.example.jingbin.designpattern.observer.javautil.SubjectForSSQ;
/**
* 可以看出,使用Java内置的类实现观察者模式,代码非常简洁,
* 对了addObserver,removeObserver,notifyObservers都已经为我们实现了,
* 所有可以看出Observable(主题)是一个类,而不是一个接口,
* 基本上书上都对于Java的如此设计抱有反面的态度,觉得Java内置的观察者模式,
* 违法了面向接口编程这个原则,但是如果转念想一想,
* 的确你拿一个主题在这写观察者模式(我们自己的实现),
* 接口的思想很好,但是如果现在继续添加很多个主题,
* 每个主题的ddObserver,removeObserver,notifyObservers代码基本都是相同的吧,
* 接口是无法实现代码复用的,而且也没有办法使用组合的模式实现这三个方法的复用,
* 所以我觉得这里把这三个方法在类中实现是合理的。
*/
public class ObserverActivity extends AppCompatActivity implements View.OnClickListener {
private ObjectFor3D objectFor3D;
private ObserverUser1 observerUser1;
private ObserverUser2 observerUser2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityObserverBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_observer);
setTitle("观察者模式");
binding.tvDefine.setText(EMTagHandler.fromHtml(AppConstant.OBSERVER_DEFINE));
binding.btMyself.setOnClickListener(this);
binding.btSystem.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_myself:// 自己实现的3D服务号
// 创建服务号
objectFor3D = new ObjectFor3D();
// 创建两个订阅者
observerUser1 = new ObserverUser1(objectFor3D);
observerUser2 = new ObserverUser2(objectFor3D);
// 两个观察者,发送两条信息
objectFor3D.setMsg("201610121 的3D号为:127");
objectFor3D.setMsg("20161022 的3D号为:000");
break;
case R.id.bt_system://使用Java内置的类实现观察者模式
loadSystemObserver();
break;
default:
break;
}
}
/**
* 系统的观察者接口
*/
private void loadSystemObserver() {
// 创建2个服务号
SubjectFor3d subjectFor3d = new SubjectFor3d();//3d
SubjectForSSQ subjectForSSQ = new SubjectForSSQ();//双色球
// 创建订阅者
Observer1 observer1 = new Observer1();
observer1.registerSubject(subjectFor3d);
observer1.registerSubject(subjectForSSQ);
// 发送信息
subjectFor3d.setMsg("hello 3d'nums : 110 ");
subjectForSSQ.setMsg("ssq'nums : 12,13,31,5,4,3 15");
}
@Override
protected void onStop() {
super.onStop();
if (observerUser1 != null) {
objectFor3D.removeObserver(observerUser1);
}
if (observerUser2 != null) {
objectFor3D.removeObserver(observerUser2);
}
objectFor3D = null;
}
}