《23种设计模式》二:观察者模式

项目背景

      学校为了丰富学生的业余生活,加强素质教育和价值观的养成,准备了大约30期的公开课,但是具体时间和教室要根据报名的人数和大家时间来定,目前开放的学院的有两个,一个是人文学院一个是计算机学院。要求就是教务处把课程上传之后,系统可以自动通知这两个学院,然后由学院具体统计人数。项目经理小王整理思路之后。。。。

代码实现

/**
 * 公开课
 * 一切从简(只为举栗子!)
 */
public class OpenClass {

	public String title;
	
	public String place;
	
	public String time;
	
	public OpenClass(String title, String place, String time) {
		this.title = title;
		this.place = place;
		this.time = time;
	}

	@Override
	public String toString() {
		return "公开课 [title=" + title + ", place=" + place + ", time=" + time + "]";
	}
}

/**
 * 教务处
 */
public class EduOffice {
	
	private OpenClass openClass;
	
	private ComputerCollege computerCollege;
	
	private HumanityCollege humanityCollege;
	
	
	public EduOffice(ComputerCollege computerCollege, HumanityCollege humanityCollege) {
		this.computerCollege = computerCollege;
		this.humanityCollege = humanityCollege;
	}
	
	/**
	 * 教务处安排公开课信息
	 */
	public void setData(OpenClass openClass){
		this.openClass = openClass;
		this.pushMsg();
	}
	
	/**
	 * 给各个学院推送消息
	 */
	public void pushMsg(){
		computerCollege.update(openClass);
		humanityCollege.update(openClass);
	}
}

/**
 * 计算机学院
 */
public class ComputerCollege {

	private OpenClass openClass;
	
	public void update(OpenClass openClass){
		this.openClass = openClass;
		display();
	}

	public void display() {
		System.out.println("计算机学院发布信息:" +openClass.toString());
	}
}

/**
 * 人文学院
 * @project Test
 * @date 2018年4月25日 上午10:24:31 
 * @author Huaxu-Charles
 */
public class HumanityCollege {

	private OpenClass openClass;
	
	public void update(OpenClass openClass){
		this.openClass = openClass;
		display();
	}

	public void display() {
		System.out.println("人文学院发布信息:" + openClass.toString());
	}
}
// 主程序
	public static void main(String[] args) {
		ComputerCollege computerCollege = new ComputerCollege();
		HumanityCollege humanityCollege = new HumanityCollege();
		
		EduOffice eduOffice = new EduOffice(computerCollege, humanityCollege);
		eduOffice.setData(new OpenClass("论阅读的重要性", "科技大厦w505", "周三上午3-4节"));
	}
// 运行结果
计算机学院发布信息:公开课 [title=论阅读的重要性, place=科技大厦w505, time=周三上午3-4节]
人文学院发布信息:公开课 [title=论阅读的重要性, place=科技大厦w505, time=周三上午3-4节]

      小王同学这次反应很快就完成老师的要求也得到了表扬。公开课上过两节之后发现效果特别好,经济学院也参与了报名,这个时候教务处的老师又喊来了小王,他想了一下,这下可是有点麻烦了,如果加一个院系的话,那么整体的代码都需要重新编译,不仅如此,如果明天再加一个院系怎么办那?

     如果是单纯这一次的话,代码可以改成

/**
 * 经济学院
 */
public class EconomicsCollege {

	private OpenClass openClass;
	
	public void update(OpenClass openClass){
		this.openClass = openClass;
		display();
	}

	public void display() {
		System.out.println("经济学院发布信息:" + openClass.toString());
	}
}

/**
 * 教务处
 */
public class EduOffice {
	
	private OpenClass openClass;
	
	private ComputerCollege computerCollege;
	
	private HumanityCollege humanityCollege;
	
	private EconomicsCollege economicsCollege;
	
	public EduOffice(ComputerCollege computerCollege, HumanityCollege humanityCollege, EconomicsCollege economicsCollege) {
		this.computerCollege = computerCollege;
		this.humanityCollege = humanityCollege;
		this.economicsCollege = economicsCollege;
	}
	
	/**
	 * 教务处安排公开课信息
	 */
	public void setData(OpenClass openClass){
		this.openClass = openClass;
		this.pushMsg();
	}
	
	/**
	 * 给各个学院推送消息
	 */
	public void pushMsg(){
		computerCollege.update(openClass);
		humanityCollege.update(openClass);
		economicsCollege.update(openClass);
	}
}

     其余的不需要变化即可。但是如果再添加不能每次就这样,于是小王同学日思夜想,还参看了华绪同学的博客,找到了解决问题的思路!!!!

/**
 * 订阅者接口
 */
public interface ObServer {
	public void update(OpenClass openClass);
}

/**
 * 服务端接口
 */
public interface Subject {
	/**
	 * 注册观察者(订阅者)
	 */
	public void registerObserver(ObServer o);
	/**
	 * 删除观察者(订阅者)
	 */
	public void removeObserver(ObServer o);
	/**
	 * 发布信息
	 */
	public void notifyObservers();
}

/**
 * 教务处
 */
public class EduOffice implements Subject{
	
	private OpenClass openClass;
	
	private ArrayList<ObServer> obServerList;
	
	public EduOffice() {
		obServerList = new ArrayList<ObServer>();
	}
	/**
	 * 教务处安排公开课信息
	 */
	public void setData(OpenClass openClass){
		this.openClass = openClass;
		this.notifyObservers();
	}

	@Override
	public void registerObserver(ObServer o) {
		obServerList.add(o);
	}

	@Override
	public void removeObserver(ObServer o) {
		if (obServerList.contains(o)) {
			obServerList.remove(o);
		}
	}

	@Override
	public void notifyObservers() {
		for (ObServer obServer : obServerList) {
			obServer.update(openClass);
		}
	}
}

/**
 * 计算机学院
 */
public class ComputerCollege implements ObServer{

	private OpenClass openClass;

	@Override
	public void update(OpenClass openClass) {
		this.openClass = openClass;
		display();		
	}

	public void display() {
		System.out.println("计算机学院发布信息:" +openClass.toString());
	}
}

/**
 * 经济学院
 */
public class EconomicsCollege implements ObServer{

	private OpenClass openClass;
	
	@Override
	public void update(OpenClass openClass) {
		this.openClass = openClass;
		display();		
	}

	public void display() {
		System.out.println("经济学院发布信息:" + openClass.toString());
	}
}

/**
 * 人文学院
 */
public class HumanityCollege implements ObServer{

	private OpenClass openClass;
	
	@Override
	public void update(OpenClass openClass) {
		this.openClass = openClass;
		display();		
	}

	public void display() {
		System.out.println("人文学院发布信息:" + openClass.toString());
	}
}
// 主程序
	public static void main(String[] args) {
		ComputerCollege computerCollege = new ComputerCollege();
		HumanityCollege humanityCollege = new HumanityCollege();
		EconomicsCollege economicsCollege = new EconomicsCollege();
		
		EduOffice eduOffice = new EduOffice();
		eduOffice.registerObserver(computerCollege);
		eduOffice.registerObserver(humanityCollege);
		eduOffice.registerObserver(economicsCollege);
		eduOffice.setData(new OpenClass("论阅读的重要性", "科技大厦W505", "周三上午3-4节"));
		
		System.out.println("=============分割线==============");
		eduOffice.removeObserver(humanityCollege);
		eduOffice.setData(new OpenClass("解读李开复写给大学生的七封信", "科技大厦N305", "周六上午3-4节"));
	}
// 运行结果
计算机学院发布信息:公开课 [title=论阅读的重要性, place=科技大厦W505, time=周三上午3-4节]
人文学院发布信息:公开课 [title=论阅读的重要性, place=科技大厦W505, time=周三上午3-4节]
经济学院发布信息:公开课 [title=论阅读的重要性, place=科技大厦W505, time=周三上午3-4节]
=============分割线==============
计算机学院发布信息:公开课 [title=解读李开复写给大学生的七封信, place=科技大厦N305, time=周六上午3-4节]
经济学院发布信息:公开课 [title=解读李开复写给大学生的七封信, place=科技大厦N305, time=周六上午3-4节]

       经过解决这个问题,就引出了观察者模式的定义:定义对象间一种一对多的依赖关系,使得当每一个对象改变状态,则所有依赖于它的对象都会得到通知并自动更新。

观察者模式结构

被观察者 这个角色可以是接口,也可以是抽象类或者具体的类,因为很多情况下会与其他的模式混用,所以使用接口的情况比较多,具体应该包含三个方法,一个注册观察者,一个删除观察者,一个发布消息
观察者   观察者角色一般是一个接口,它只有一个update方法,在被观察者状态发生变化时,这个方法就会被触发调用。
具体的被观察者 使用这个角色是为了便于扩展,可以在此角色中定义具体的业务逻辑。
具体的观察者         观察者接口的具体实现,在这个角色中,将定义被观察者对象状态发生变化时所要处理的逻辑。

优点

  1. 观察者与被观察者之间是属于轻度的关联关系,并且是抽象耦合的,这样,对于两者来说都比较容易进行扩展。
  2. 观察者模式是一种常用的触发机制,它形成一条触发链,依次对各个观察者的方法进行处理。但同时,这也算是观察者模式一个缺点,由于是链式触发,当观察者比较多的时候,性能问题是比较令人担忧的。并且,在链式结构中,比较容易出现循环引用的错误,造成系统假死

总结

       java语言中,有一个接口Observer,以及它的实现类Observable,对观察者角色常进行了实现。我们可以在jdk的api文档具体查看这两个类的使用方法。

猜你喜欢

转载自blog.csdn.net/weixin_39923425/article/details/80075811