使用超类静态方法获取子类的实例 [英] Get instance of subclass with superclass static method

查看:33
本文介绍了使用超类静态方法获取子类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超类,我想将一个名为 getInstance() 的静态方法转发给所有子类.

I have a superclass that I would like to forward a static method called getInstance() to all subclasses.

在创建子类的实例时,我然后在超类中注册该实例(可能使用哈希表,其中的键基于 getClass()).然后,我希望使用前面提到的静态方法( getInstance ),其中超类方法将返回正确类型的实例.

When creating an instance of a subclass, I then register the instance in the superclass (perhaps using a hashtable, where the key is based on getClass()). Then, I wish to use the aforementioned static method ( getInstance ) where the superclass method will return the instance of the correct type.

例如,我有一个超类 A,一个子类 B 扩展了 A.我想写一个静态方法 A.getInstance();当从 B (B.getInstance()) 调用时,我希望它返回我之前存储的 B 实例.

For example, I have a superclass A, and a subclass B extends A. I want to write a static method A.getInstance(); when called from B (B.getInstance()), I would like it to return the instance of B that I stored earlier.

这有点难以解释,但我将大量使用这个超类,我宁愿不将 getInstance 方法编码到每个子类中.

Its kinda hard to explain, but I am going to be using this superclass a lot, and I would rather not code a getInstance method into every single subclass.

我将如何去做这样的事情?

How would I go about doing something like this?

我刚刚意识到我的问题可能会被误解为创建对象的新实例.我已经创建了实例,我希望获取该类的现有实例

edit: I just realized that my question may be misconstrued as creating a NEW instance of the object. I have already created the instance, and i wish to get the existing instance of the class

推荐答案

正如许多其他人在评论中指出的那样,静态方法无法实现您尝试执行的操作.此外,您应该尽可能避免使用静态方法,因为它们会导致测试和维护噩梦 (*).

As many others have noted in the comments, what you are trying to do is not possible with static methods. Also, you should try to avoid static methods whenever possible because they can result in a testing and maintanance nightmare (*).

您将方法命名为getInstance",所以我猜测您想要做的是工厂模式和单例模式的混合.以下是一些可帮助您开始了解这些模式的信息:

You named your method "getInstance", so I guess what you want to do is a mix of Factory- and Singleton patterns. Here is some information to get you started about these patterns:

单身:http://en.wikipedia.org/wiki/Singleton_pattern
工厂方法:http://en.wikipedia.org/wiki/Factory_method_pattern
抽象工厂:http://en.wikipedia.org/wiki/Abstract_factory

在这些日子里,两者都不应该手工编码 (*) - 看看一个好的依赖注入"(DI) 容器,比如 Google Guice 或 Spring.我不能 100% 确定您到底想要实现什么,但看起来 DI 容器可以为您完成.

Both should not be coded by hand in these days (*) - Have a look at a good "Dependency Injection" (DI) container like Google Guice or Spring. I am not 100% sure what exactly you want to achieve, but it looks like a DI container will do it for you.

这是对问题编辑的回应.您希望接收子类的缓存实例.在这种情况下,我仍然建议不要使用静态方法.您可以创建BCache"类的单例实例(使用 DI 容器或手动对其进行编程),然后使用此缓存对象来查找您注册的对象.使用 Guice 作为 DI 容器,它可能看起来像这样(警告,未经测试):

This is a response to an edit of the question. You want to receive a cached instance of the sub classes. In this case, I would still advise against static methods. You could create a singleton instance of a "BCache" class (using a DI container or programming it by hand), and then use this cache object to look up your registered objects. Using Guice as a DI container, it could look like this (warning, untested):

@Singleton
public class BCache {
    private Map<Class<? extends B>, B> cache = ...;

    public <T> T getInstance(Class<? extends T> type) {
        return (T) cache.get(type);
    }
}

不过,我仍然认为可以使用 DI 容器完全摆脱缓存类.同样,这是使用 Guice 的未经测试的代码,但它可能如下所示:

I still think it would be possible to get rid of the cache class completely using a DI container, though. Again, this is untested code, using Guice, but it could look like this:

@Singleton
public class A extends B {
    public A() {
        //I am not sure if you need to register in this case, because your
        //DI container keeps track of the singleton instances.
        super.register(this);
    }
}

public class SomeClassUsingA {
    @Inject private A a;
}

(*) 注意所有概括都是错误的",也就是说,在某些项目中它可能有意义,但在大多数情况下却没有.

(*) Note that "all generalizations are wrong", that is, in some projects it might make sense, but in most it will not.

这篇关于使用超类静态方法获取子类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆