可观察模式实现不调用更新方法

Observable Pattern Implementation not calling update method

我正在努力在为作业提供的代码上实施 Observable 设计模式。我不想要完整的答案,但我确实想了解缺少的内容。我不会复制所有的代码,但是到目前为止我已经写了一些相关的代码。该代码是一个简单的银行 GUI,允许我们将储蓄和支票账户保存到 ArrayList。我的第一步是将其拉出到一个新对象 AccountList 中并扩展 Observable:

public class AccountList extends Observable {
public List<AAccount> accountList;

public AccountList(List<AAccount> accountList) {
    this.accountList = accountList;
}

public void add(AAccount acc) {
    accountList.add(acc);
    hasChanged();
    notifyObservers();
}
}

我的下一步是实现观察者 class 这样的:

public class AccountListObserver implements Observer{

public AccountListObserver(Observable o) {
    o.addObserver(this);
}

@Override
public void update(Observable o, Object arg) {
    System.out.println("Account " + o + " has been added, " +
            "there are now " + o + " accounts on the server.");

}
}

现在在实际服务器中 class 我在开头调用了以下内容:

AccountList accList = new AccountList(new ArrayList<AAccount>());
Observer accObserver = new AccountListObserver(accList);

然后每当添加帐户时,我都会调用 accList.add(acc)

我没有得到我的输出,但程序运行正常。我缺少什么来实现这个功能。一旦我了解缺少或需要什么 moved/changed,我就可以对所有功能进行必要的更改。我只是对可能缺少的内容感到非常困惑,因为它看起来像是一个合乎逻辑的配置。

将对 hasChanged() 的调用替换为 setChanged()。这会将更改的标志设置为 true 并允许 notifyObservers() 方法通知观察者。