简体   繁体   English

所有已实现类中的启动方法

[英]Starting method in all implemented classes

I'm kind of new to Java and have a rather simple question:我是 Java 新手,有一个相当简单的问题:

I have an interface, with a method:我有一个接口,有一个方法:

public interface Interface_Updatable {

    public void updateViewModel();

}

I implement this interface in several classes.我在几个类中实现了这个接口。 Each class then of course has that method updateViewModel .每个类当然都有那个方法updateViewModel

Edit: I instantiate these classes in a main function.编辑:我在主函数中实例化这些类。 Here I need code that calls updateViewModel for all objects that implement the interface.在这里,我需要为实现该接口的所有对象调用updateViewModel代码。

Is there an easy way to do it combined?有没有一种简单的方法可以将它结合起来? I don't want to call every method from every object instance separately and keep that updated.我不想分别从每个对象实例调用每个方法并保持更新。 Keeping it updated might lead to errors in the long run.从长远来看,保持更新可能会导致错误。

The short form is: no , there's no simple way to "call this method on all instances of classes that implement this interface".简短的形式是:,没有简单的方法可以“在实现此接口的类的所有实例上调用此方法”。

At least not in a way that's sane and maintainable.至少不是以一种理智和可维护的方式。

So what should you do instead?那么你应该怎么做呢?

In reality you almost never want to just "call it on all instances", but you have some kind of relation between the thing that should trigger the update and the instances for which it should be triggered.实际上,您几乎从不想只是“在所有实例上调用它”,但是在应该触发更新的事物和应该为其触发更新的实例之间存在某种关系

For example, the naming of the method suggests that instances of Interface_Updatable are related to the view model.例如,该方法的命名表明Interface_Updatable实例与视图模型相关。 So if they "care" about changes to the view model, they could register themselves as interested parties by doing something like theViewModel.registerForUpdates(this) , the view model could hold on to a list of all objects that registered like this and then loop over all the instances and calls updateViewModel on each one (of course one would need to make sure that unregistration also happens, where appropriate).因此,如果他们“关心”视图模型的更改,他们可以通过执行诸如theViewModel.registerForUpdates(this)类的theViewModel.registerForUpdates(this)将自己注册为相关方,视图模型可以保留所有像这样注册的对象的列表,然后循环在所有实例上调用updateViewModel (当然,需要确保在适当的情况下也会发生注销)。

This is the classical listener pattern at work.这是工作中的经典侦听器模式。

But the high-level answer is: you almost never want to call something on "all instances", instead the instances you want to call it on have some relation to each other and you would need to make that relation explicit (via some registration mechanism like the one described above).但高级答案是:你几乎从不想在“所有实例”上调用某些东西,相反,你想调用它的实例彼此之间某种关系,你需要使这种关系明确(通过某种注册机制就像上面描述的那样)。

There is no easy way to call this method on all classes that implement this interface.没有简单的方法可以在实现此接口的所有类上调用此方法。 The problem is that you need to somehow keep track of all the classes that implement this interface.问题是您需要以某种方式跟踪实现此接口的所有类。

A possible object-oriented way to do this would be passing a list containing objects that are instances of classes that implement the Interface_Updateable interface to a function, and then calling updateViewModel on each object in that list:一种可能的面向对象的方法是将包含对象的列表传递给函数,这些对象是实现Interface_Updateable接口的类的实例,然后在该列表中的每个对象上调用updateViewModel

public void updateViewModels(List<Interface_Updateable> instances) {
    for(var instance : instances) {
        instance.updateViewModel();
    }
}

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

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