观察多个可观察对象同时避免java中的instanceof运算符?

发布于 2024-10-05 22:35:40 字数 169 浏览 1 评论 0 原文

如果我有一个对象,我希望能够观察其他几个可观察对象,而不是所有对象都是同一类型。例如,我希望 A 能够观察 B 和 C。B 和 C 完全不相关,除了它们都实现了 Observable。

显而易见的解决方案就是在更新方法中使用“if instanceof”,但这很快就会变得混乱,因此我想知道是否还有其他方法?

If I have an object that I want to be able to observe several other observable objects, not all of the same type. For example I want A to be able to observe B and C. B and C are totally un-related, except for the fact that they both implement Observable.

The obvious solution is just to use "if instanceof" inside the update method but that quickly can become messy and as such I am wondering if there is another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉南桥 2024-10-12 22:35:40

一个干净的解决方案是在 A 中使用(匿名)内部类来充当观察者。例如:

class A {
    public A(B b, C c) {
        b.addObserver(new BObserver());
        c.addObserver(new CObserver());
    }

    private class BObserver implements Observer {
        // Logic for updates on B in update method
    }

    private class CObserver implements Observer {
        // Logic for updates on C in update method
    }
}

这将允许您将 BObserver/CObserver 实例添加到任意数量的 BC 中其实很想看。它还有一个额外的好处,即 A 的公共接口不太混乱,并且您可以轻松添加新的内部类来处理类 DEF

A clean solution would be to use (anonymous) inner classes in A to act as the Observers. For example:

class A {
    public A(B b, C c) {
        b.addObserver(new BObserver());
        c.addObserver(new CObserver());
    }

    private class BObserver implements Observer {
        // Logic for updates on B in update method
    }

    private class CObserver implements Observer {
        // Logic for updates on C in update method
    }
}

This will allow you to add BObserver/CObserver instances to however many Bs and Cs you actually want to watch. It has the added benefit that A's public interface is less cluttered and you can easily add new inner classes to handle classes D, E and F.

冰雪梦之恋 2024-10-12 22:35:40

与之前的建议类似,您可以更改更新。

public void update(Observable o, Object arg) {
  try{
    Method update = getClass().getMethod(o.getClass(), Object.class);
    update.invoke(this, o, arg);
  } catch(Exception e) {
    // log exception
  }
}

添加一个方法

public void update(A a, Object arg);
public void update(B b, Object arg);
public void update(C c, Object arg);

通过这种方式,您可以为您想要观察的每种类型 。不幸的是,您需要知道 Observable 的确切具体类型。但是,您可以更改反射以允许接口等。

Similar to previous suggestions you could change you update to.

public void update(Observable o, Object arg) {
  try{
    Method update = getClass().getMethod(o.getClass(), Object.class);
    update.invoke(this, o, arg);
  } catch(Exception e) {
    // log exception
  }
}

This way you can add one method

public void update(A a, Object arg);
public void update(B b, Object arg);
public void update(C c, Object arg);

for each type you want to observe. Unfortunately you need to know the exact concrete type of the Observable. However you could change the reflections to allow interfaces etc.

顾北清歌寒 2024-10-12 22:35:40

假设对象 B/C 上的操作是相同的,并且您只想区分两个对象以实现状态杂耍,您还可以创建一个委托对象来实现实际的观察逻辑/状态,并在您的 main 中使用查找机制对象来检索特定 Observable 对象的正确委托。然后转接电话。

Assuming that the operations on object B/C would be identical and you merely want to distinguish between the two objects for state juggling purposes, you could also create a delegate object which implements the actual observation logic/state and use a lookup mechanism in your main object to retrieve the right delegate for the particular Observable object. Then forward the calls.

忘东忘西忘不掉你 2024-10-12 22:35:40

你总是可以有一个 Map, EventHandler> 。类似,但没有明确的“instanceof”运算符。它在地图中被 containsKey() 替换。

You can always have a Map<Class<? extends Event>, EventHandler> in your listener. Similar, but no explicit 'instanceof' operator. It gets replaced with containsKey() in a map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文