简体   繁体   English

在java中避免使用instanceof运算符时观察多个observable?

[英]Observing multiple observables while avoiding instanceof operator in java?

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. 例如,我希望A能够观察B和C.B和C完全不相关,除了它们都实现了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? 显而易见的解决方案是在更新方法中使用“if instanceof”,但很快就会变得混乱,因此我想知道是否还有其他方法?

A clean solution would be to use (anonymous) inner classes in A to act as the Observer s. 一个干净的解决方案是在A使用(匿名)内部类来充当Observer 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 B s and C s you actually want to watch. 这将允许您将BObserver / CObserver实例添加到您实际想要观看的许多BC 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 . 它的A好处是A的公共界面不那么杂乱,您可以轻松添加新的内部类来处理DEF

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. 不幸的是,您需要知道Observable的具体类型。 However you could change the reflections to allow interfaces etc. 但是,您可以更改反射以允许接口等。

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. 假设对象B / C上的操作是相同的,并且您只想区分用于状态杂耍目的的两个对象,您还可以创建一个委托对象来实现实际的观察逻辑/状态并在主体中使用查找机制object用于检索特定Observable对象的权限委托。 Then forward the calls. 然后转发电话。

You can always have a Map<Class<? extends Event>, EventHandler> 你总是可以拥有Map<Class<? extends Event>, EventHandler> Map<Class<? extends Event>, EventHandler> in your listener. 在您的侦听器中Map<Class<? extends Event>, EventHandler> Similar, but no explicit 'instanceof' operator. 类似,但没有明确的'instanceof'运算符。 It gets replaced with containsKey() in a map. 它在地图中被containsKey()替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM