首页 > 代码库 > 观察者模式总结

观察者模式总结

观察者模式由两部分组成:观察者、主题。

观察者模式的核心思想:当主题发生变化是,告知所有有观察者。

观察者模式的实现方法:观察者模式实现方式分两种 推送模式【push】和索取模式【pull】,共同点主题持有观察者,当有变化时通知观察者.区别在于消息的传递:

推送模式: 由主题将消息传递给观察者,消息包含主题中所有有用值;

索取模式: 由主题将自身的引用值传递给观察者,由观察者决定需要获取哪些值是必要的。

观察者模式的用途:消息广播的一种解决方案。

观察者模式的缺点:1.当观察者数量比较多时,遍历观察者会降低性能;2.尽量避免闭环,在遍历是会造成系统假死。


简单示例

模拟新闻推送,包含主体:服务器,客户端。当服务器接收到信息时,将消息推送给各种客户端

推送模式

package observerpattern.server;

import observerpattern.client.Observer;

/**
 * 主题接口
 */
public interface Observable {

	/**
	 * 向主题中添加观察者
	 * 
	 * @param ob
	 * @return
	 */
	String add(Observer ob);
	
	/**
	 * 从主题中移出观察者
	 * 
	 * @param ob
	 * @return
	 */
	String remove(Observer ob);
	
	/**
	 * 发送消息
	 * 
	 * @param msg
	 * @return
	 */
	String sendMessage(Message msg);
}

package observerpattern.server;

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

import observerpattern.client.Observer;

public class Server implements Observable{

	private List<Observer> ls = new ArrayList<Observer>();
	
	@Override
	public String add(Observer ob) {
		
		if(ob != null && !ls.contains(ob))
			ls.add(ob);
		
		return Constants.SUCCESS;
	}

	@Override
	public String remove(Observer ob) {
		
		if(ob!=null && ls.contains(ob))
			ls.remove(ob);
		
		return Constants.SUCCESS;
	}

	@Override
	public String sendMessage(Message msg) {

		for (Observer ob : ls) 
		{
			ob.showMessage(msg);
		}
		return Constants.SUCCESS;
		
	}

}

package observerpattern.server;

public class Message {

	private String title;
	private String contents;
	private String time;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

}

package observerpattern.server;

public class Constants {

	private Constants(){}
	
	public static final String SUCCESS = "success"; //成功
	
}

package observerpattern.client;

import observerpattern.server.Message;

/**
 * 观察者接口
 */
public interface Observer {
	
	/**
	 * 显示消息
	 * 
	 * @param msg
	 * @return
	 */
	void showMessage(Message msg);
	
}

package observerpattern.client;

import observerpattern.server.Message;

public class Client implements Observer{

	private String name;
	
	public Client(String name)
	{
		this.name = name;
	}
	
	@Override
	public void showMessage(Message msg) {
		System.out.println(name+":Title->"+msg.getTitle()+" Time->"+msg.getTime()+" Contents->"+msg.getContents());
	}

}

索取模式

根据推送模式,进行简单修改

package observerpattern.server;

import observerpattern.client.Observer;

/**
 * 主题接口
 */
public interface Observable {

	/**
	 * 向主题中添加观察者
	 * 
	 * @param ob
	 * @return
	 */
	String add(Observer ob);
	
	/**
	 * 从主题中移出观察者
	 * 
	 * @param ob
	 * @return
	 */
	String remove(Observer ob);
	
	/**
	 * 发送消息
	 * 
	 * @param msg
	 * @return
	 */
	String sendMessage(Message msg);
	
	/**
	 * 将信息传递给观察者
	 * 
	 * @param msg
	 */
	void notifyObservers();
}

package observerpattern.server;

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

import observerpattern.client.Observer;

public class Server implements Observable{

	private List<Observer> ls = new ArrayList<Observer>();
	
	@Override
	public String add(Observer ob) {
		
		if(ob != null && !ls.contains(ob))
			ls.add(ob);
		
		return Constants.SUCCESS;
	}

	@Override
	public String remove(Observer ob) {
		
		if(ob!=null && ls.contains(ob))
			ls.remove(ob);
		
		return Constants.SUCCESS;
	}

	@Override
	public String sendMessage(Message msg) {

		this.msg = msg;
		
		notifyObservers();
		
		return Constants.SUCCESS;
		
	}

	private Message msg = null;
	
	@Override
	public void notifyObservers() {
		for (Observer ob : ls) {
			ob.saveMessage(msg);
		}
	}

}

package observerpattern.server;

public class Message {

	private String title;
	private String contents;
	private String time;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

}

package observerpattern.server;

public class Constants {

	private Constants(){}
	
	public static final String SUCCESS = "success"; //成功
	
}

package observerpattern.client;

import observerpattern.server.Message;

/**
 * 观察者接口
 */
public interface Observer {
	
	/**
	 * 显示消息
	 * 
	 * @param msg
	 * @return
	 */
	void showMessage();

	void saveMessage(Message msg);
	
}

package observerpattern.client;

import observerpattern.server.Message;

public class Client implements Observer{

	private String name;
	
	private Message msg = null;
	
	public Client(String name)
	{
		this.name = name;
	}
	
	@Override
	public void showMessage() {
		System.out.println(name+":Title->"+msg.getTitle()+" Time->"+msg.getTime()+" Contents->"+msg.getContents());
	}

	@Override
	public void saveMessage(Message msg) {
		this.msg = msg;
	}

}

package observerpattern.test;

import observerpattern.client.Client;
import observerpattern.client.Observer;
import observerpattern.server.Message;
import observerpattern.server.Observable;
import observerpattern.server.Server;

public class Test {

	public static void main(String[] args) {
		Observable  server = new Server();
		
		Observer ob1 = new Client("phone");
		Observer ob2 = new Client("pad");
		Observer ob3 = new Client("computer");
		
		server.add(ob1);
		server.add(ob2);
		server.add(ob3);
		
		Message msg = new Message();
		msg.setTitle("新闻标题");
		msg.setTime("2014/10/31");
		msg.setContents("都是呵呵啊");
		
		
		server.sendMessage(msg);
		
		ob1.showMessage();
	}

}

运行结果

phone:Title->新闻标题 Time->2014/10/31 Contents->都是呵呵啊



观察者模式总结