首页 > 代码库 > 【设计模式篇】Android设计模式之-观察者模式 自我见解
【设计模式篇】Android设计模式之-观察者模式 自我见解
观察者模式在Android广泛的使用。现在结合自己的理解,并且写一个简单demo.
当老师讲课的时候,老师就是一个被观察者,他应该继承于Observer
学生听课的时候,就是观察者,他应该实现观察者的接口。
这样的话,老师讲课的时候,学生都能收到消息了
package com.ferris.observice; import java.util.Observable; import java.util.Observer; import android.content.Context; import android.widget.Toast; //观察者 public class Student implements Observer{ private Teacher teacher; private Context context; private Listeming listeming; public Listeming getListeming() { return listeming; } public void setListeming(Listeming listeming) { this.listeming = listeming; } public Context getContext() { return context; } @Override public String toString() { return "Student [<pre name="code" class="java">teacher=" + girl + ", context=" + context + "]";}public void setContext(Context context) {this.context = context;}public Teacher getGirl() {return girl;}public void setGirl(Teacher girl) {this.girl = girl;}public Student(){}@Overridepublic void update(Observable observable, Object data) {// TODO Auto-generated method stubthis.girl = (Teacher)observable;Toast.makeText(context, "老师好!", 1).show();}public interface Listeming{public void lisetem(String sss);}}
package com.ferris.observice; import java.util.Observable; import java.util.Observer; import android.content.Context; import android.widget.Toast; //观察者 public class Student implements Observer{ private Teacher girl; private Context context; private Listeming listeming; public Listeming getListeming() { return listeming; } public void setListeming(Listeming listeming) { this.listeming = listeming; } public Context getContext() { return context; } @Override public String toString() { return "Student [girl=" + girl + ", context=" + context + "]"; } public void setContext(Context context) { this.context = context; } public Teacher getGirl() { return girl; } public void setGirl(Teacher girl) { this.girl = girl; } public Student(){ } @Override public void update(Observable observable, Object data) { // TODO Auto-generated method stub this.girl = (Teacher)observable; Toast.makeText(context, "老师好!", 1).show(); } public interface Listeming{ public void lisetem(String sss); } }
package com.ferris.androidpattern; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import com.ferris.observice.Student; import com.ferris.observice.Student.Listeming; import com.ferris.observice.Teacher; public class StrategyActivity extends Activity { private Teacher teacher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.strategy); teacher=new Teacher(); //被观察者 Student student=new Student(); student.setContext(this); student.setGirl(teacher);//添加观察对象 student.setListeming(new Listeming() { @Override public void lisetem(String sss) { // TODO Auto-generated method stub Toast.makeText(StrategyActivity.this, "收到", 1).show(); } }); teacher.addObserver(student); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub teacher.Speak("大家好我是老师哦!"); } } ); } }
【设计模式篇】Android设计模式之-观察者模式 自我见解
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。