1

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.

2
  • "... I have the name and an instance of every module but I cannot make a new instance of each module." Can you explain that a bit more? Commented Jan 5, 2015 at 9:10
  • @Stefan I have an instance of each module stored in a map with the modules name. As each character needs a separate instance of each module I will need to create more instances of it. Commented Jan 5, 2015 at 9:40

4 Answers 4

3

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();
}
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.