码迷,mamicode.com
首页 > 移动开发 > 详细

【设计模式篇】Android设计模式之-观察者模式 自我见解

时间:2014-11-26 18:56:06      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:android   blog   os   使用   sp   java   on   2014   log   

观察者模式在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设计模式之-观察者模式 自我见解

标签:android   blog   os   使用   sp   java   on   2014   log   

原文地址:http://blog.csdn.net/xufeifandj/article/details/41519385

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!