0

I am trying to figure out if there is a way to instantiate a new object dynamically based on user input in java.

E.g.

I have 3 entities/objects

public class Item {}

public class Room {}

public class Location {}

In a form the user would fill out a input field called "id='objecttype'" and it would be sent to a controler. The controller would dynamically return a object based on the String value.

e.g.

If user types in Item, it will generate a Item object and return it as such. Same goes if the user types in Room, it will instantiate a Room object and return new Room() object, and finally same for Location where user types in Location and java returns a new Location() object.

E.G. of controller

@RequestMapping(value = "/retVal", method = /*POST Type*/) 
public Object retVal(@RequestParameter("objecttype") String type) {
// here the logic would determine which object would get returned
// if type is Item
//    return new Item();
// if type is Room
//    return new Room();
// if type is Location
//    return new Location();

   return null;
}
Aeseir
  • 7,754
  • 10
  • 58
  • 107
  • 1
    Java strings are under double quotes, and one compares them using `String.equals`. However, the best way to do it here is using a `switch` or a map + factories if the number of different types is likely to increase in the future – Dici Jun 29 '15 at 01:19
  • yea fully aware of that, it was merely to demonstrate the logic. Doesn't make sense why this got down vote at all as it is legitimate question. Switch is code 101 but not preferred since this is but a representation of start point of my code base. It is substantially larger and if i had to do a switch and corresponding logic, it would be pages and pages long. Reason for object use is that i can use common logic in one place instead of duplication. – Aeseir Jun 29 '15 at 01:26
  • @Aeseir I did not get why you don't want a switch – Dici Jun 29 '15 at 01:28
  • @Makoto wrong focus, that was a quick e.g. i made not representative of code. – Aeseir Jun 29 '15 at 01:28
  • @Dici i have over 30 objects the admin can choose from. They currently use switch and its rather messy. I am investigating options other than Switch such as createInstance to reduce the amount of code and standardise it. If switch is the only way then fair enough, but i wanted to see if there are other better ways of doing this. – Aeseir Jun 29 '15 at 01:30
  • So you want to use a `String` to instantiate a type, right? – Makoto Jun 29 '15 at 01:30
  • Yes if they type in Item they get a Item object back, Room they get Room object back etc. – Aeseir Jun 29 '15 at 01:31
  • Thanks @Makoto, that is what I was after. – Aeseir Jun 29 '15 at 01:33
  • @Aeseir I don't think you have many options here. You can use a switch or a `Map>`, that's all I can see – Dici Jun 29 '15 at 01:34
  • Not many options but Makoto pointed to the one i saw long time ago but couldn't find this round. Thanks for the assistance guys. – Aeseir Jun 29 '15 at 01:35
  • Works if none of your classes need any argument to be constructed, or if they all have the same signature but if this is the case, yes, it will shrink your code down – Dici Jun 29 '15 at 01:36
  • Yep that will do nicely then i can manipulate accordingly. The system only needs blanks to work with, then external logic will process accordingly. – Aeseir Jun 29 '15 at 01:37

0 Answers0