观察者模式的定义
观察者定义了一个一对多的依赖关系,让一个或者多个观察者监听一个主题(被观察者)的变化。因此,当主题状态发生变化时,主题会通知观察者,使观察者能够自动更新。
被观察者的特点
- 持有监听的观察者的引用
- 支持增加和删除观察者
- 主题状态改变,通知观察者
观察者的特点
接收到被观察者状态发生变化时做出变化。
如何实现
- 观察者实现Observer接口即可
- 被观察者继承Observable即可
示例代码
package com.example.mazhihao.observerdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Organization organization = new Organization();
organization.setMsg("Hello Observer...");
Member member = new Member(MainActivity.this);
organization.addObserver(member);//添加一个观察者
organization.display();
Log.i("mzh", "observer count is " + organization.countObservers());
organization.deleteObserver(member);
Log.i("mzh", "observer count is " + organization.countObservers());
}
});
}
}
观察者
package com.example.mazhihao.observerdemo;
import android.content.Context;
import android.widget.Toast;
import java.util.Observable;
import java.util.Observer;
/**
* Created by mazhihao on 2016/7/13.
*/
public class Member implements Observer {
private Context context;
public Member(Context context) {
this.context = context;
}
@Override
public void update(Observable observable, Object data) {
String msg = ((Organization) observable).getMsg();
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
被观察者
package com.example.mazhihao.observerdemo;
import java.util.Observable;
/**
* Created by mazhihao on 2016/7/13.
*/
public class Organization extends Observable {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void display() {
this.setChanged();//改变状态
this.notifyObservers();
}
}
关注要点
主题发生改变时,需要调用setChange()方法,表示状态放生改变。下面看看这个方法的具体实现
/**
* Sets the changed flag for this {@code Observable}. After calling
* {@code setChanged()}, {@code hasChanged()} will return {@code true}.
*/
protected void setChanged() {
changed = true;
}
再来看看notifyObservers()这个方法的具体实现:
/**
* If {@code hasChanged()} returns {@code true}, calls the {@code update()}
* method for every observer in the list of observers using null as the
* argument. Afterwards, calls {@code clearChanged()}.
*
* Equivalent to calling {@code notifyObservers(null)}.
*/
public void notifyObservers() {
notifyObservers(null);
}
/**
* If {@code hasChanged()} returns {@code true}, calls the {@code update()}
* method for every Observer in the list of observers using the specified
* argument. Afterwards calls {@code clearChanged()}.
*
* @param data
* the argument passed to {@code update()}.
*/
@SuppressWarnings("unchecked")
public void notifyObservers(Object data) {
int size = 0;
Observer[] arrays = null;
synchronized (this) {
if (hasChanged()) {
clearChanged();
size = observers.size();
arrays = new Observer[size];
observers.toArray(arrays);
}
}
if (arrays != null) {
for (Observer observer : arrays) {
observer.update(this, data);
}
}
}
总结
当主题状态发生变化时,必须要调用setChange()方法改变标记,然后调用notifyObservers()方法通知所有的观察者才会生效。