码迷,mamicode.com
首页 > 其他好文 > 详细

Observer Observable

时间:2015-10-16 10:15:30      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:


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();
        }
    }
}


运行结果:

技术分享

/********************     测试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);
		}
	}
}
运行结果:


技术分享


/****************************************     补充                 *****************************************/

技术分享




Observer Observable

标签:

原文地址:http://my.oschina.net/u/1989867/blog/517871

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!