[Design pattern] Android design pattern-Observer Pattern self-insight, android Design Pattern

Source: Internet
Author: User

[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! ");}});}}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.