设计模式之观察者模式简单运用

先上一段JDK的观察者源码

public class Observable {
    private boolean changed = false;
    private Vector obs;

    /** Construct an Observable with zero Observers. */

    public Observable() {
        obs = new Vector();
    }


    public synchronized void addObserver(Observer o) {
        if (o == null)
            throw new NullPointerException();
        if (!obs.contains(o)) {
            obs.addElement(o);
        }
    }


    public synchronized void deleteObserver(Observer o) {
        obs.removeElement(o);
    }


    public void notifyObservers() {
        notifyObservers(null);
    }


    public void notifyObservers(Object arg) {
        /*
         * a temporary array buffer, used as a snapshot of the state of
         * current Observers.
         */
        Object[] arrLocal;

        synchronized (this) {

            if (!changed)
                return;
            arrLocal = obs.toArray();
            clearChanged();
        }

        for (int i = arrLocal.length-1; i>=0; i--)
            ((Observer)arrLocal[i]).update(this, arg);
    }

 
    public synchronized void deleteObservers() {
        obs.removeAllElements();
    }

    protected synchronized void setChanged() {
        changed = true;
    }


    protected synchronized void clearChanged() {
        changed = false;
    }

    public synchronized boolean hasChanged() {
        return changed;
    }

    public synchronized int countObservers() {
        return obs.size();
    }
}
看到这个类的是实现还是不太难的!


package com.git.base.datastructure.observable;

import java.util.Observable;

/**
 * 被观察者
 * <p>Title: PersonObservable.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: Sage</p>
 * @author 五虎将
 * @date 2016年5月19日下午11:28:29
 * @version 1.0
 */
public class PersonObservable extends Observable {

	
	private String name;
	
	private int age;

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
	public boolean setUpdate(String name,int age){
		this.name = name;
		this.age  = age;
		setChanged();
		notifyObservers(age);
		return true;
	}
	
	

}

package com.git.base.datastructure.observable;

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

/**
 * 人类年龄变化监听器
 * <p>Title: PersonAgeObserver.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: Sage</p>
 * @author 五虎将
 * @date 2016年5月19日下午11:43:37
 * @version 1.0
 */
public class PersonAgeObserver implements Observer{

	@Override
	public void update(Observable o, Object age) {
		System.out.println(this + "  变化后年龄为 " + age);
	}



}


package com.git.base.datastructure.observable;
/*
 * 演示类
 */
public class MyObserverDemo {

	public static void main(String[] args) {
		
		PersonObservable master = new PersonObservable();
		
		PersonAgeObserver ageObserver = new PersonAgeObserver();
		PersonAgeObserver ageObserver2 = new PersonAgeObserver();
		master.addObserver(ageObserver);
		master.addObserver(ageObserver2);
		master.setUpdate("Sage", 22);
		
		
	}
}





你可能感兴趣的:(java,观察者模式,observer)