[Design pattern] Android design pattern-Observer Pattern self-insight, android Design Pattern
The observer mode is widely used in Android. Now, let's take a look at your understanding and write a simple demo.
When a teacher lectures, the teacher is an Observer, and he should inherit from the Observer
When a student lectures, It is the observer. He should implement the observer interface.
In this way, students can receive messages when they give lectures.
Package com. ferris. observice; import java. util. observable; import java. util. observer; import android. content. context; import android. widget. toast; // Observer 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 ;}@ Overridepublic 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, "Good Teacher! ", 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; // Observer 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;} @ Overridepublic 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 () {}@ Overridepublic void update (Observable observable, Object data) {// TODO Auto-generated method s Tubthis. girl = (Teacher) observable; Toast. makeText (context, "Good Teacher! ", 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; @ Overrideprotected void onCrea Te (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. strategy); teacher = new Teacher (); // Student student = new Student (); student. setContext (this); student. setGirl (teacher); // Add the observed object student. setListeming (new Listeming () {@ Overridepublic void lisetem (String sss) {// TODO Auto-generated method stubToast. makeText (StrategyActivity. this, "received", 1 ). show () ;}}); teacher. add Observer (student); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubteacher. speak ("Hello everyone, I am a teacher! ");}});}}