design_model(18)observe

Posted gg128

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了design_model(18)observe相关的知识,希望对你有一定的参考价值。

1.观察者模式

 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象在状态发生变化的时,会通知所有观察者对象,使他们能够自动更新自己。

2.实例1

public class ConcreteSubject implements Subject {
	private int i;

	public void setI(int i) {
		this.i = i;
	}

	private List<Observer> list = new ArrayList<>();

	public void add(Observer observer) {
		list.add(observer);
	}

	public void update() {
		for (Observer observer : list) {
			observer.update(i);
		}
	}
}

public class Observer {
	private int i;

	public void update(int i) {
		this.i = i;
	}

	public int getI() {
		return i;
	}
}

public interface Subject {
	
}

public class Client {
	public static void main(String[] args) {
		ConcreteSubject cs = new ConcreteSubject();
		cs.setI(3);
		Observer observer1 = new Observer();
		Observer observer2 = new Observer();
		cs.add(observer1);
		cs.add(observer2);
		cs.update();
		System.out.println(observer1.getI());
		System.out.println(observer1.getI());
	}
}

3.实例2

 

public class ConcreteSubject extends Observable {
	private int i;

	public void set(int s) {
		this.i = s;
		setChanged();
		notifyObservers(i);
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

}

public class Observe implements Observer {

	private int i;

	@Override
	public void update(Observable o, Object arg) {
		this.i = ((ConcreteSubject) o).getI();
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

}

public class Client {
	public static void main(String[] args) {
		ConcreteSubject cs = new ConcreteSubject();
		Observe observer1 = new Observe();
		Observe observer2 = new Observe();
		cs.addObserver(observer1);
		cs.addObserver(observer2);
		cs.set(3);
		System.out.println(observer1.getI());
		System.out.println(observer1.getI());
	}
}

//源码

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

        synchronized (this) {
            /* We don‘t want the Observer doing callbacks into
             * arbitrary code while holding its own Monitor.
             * The code where we extract each Observable from
             * the Vector and store the state of the Observer
             * needs synchronization, but notifying observers
             * does not (should not).  The worst result of any
             * potential race-condition here is that:
             * 1) a newly-added Observer will miss a
             *   notification in progress
             * 2) a recently unregistered Observer will be
             *   wrongly notified when it doesn‘t care
             */
            if (!changed)
                return;
            arrLocal = obs.toArray();
            clearChanged();
        }

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

 

以上是关于design_model(18)observe的主要内容,如果未能解决你的问题,请参考以下文章

design_model(19)memento

design_model(11)flyweight

design_model(16)template

design_model(24)分类与备注

更新绑定的 Observable 集合的一个元素

18观察者模式(Observer Pattern)