user2248702
user2248702

Reputation: 3018

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. Modules are able to serialize themselves and load themselves from a serialized form (saving to XML) when is passed in the constructor.

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? 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.

Upvotes: 1

Views: 112

Answers (4)

Niels
Niels

Reputation: 312

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).

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.

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. 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>

Upvotes: 0

Pandiri
Pandiri

Reputation: 329

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.

I think we can call static methods on objects( though not a good practice).

Upvotes: 0

shikjohari
shikjohari

Reputation: 2288

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,

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

I m not sure if I completely understood what you want

Upvotes: 0

axwcode
axwcode

Reputation: 7824

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).

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. 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();
}

Upvotes: 3

Related Questions