Android——观察者模式的使用
2017-08-01 10:50
127 查看
观察者模式的使用
大致需要四步:
1.提供观察者接口,用来更新update数据
2.创建具体的观察者,实现该接口,具体实现接口的更新方法
(可以设置观察者名字,根据目标对象提醒要做的事情)
3.创建目标对象(被观察的)subject,添加加入、删除、提醒所有观察者的方法
4.创建具体目标对象,继承subject,获得具体目标对象内容(天气),并且在设置的时候
this.notify通知所有观察者。
在activity中调用
同样是一个天气情况,观察天气情况两个观察者做出不一样的选择,这便是观察者模式的一种体现。
大致需要四步:
1.提供观察者接口,用来更新update数据
/**观察者接口,给那些观察者数据变化发生时候的更新 * Created by PeLon on 2017/7/28. */ public interface Observer { public void update(Subject subject); }
2.创建具体的观察者,实现该接口,具体实现接口的更新方法
(可以设置观察者名字,根据目标对象提醒要做的事情)
public class ConcreObserver implements Observer { //观察者名字 private String name; private Context context; //获取目标类的状态同步到观察者的状态中 private String WeatherContent; //提醒要做的事情 private String remindThing; public ConcreObserver(Context context) { this.context=context; } public ConcreObserver(String name, String weatherContent, String remindThing) { this.name = name; WeatherContent = weatherContent; this.remindThing = remindThing; } @Override public void update(Subject subject) { WeatherContent=((ConcreSubject)subject).getWeatherContent(); String str=name+"收到了天气情况:"+WeatherContent+"准备"+remindThing+"\n"; Intent intent=new Intent(); intent.setAction("a"); intent.putExtra("response",str); context.sendBroadcast(intent); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWeatherContent() { return WeatherContent; } public void setWeatherContent(String weatherContent) { WeatherContent = weatherContent; } public String getRemindThing() { return remindThing; } public void setRemindThing(String remindThing) { this.remindThing = remindThing; } }
3.创建目标对象(被观察的)subject,添加加入、删除、提醒所有观察者的方法
/**目标对象,将观察者放入集合内,并添加加入、删除的借口 * Created by PeLon on 2017/7/28. */ public class Subject { private List<Observer> list=new ArrayList<>(); public void attach(Observer observer) { list.add(observer); } public void deleteObserver(Observer observer) { list.remove(observer); } public void notifyObservers() { for (Observer observer:list) { observer.update(this); } } }
4.创建具体目标对象,继承subject,获得具体目标对象内容(天气),并且在设置的时候
this.notify通知所有观察者。
public class ConcreSubject extends Subject { //获得具体目标对象内容,并且在设置的时候通知观察者 //获得的天气情况 private String WeatherContent; public ConcreSubject() { } public void setConcerSubjectState(String WeatherContent) { this.WeatherContent = WeatherContent; this.notifyObservers(); } public String getWeatherContent() { return WeatherContent; } }
在activity中调用
public class MainActivity extends AppCompatActivity { private ConcreObserver observer,observer2; private EditText text; private TextView textView; private Button button; private StringBuffer buffer=new StringBuffer(); private BroadcastReceiver receiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String string=intent.getStringExtra("response"); Toast.makeText(MainActivity.this, string,Toast.LENGTH_SHORT).show(); buffer.append(string); textView.setText(buffer.toString()); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text= (EditText) findViewById(R.id.edittext); button= (Button) findViewById(R.id.send); textView= (TextView) findViewById(R.id.textview); IntentFilter filter=new IntentFilter(); filter.addAction("a"); registerReceiver(receiver,filter); //1.创建目标对象 final ConcreSubject weather=new ConcreSubject(); //2.创建观察者 observer=new ConcreObserver(this); observer.setName("妈妈:"); observer.setRemindThing("适合购物"); observer2=new ConcreObserver(this); observer2.setName("女朋友:"); observer2.setRemindThing("适合约会"); //3.注册观察者 weather.attach(observer); weather.attach(observer2); //4.发送目标 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String weathercontent=text.getText().toString().trim(); weather.setConcerSubjectState(weathercontent); } });
同样是一个天气情况,观察天气情况两个观察者做出不一样的选择,这便是观察者模式的一种体现。
相关文章推荐
- Android中观察者设计模式的使用
- 在Android开发中巧妙使用观察者模式避免内存泄露
- 在Android中观察者模式的简单使用
- 6、android单例模式下使用观察者模式
- Android 关于观察者模式使用代码总结
- android开发之观察者模式的使用
- Android中使用软件设计模式之观察者模式、Builder模式、单例模式、简单工厂模式
- 【android】 使用观察者模式更新用户资料
- android——使用观察者模式打造跨线程、跨界面等一对多通讯
- Android使用的设计模式1——观察者模式
- Android使用观察者模式Observer实现网络状态监听
- android设计模式的使用之观察者模式
- [Android进阶]Android中使用的观察者模式
- 观察者模式在Android中使用浅析
- android中对观察者模式的使用,Adapter中的观察者
- 使用观察者模式Observer实现对Android网络状态的监听
- Android中的观察者模式简单使用
- Android使用的设计模式1——观察者模式
- 观察者模式在android中使用