observer

意图:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新
主要解决:一个对象状态改变给其他对象通知的问题,而且要考虑到易用和低耦合,保证高度的协作
什么时候使用:一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知,进行广播通知
 关键代码:在抽象类里有一个ArrayList放观察者们
应用实例:1、拍卖的时候,拍卖师观察最高标价,然后通知给其他竞价者竞价 2、西游记里面悟空请求菩萨降服红孩儿,菩萨洒了一地水招来一个老乌龟,这个乌龟就是观察者,他观察菩萨洒水这个动作
优点:1、观察者和被观察者是抽象耦合的 2、建立一套触发机制
缺点:1、如果一个被观察者对象有很多的直接和间接的观察者的话,将所有的观察者都通知到会花费很多时间.2、 如果在观察者和观察目标之间有循环依赖的话,观察目标会触发它们之间进 行循环调用,可能导致系统崩溃.3、 观察者模式没有相应的机制让观察者知道所观察的目标对象是怎么发生变化的,而仅仅只是知道观察目标发生了变化.
使用场景:1、有多个子类共有的方法,且逻辑相同 2、重要的、复杂的方法,可以考虑作为模板方法
注意事项:1、JAVA中已经有了对观察者模式的支持类 2、避免循环引用 3、如果顺序执行,某一观察者错误会导致系统卡壳,一般采用异步方式

package com.bjsxt.dp.observer.test;
interface ActionListener {
	public void actionPerformed(ActionEvent e);
}

package com.bjsxt.dp.observer.test;
class ActionEvent {
	
	long when;
	Object source;
	
	public ActionEvent(long when, Object source) {
		super();
		this.when = when;
		this.source = source;
	}
	
	
	public long getWhen() {
		return when;
	}

	public Object getSource() {
		return source;
	}
	
}

package com.bjsxt.dp.observer.test;

import java.util.ArrayList;
import java.util.List;

class Button {
	
	private List<ActionListener> actionListeners = new ArrayList<ActionListener>();
	
	public void buttonPressed() {
		ActionEvent e = new ActionEvent(System.currentTimeMillis(),this);
		for(int i=0; i<actionListeners.size(); i++) {
			ActionListener l = actionListeners.get(i);
			l.actionPerformed(e);
		}
	}
	
	public void addActionListener(ActionListener l) {
		actionListeners.add(l);
	}
}

package com.bjsxt.dp.observer.test;
class MyActionListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		System.out.println("button pressed!");
	}
}

package com.bjsxt.dp.observer.test;
class MyActionListener2 implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		System.out.println("button pressed 2!");
	}
}

package com.bjsxt.dp.observer.test;
public class Test {
	public static void main(String[] args) {
		Button b = new Button();
		b.addActionListener(new MyActionListener());
		b.addActionListener(new MyActionListener2());
		b.buttonPressed();
	}
}
package com.bjsxt.dp.observer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

class WakenUpEvent {
	private long time;
	private String loc;
	private Child source;
	
	public WakenUpEvent(long time, String loc, Child source) {
		super();
		this.time = time;
		this.loc = loc;
		this.source = source;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	public Child getSource() {
		return source;
	}
	public void setSource(Child source) {
		this.source = source;
	}
}

class Child implements Runnable {
	private List<WakenUpListener> wakenUpListeners = new ArrayList<WakenUpListener>();

	public void addWakenUpListener(WakenUpListener l) {
		wakenUpListeners.add(l);
	}
	
	void wakeUp() {
		for(int i=0; i<wakenUpListeners.size(); i++) {
			WakenUpListener l = wakenUpListeners.get(i);
			l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
		}
	}

	public void run() {
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		this.wakeUp();
	}
	
}

class Dad implements WakenUpListener {

	public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {
		System.out.println("feed child");
	}
	
}

class GrandFather  implements WakenUpListener {

	public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {
		System.out.println("hug child");
	}
	
}

class Dog implements WakenUpListener {

	public void ActionToWakenUp(WakenUpEvent arg0) {
		System.out.println("wang wang ...");
	}
	
}

interface WakenUpListener {
	public void ActionToWakenUp(WakenUpEvent wakenUpEvent);
}

public class Test {
	public static void main(String[] args) {
		Child c = new Child();
		
		
		String[] observers = PropertyMgr.getProperty("observers").split(",");

		for(String s : observers) {
			try {
				c.addWakenUpListener((WakenUpListener)(Class.forName(s).newInstance()));
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		
		new Thread(c).start();
	}
}

class PropertyMgr {
	private static Properties props = new Properties();
	
	static {
		try {
			props.load(Test.class.getClassLoader().getResourceAsStream("com/bjsxt/dp/observer/observer.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getProperty(String key) {
		return props.getProperty(key);
	}
}

class CryEvent {
}

abstract class Event {

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值