天天看点

Java 简简单单理解观察者模式

我所理解的观察者模式,就是当一个对象发生改变时,可以让起其的对象接收到信息并作出回应。

按照我的粗陋经验来讲,比如Android应用打开多个界面时,当我们在某个界面按退出按钮,就得让其他所有的界面都结束,即执行.finish()方法。这个以通过维护一个全局的由活动的页面(Activity)的引用组成的列表来实现。当程序退出时,此列表挨个执行所有引用的.finish()方法,当然也可以用Android的进程栈来实现,与本题无关不另说。再比如做绘图程序,当界面的某些数据元素发生改变时,也应通知界面元素执行.repaint(),方法,诸如此类,不胜枚举。

观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。

我整理了一下,可以让自己或其他初学者了解开发者的通行做法。

①简单例子

两个接口

public interface Observer {
	public void update();
}
           
public interface Subject {
	public void attach(Observer o);

	public void detach(Observer o);

	public void notice();
}
           

两个继承他们的类

public class Student implements Observer {
	private String name;      
	private String phone;      
	private Teacher teacher;      
	public Student(String name,Teacher t){
		this.name = name;
		teacher = t;      
	}      
	public void show(){         
	System.out.println("Name:"+name+"\nTeacher'sphone:"+phone);      
	}   
	@Override
	public void update() {
		phone = teacher.getPhone();
	}

}
           
public class Teacher implements Subject {
	private String phone;
	private Vector students;

	public Teacher() {
		phone = "";
		students = new Vector();
	}

	@Override
	public void attach(Observer o) {
		students.add(o);
	}

	@Override
	public void detach(Observer o) {
		students.remove(o);
	}

	@Override
	public void notice() {
		for (int i = 0; i < students.size(); i++)
			((Observer) students.get(i)).update();
	}

	public void setPhone(String phone) {
		this.phone = phone;
		notice();
	}

	public String getPhone() {
		return phone;
	}
}
           

验证类:

public static void main(String[] args) {
		Vector<Student> students = new Vector<Student>();         
		Teacher t = new Teacher();         
		for(int i= 0 ;i<10;i++){
			Student st = new Student("lili"+i,t);
			students.add(st);
			t.attach(st);         
		}         
		t.setPhone("110");         
		for(int i=0;i<10;i++)                
		students.get(i).show();         
		t.setPhone("10086");         
		for(int i=0;i<10;i++)                
		students.get(i).show();      
	}  
}
           

运行结果:

Name:lili0

Teacher'sphone:110

Name:lili1

Teacher'sphone:110

Name:lili2

Teacher'sphone:110

Name:lili3

Teacher'sphone:110

Name:lili4

Teacher'sphone:110

Name:lili5

Teacher'sphone:110

Name:lili6

Teacher'sphone:110

Name:lili7

Teacher'sphone:110

Name:lili8

Teacher'sphone:110

Name:lili9

Teacher'sphone:110

Name:lili0

Teacher'sphone:10086

Name:lili1

Teacher'sphone:10086

Name:lili2

Teacher'sphone:10086

Name:lili3

Teacher'sphone:10086

Name:lili4

Teacher'sphone:10086

Name:lili5

Teacher'sphone:10086

Name:lili6

Teacher'sphone:10086

Name:lili7

Teacher'sphone:10086

Name:lili8

Teacher'sphone:10086

Name:lili9

Teacher'sphone:10086

②用到Java的API

两个类:

public class Watched extends Observable {
	private String data = "";

	public String retrieveData() {
		return data;
	}

	public void changeData(String data) {
		if (!this.data.equals(data)) {
			this.data = data;
			setChanged();
		}

		notifyObservers();
	}
}
           
public class Watcher implements Observer {
	public Watcher(Watched w)
	{
		w.addObserver(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		System.out.println("Data has been changed to: '" + ((Watched)o).retrieveData() + "'---args=="+arg);
	}
}
           

验证:

public class Tester {
	static private Watched watched;
	static private Observer watcher;

	public static void main(String[] args) {
		watched = new Watched();

		watcher = new Watcher(watched);

		watched.changeData("In C, we create bugs.");
		watched.changeData("In Java, we inherit bugs.");
		watched.changeData("In Java, we inherit bugs.");
		watched.changeData("In Visual Basic, we visualize bugs.");
	}
}
           

结果:

Data has been changed to: 'In C, we create bugs.'---args==null

Data has been changed to: 'In Java, we inherit bugs.'---args==null

Data has been changed to: 'In Visual Basic, we visualize bugs.'---args==null