Java观察者/可观察模式不通知

Java观察者/可观察模式不通知,java,model-view-controller,swt,observer-pattern,Java,Model View Controller,Swt,Observer Pattern,我正在尝试使用MVC模式用SWT构建一个简单的Java应用程序。我希望能够在后台发生某些事情时自动更新视图,因此我尝试使用Observer/Observable模式,但看起来我的Observable更改时从未通知我的Observable 代码如下: Launcher.java(主类) Application.java(后台应用程序) Penguin.java(模型,我的可观察对象) PenguinView.java(SWT视图,我的观察者) Controller.java(控制器) 输出: I

我正在尝试使用MVC模式用SWT构建一个简单的Java应用程序。我希望能够在后台发生某些事情时自动更新视图,因此我尝试使用Observer/Observable模式,但看起来我的Observable更改时从未通知我的Observable

代码如下:

Launcher.java(主类)

Application.java(后台应用程序)

Penguin.java(模型,我的可观察对象)

PenguinView.java(SWT视图,我的观察者)

Controller.java(控制器)

输出

I now have 1 rocks
I now have 2 rocks
My new color is Blue
I now have 3 rocks
I now have 4 rocks
你知道我做错了什么吗


谢谢

据我所知,企鹅视图构造函数中的while循环阻塞了您的主线程,因此控制器永远不会被实例化,观测者也永远不会被添加。

在什么情况下,您的视图在企鹅实例中注册为观测者?我似乎找不到LOC(可能我在某处遗漏了它…)你的方法notifyObservators()在哪里?@Brian Driscoll:在控制器的构造函数中:
\u app.getPenguin().addObservator(\u v)
@talnicolas:在企鹅的
iFoundRock()方法中class@talnicolas我猜它是在基类中实现的。在SWT中不确定是否需要重写。是的,我已经解决了。但是SWT还有另一个问题:只要我实例化我的视图,主线程就会卡在循环中,我不能实例化我的控制器(我不能在视图之前实例化它,因为它需要对视图实例的引用)。嗨,我有一个类似的问题。你找到解决方案了吗?不要在构造函数中放入无限循环,将它们放入从main()调用的另一个方法中。如果这不起作用,请将代码放入新问题中。
public class Application {
    private Penguin _myPenguin;

    public Application() {
        _myPenguin = new Penguin();
        new Thread(_myPenguin).start();
    }

    public Penguin getPenguin() {
        return _myPenguin;
    }
}
public class Penguin extends Observable implements Runnable {
    private String _color;
    private boolean _isHappy;
    private int _myRocks;

    public Penguin() {
        _color = "Black";
        _isHappy = true;
        _myRocks = 0;
    }

    public void paint(String color) {
        _color = color;
        System.out.println("My new color is " + _color);
        setChanged();
        notifyObservers();
    }

    public String getColor() {
        return _color;
    }

    public void upset() {
        _isHappy = false;
        setChanged();
        notifyObservers();
    }

    public void cheerUp() {
        _isHappy = true;
        setChanged();
        notifyObservers();
    }

    public boolean isHappy() {
        return _isHappy;
    }

    public void run() {
        // Penguin starts walking and find rocks!
        while(true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            iFoundRock();
        }
    }

    private void iFoundRock() {
        _myRocks++;
        System.out.println("I now have " + _myRocks + " rocks");
        setChanged();
        notifyObservers();
    }

}
public class PenguinView implements Observer {
    private Application _app;

    private Display _d;
    private Shell _s;

    private Label _penguinColor;
    private Label _penguinHumor;
    private Label _penguinRocks;
    private Button _upset;
    private Button _cheerUp;
    private Text _newColor;
    private Button _paint;


    public PenguinView(Application app) {
        _app = app;
        _d = new Display();
        _s = new Shell();

        RowLayout rl = new RowLayout();
        rl.marginWidth = 12;
        rl.marginHeight = 12;
        _s.setLayout(rl);

        new Label(_s, SWT.BORDER).setText("Penguin Color: ");
        _penguinColor = new Label(_s, SWT.BORDER);
        _penguinColor.setText(_app.getPenguin().getColor());

        new Label(_s, SWT.BORDER).setText(" Humor: ");
        _penguinHumor = new Label(_s, SWT.BORDER);
        String humor = _app.getPenguin().isHappy() ? "Happy" : "Sad";
        _penguinHumor.setText(humor);

        new Label(_s, SWT.BORDER).setText(" Rocks: ");
        _penguinRocks = new Label(_s, SWT.BORDER);
        _penguinRocks.setText(String.valueOf(_app.getPenguin().howManyRocks()));

        _upset = new Button(_s, SWT.PUSH);
        _upset.setText(":(");
        _upset.addListener(SWT.Selection, new Listener(){
            public void handleEvent(Event e) {
                _penguinHumor.setText("Sad");
            }
        });

        _cheerUp = new Button(_s, SWT.PUSH);
        _cheerUp.setText(":)");
        _cheerUp.addListener(SWT.Selection, new Listener(){
            public void handleEvent(Event e) {
                _penguinHumor.setText("Happy");
            }
        });

        _newColor = new Text(_s, SWT.BORDER);

        _paint = new Button(_s, SWT.PUSH);
        _paint.setText("Paint!");
        _paint.addListener(SWT.Selection, new Listener(){
            public void handleEvent(Event e) {
                //_penguinColor.setText(_newColor.getText());
                _app.getPenguin().paint(_newColor.getText());
            }
        });

        _s.pack();
        _s.open();
        while (!_d.isDisposed()) {
            if (!_d.readAndDispatch()) {
                _d.sleep();
            }
        }
    }

    public void update(Observable obs, Object obj) {
        System.out.println("I go here!");
        _penguinRocks.setText(String.valueOf(((Penguin)obs).howManyRocks()));
        if(obs.equals(_app.getPenguin())) {
            _penguinRocks.setText(String.valueOf(((Penguin)obs).howManyRocks()));
        }
    }

    public void update(Observable obs) {
        System.out.println("I go there!");
    }
public class Controller {
    Application _app;
    PenguinView _v;

    public Controller(Application app, PenguinView v) {
        _app = app;
        _v = v;

        // Set observer
        _app.getPenguin().addObserver(_v);
    }

}
I now have 1 rocks
I now have 2 rocks
My new color is Blue
I now have 3 rocks
I now have 4 rocks