使用Observer模式进行传递
三个主要类:Observer、Subject、TestObServernotice
/*** 作者:created by meixi* 邮箱:15913707499@163.com* 日期:2019/5/23 15*/public interface Observer {void update(int id, int intparameter, String string, Bitmap bitmap);
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
public interface Subject {/*** 注册一个观察者*/void addObserver(Observer observer);/*** 删除一个观察者** @return 是否删除成功*/boolean removeObserver(Observer observer);/*** 通知所有的观察者** @param id 对应观察者的id*/void notifyObserver(int id,int intparameter,String s,Bitmap bitmap);}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
public class TestObServernotice implements Subject {private static Subject subject;private List<Observer> observers = new ArrayList<>();private TestObServernotice() {// TODO Auto-generated constructor stub}public static Subject getInstance() {if (null == subject) {synchronized (TestObServernotice.class) {if (null == subject)subject = new TestObServernotice();}}return subject;}@Overridepublic void addObserver(Observer observer) {// TODO Auto-generated method stubobservers.add(observer);}@Overridepublic boolean removeObserver(Observer observer) {// TODO Auto-generated method stubreturn observers.remove(observer);}@Overridepublic void notifyObserver(int id, int intparam, String param, Bitmap bitmap) {// TODO Auto-generated method stubfor (Observer observer : observers) {observer.update(id,intparam,param,bitmap);}}
}
开始使用:
观察者中注册Observer,如MainActivity中注册
private Observer deleteObserver = new Observer() {@Overridepublic void update(int id,int intparam,String param) {if (id==11){}}
};
TestObServernotice.getInstance().addObserver(deleteObserver);
同时注意删除观察者
@Override
protected void onDestroy() {super.onDestroy();TestObServernotice.getInstance().removeObserver(deleteObserver);deleteObserver = null;Log.v("lgq", "......11..onDestroy....");
}
调用,通知观察者,主要在receiver中调用,fragment,activity都可以调用,跨界面刷新数据
TestObServernotice.getInstance().notifyObserver(111, 1, "6666", null)
改进版本
public class MqttMsgBean {//public ParamBean data;public chuhuoBean orders;public String type;public String macno;public int speed;public String order_id;public int user_id;public static class ParamBean {/*** number : 1*/public String version;//版本号public String url;//apk下载路径public String pickcode;//取货码public String order_id;//订单IDpublic int order_user_id;//订单类型0安卓屏。1公众号、2.预定}public static class chuhuoBean {public int amount;//数量public int length;//长度} }
public interface MyObserver {void update(int id, int intparameter, String string, MqttMsgBean bitmap); }
*/ public class ObServernotice implements Subject {private static Subject subject;private List<MyObserver> observers = new ArrayList<>();private ObServernotice() {// TODO Auto-generated constructor stub}public static Subject getInstance() {if (null == subject) {synchronized (ObServernotice.class) {if (null == subject){subject = new ObServernotice();}}}return subject;}@Overridepublic void addObserver(MyObserver observer) {// TODO Auto-generated method stubobservers.add(observer);}@Overridepublic boolean removeObserver(MyObserver observer) {// TODO Auto-generated method stubreturn observers.remove(observer);}@Overridepublic void notifyObserver(int id, int intparam, String param, MqttMsgBean bitmap) {// TODO Auto-generated method stubfor (MyObserver observer : observers) {observer.update(id,intparam,param,bitmap);}}@Overridepublic void notifyObserver(int id) {for (MyObserver observer : observers) {observer.update(id,0,"",null);}} }
public interface Subject {/*** 注册一个观察者*/void addObserver(MyObserver observer);/*** 删除一个观察者** @return 是否删除成功*/boolean removeObserver(MyObserver observer);/*** 通知所有的观察者** @param id 对应观察者的id*/void notifyObserver(int id, int intparameter, String s, MqttMsgBean bitmap);void notifyObserver(int id);}
public interface MyObserver {void update(int id, int intparameter, String string, MqttMsgBean bitmap); }
public abstract class BaseClass extends Activity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);ObServernotice.getInstance().addObserver(observer);}public void setbutt(){Button button = findViewById(R.id.abc);button.setText("aaaaaaaaa2322");}public void mStartactivity(Class<? extends Activity> cls){startActivity(new Intent(BaseClass.this,cls));}public abstract void say();MyObserver observer = new MyObserver() {@Overridepublic void update(int id, int intparameter, String string, MqttMsgBean bitmap) {Observernotice(id,intparameter,string,bitmap);}};@Overrideprotected void onDestroy() {super.onDestroy();ObServernotice.getInstance().removeObserver(observer);}public abstract void Observernotice(int id,int intp,String stringp,MqttMsgBean bean); }