自己实现的观察者模式

JAVA标准库实现
package com.moshi.observer;

import java.util.Date;
import java.util.Observable;

public class Product extends Observable  {
	
	private Long id;
	
	private String name;
	
	private Date endDate;
	
	private Integer price;
	
	

	public Product(Long id, String name, Integer price, Date date) {
		super();
		// TODO Auto-generated constructor stub
		endDate = date;
		this.id = id;
		this.name = name;
		this.price = price;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getPrice() {
		return price;
	}

	public void setPrice(Integer price) {
		this.setChanged();//修改价格通知 第一次
		notifyObservers(this.price);
		this.price = price;
		setChanged();//修改价格通知 第二次
		notifyObservers(price);
	}
	
	

}



package com.moshi.observer;

import java.util.Observable;
import java.util.Observer;

public class ProductObserver implements Observer {

	public void update(Observable arg, Object obj) {
		// TODO Auto-generated method stub
		System.out.println(arg.getClass().toString());
		if(obj instanceof Integer){
			System.out.println("产品:" + ((Product)arg).getName() +",最新价格修改为" + obj.toString());
		}

	}

}

package com.moshi.observer;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Product product = new Product(1L, "鞋", 1,new java.util.Date());
		product.addObserver(new ProductObserver());
		product.setPrice(2);
	}

}



我自己的实现,其实原理很简单
package org.moshi.observer;

import java.util.Observable;

/**
 * 观察对象的基础接口,所有观察者统一实现
 * @author Administrator
 *
 */
public interface ObsInterface {
	
	public void update(Object obj1, Object obj);

}



package org.moshi.observer;

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

/**
 * 继承该对象便拥有了被观察的能力
 * @author Administrator
 *
 */
public class ObserverBean {
	
	private boolean able = false;
	
	private List<ObsInterface> observers = new ArrayList<ObsInterface>();
	
	public void addObserver(ObsInterface o){
		observers.add(o);
	}
	
	public void setChanged(){
		this.able = true;
	}
	
	public void notifyObservers(Object obj1,Object obj2){
		if(this.able){
			for(ObsInterface o: observers){
				o.update(obj1,obj2);
			}
		}
		this.able = false;
	}
	
	public void notifyObservers(Object obj2){
		notifyObservers(this,obj2);
	}

}


package org.moshi.observer;

import java.util.Date;

public class Product extends ObserverBean{
	private Long id;
	
	private String name;
	
	private Date endDate;
	
	private Integer price;
	
	

	public Product(Long id, String name, Integer price, Date date) {
		super();
		// TODO Auto-generated constructor stub
		endDate = date;
		this.id = id;
		this.name = name;
		this.price = price;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getPrice() {
		return price;
	}

	public void setPrice(Integer price) {
		this.setChanged();//修改价格通知
		notifyObservers(this.price);
		this.price = price;
		this.setChanged();//修改价格通知
		notifyObservers(2);
	}
}


package org.moshi.observer;

import org.moshi.observer.Product;
/**
*实现观察者接口
*/
public class ProductObserver implements ObsInterface {

	public void update(Object obj1, Object obj) {
		// TODO Auto-generated method stub
		System.out.println(obj1.getClass().toString());
		if(obj instanceof Integer){
			System.out.println("产品:" + ((Product)obj1).getName() +",最新价格修改为" + obj.toString());
		}		

	}

}

你可能感兴趣的:(java)