观察者模式就是:当一个对象的状态发送改变时,所有依赖于它的对象都能得到通知并被自动更新。
下面介绍一种简单的使用方法,(下面有demo链接)先看一下project的目录构成:
ObserverListener是观察者接口,SubjectListener是被观察者接口,ObserverManager是观察者的管理类。
/**
* Created by Administrator on 2016/9/28.
* 观察者接口
*/ public interface ObserverListener {
void observerUpData(String content);//刷新操作
}
/**
* Created by Administrator on 2016/9/28.
* 被观察者接口
*/ public interface SubjectListener {
void add(ObserverListener observerListener);
void notifyObserver(String content);
void remove(ObserverListener observerListener);
}
下面是比较重要的管理类:
public class ObserverManager implements SubjectListener {
private static ObserverManager observerManager;
//观察者接口集合
private List<ObserverListener> list = new ArrayList<>(); /**
* 单例
*/
public static ObserverManager getInstance(){
if (null == observerManager){
synchronized (ObserverManager.class){
if (null == observerManager){
observerManager = new ObserverManager();
}
}
}
return observerManager;
} /**
* 加入监听队列
*/
@Override
public void add(ObserverListener observerListener) {
list.add(observerListener);
} /**
* 通知观察者刷新数据
*/
@Override
public void notifyObserver(String content) {
for (ObserverListener observerListener : list){
observerListener.observerUpData(content);
}
} /**
* 监听队列中移除
*/
@Override
public void remove(ObserverListener observerListener) {
if (list.contains(observerListener)){
list.remove(observerListener);
}
}
}
效果是当点击ThirdActivity中的按钮时,执行ObserverManager.getInstance().notifyObserver("观察者请刷新信息");
然后所有添加到监听队列的Activity都会更新数据,ObserverManager.getInstance().add(this)就是添加到监听队列的方法,更新操作执行在observerUpData(String content)方法中。
下面是3个Activity,很简单:
public class MainActivity extends AppCompatActivity implements ObserverListener {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//ObserverManager.getInstance().notifyObserver("观察者请刷新信息");
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}); //注册
ObserverManager.getInstance().add(this); } @Override
public void observerUpData(String content) {
Log.d("vvvvvvvvvvvvvvv","11111111111");
textView.setText(content);
}
}
public class SecondActivity extends AppCompatActivity implements ObserverListener {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = (TextView) findViewById(R.id.tv2);
findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
}
}); //注册
ObserverManager.getInstance().add(this);
} @Override
public void observerUpData(String content) {
Log.d("vvvvvvvvvvvvvvv","2222222222");
textView.setText(content);
}
}
public class ThirdActivity extends AppCompatActivity implements ObserverListener {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
textView = (TextView) findViewById(R.id.tv3);
findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObserverManager.getInstance().notifyObserver("观察者请刷新信息");
}
}); //注册
ObserverManager.getInstance().add(this);
} @Override
public void observerUpData(String content) {
Log.d("vvvvvvvvvvvvvvv","33333333333");
textView.setText(content);
}
}
布局都是一个TextView和一个Button。
demo链接http://yun.baidu.com/share/link?shareid=2719259515&uk=1969594477