简体   繁体   English

从对象实例中实例化对象

[英]Instantiate objects from instance of object

I am creating a game where characters can have modules attached to them which provide extra functionality, for example there could be a 'banker' module which listens for a click event and opens a menu for banking. 我正在创建一个游戏,其中角色可以附加模块,提供额外的功能,例如,可以有一个“银行家”模块,用于侦听点击事件并打开银行菜单。 Modules are user creatable by extending the Module class and registering an instance with a ModuleFactory . 通过扩展Module类并使用ModuleFactory注册实例,模块是用户可创建的。 Modules are able to serialize themselves and load themselves from a serialized form (saving to XML) when is passed in the constructor. 模块能够序列化自身并在构造函数中传递时从序列化形式(保存为XML)加载自身。

The problem I am having is that when loading the modules I have the name and an instance of every module but I cannot make a new instance of each module. 我遇到的问题是,在加载模块时,我有每个模块的名称和实例,但我不能为每个模块创建一个新实例。

Is it acceptable to make a newInstance() method inside of each module which returns an instance of the module? 在每个返回模块实例的模块中创建一个newInstance()方法是否可以接受? I know it is possible to use reflection for this but most of the time I find reflection to be more trouble than the benefits I get from it. 我知道可以使用反射,但大多数时候我发现反射比我从它获得的好处更麻烦。

It is possible to do something like this, since you said you already know the names of each Module (hopefully in some sort of list). 可以做这样的事情,因为你说你已经知道每个Module的名称(希望在某种列表中)。

Class<?> temp = Class.forName("ModuleName");
Object obj = temp.newInstance();

This is actually reflection. 这实际上就是反思。


Originally I had this, but my above code is superior, because this will require you to have a method that creates a new instances inside each Module . 最初我有这个,但我的上面的代码是优越的,因为这将要求你有一个方法,在每个Module内创建一个新的实例。 It works, but it is messy. 它有效,但它很混乱。 Think of it as this, an object that creates a clone of itself, that is just weird. 可以把它想象成一个创造自身克隆的对象,这很奇怪。

public static Module createInstance()
{
    return new ThisModule();
}

If you want a new instance as a copy of the existing instance, you can use the clone method, Otherwise create a factory method which creates instances for you, 如果要将新实例作为现有实例的副本,可以使用clone方法,否则创建一个为您创建实例的工厂方法,

public static Module createInstance(){
   return new Module();
}

I m not sure if I completely understood what you want 我不确定我是否完全理解你想要的东西

Create a static method in each module to instantiate. 在每个模块中创建一个静态方法来实例化。 This is static factory method. 这是静态工厂方法。 Effective java book says it is indeed good practice to create objects through static factory methods. 有效的java书说,通过静态工厂方法创建对象确实是一种好习惯。

I think we can call static methods on objects( though not a good practice). 我想我们可以在对象上调用静态方法(虽然不是一个好习惯)。

If i understand you right, you want to extend the behavior of an object and be able to send/serialize it via XML to a client and back (frontend <-> backend communication). 如果我理解正确,您希望扩展对象的行为,并能够通过XML将其发送/序列化到客户端并返回(前端< - >后端通信)。

I think what you are looking for is something like a decoration for your Modules and Submodules. 我认为您正在寻找的是类似于模块和子模块的装饰。 Maybe you should build them decoratable to each other, like an InputStream. 也许你应该将它们构建为彼此可装饰,就像InputStream一样。

something like this: 这样的事情:

MyBaseModule base = new MyBaseModule();
BankerModule banker = new BankerModule(base);
ExtendedBankerModuler extBanker = new ExtendedBankerModule(banker);

Maybe call extBanker.toXML() to get the XML to send it to the frontend. 也许调用extBanker.toXML()来获取XML将其发送到前端。 You can wrap each module with the tags of the decorations ones... 你可以用装饰品的标签包装每个模块......

<serialized>
    <module>
        <type>ExtendedBankerModule</type>
        <description>extended banker module</description>
        <decorates>
            <module>
                <type>BankerModule</type>
                <description>banker module</description>
                <decorates>
                    <module>
                        <type>MyBaseModule</type>
                        <description>basic module</description>
                        <decorates></decorates>
                    </module>
                </decorates>
            </module>
        <decorates>
    </module>
</serialized>

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

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