【设计模式篇】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("大家好我是老师哦!"); } } ); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。