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