Observer Observable


package com.util.ObservDemoTest;

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

public class ObservDemo extends Observable {
    MyView view;
    MyModel model;

    public ObservDemo() {
        view = new MyView();
        model = new MyModel();
        model.addObserver(view);
        System.out.println("1.初始化組件:\t"+"\n  Observer:"+view +"\n  Observable:"+model);
    }

    public static void main(String[] av) {
        ObservDemo me = new ObservDemo();
        me.demo();
    }

    public void demo() {
        System.out.println("2.對象發生變化。。。。");
        model.changeSomething();
    }

    /** The Observer normally maintains a view on the data */
    class MyView implements Observer {
        /** For now, we just print the fact that we got notified. */
        public void update(Observable obs, Object x) {
            System.out.print("4.");
            System.out.println("update(" + obs + "," + x + ");");
        }
    }

    /** The Observable normally maintains the data */
    class MyModel extends Observable {
        public void changeSomething() { // Notify observers of change
            System.out.println("**********************對象是否改變:  "+hasChanged()+" ****************");
            setChanged();
            System.out.println("3.對象是否改變    "+hasChanged()+"   ,調用觀察者的update方法:");
            notifyObservers();
        }
    }
}


運行結果: java

/********************     測試2          *********************/ 測試


package com.util.ObservDemoTest;

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

public class ObservDemo extends Observable {
	MyView view;
	MyModel model;

	public ObservDemo() {
		view = new MyView();
		model = new MyModel();
		model.addObserver(view);
		System.out.println("1.初始化組件:\t"+"\n  Observer:"+view +"\n  Observable:"+model);
	}

	public static void main(String[] av) {
		ObservDemo me = new ObservDemo();
		me.demo();
	}

	public void demo() {
		System.out.println("2.對象發生變化。。。。");
		model.changeSomething();
	}

	/** The Observer normally maintains a view on the data */
	class MyView implements Observer {
		/** For now, we just print the fact that we got notified. */
		public void update(Observable obs, Object x) {
			System.out.print("4.");
			System.out.println("update(\n  " + obs + ",\n  " + x + "\n  );");
		}
	}

	/** The Observable normally maintains the data */
	class MyModel extends Observable {
		public void changeSomething() { // Notify observers of change
			System.out.println("**********************對象是否改變:  "+hasChanged()+" ****************");
			setChanged();
			System.out.println("3.對象是否改變    "+hasChanged()+"   ,調用觀察者的update方法:");
			model = new MyModel();
			notifyObservers(model);
		}
	}
}
運行結果:



/****************************************     補充                 *****************************************/ spa



小結: code

          Observable,Observer(接口)
          被觀察者:setChanged()                  notifyObservers()
          觀察者:  update(Observable o, Object arg) orm

相關文章
相關標籤/搜索