0

Now its hard to explain. Assuming I have a function create with a parameter x which accepts a string. Is it possible to give the create function the ability to create a new object defined by the?:

public void create(String x) {
  this.stuff = new x(); 
}
Asperger
  • 3,064
  • 8
  • 52
  • 100
  • This can be achieved with reflection, but it's not usually the correct thing to do. Why do you think you need this? – Kayaman May 12 '16 at 12:58
  • Hard to explain and my code is huge. Basically I have 4 attack buttons in a game. All 4 have labels. On click that label is passed as a string parameter in a function that calls a new attack object. – Asperger May 12 '16 at 13:02

1 Answers1

1

If you only have 4 different Strings passed in, it'll be easier to do a switch than hack around with reflection. Another option especially with more than 4 different values is to create a Map<String, YourInterface> so you can get the object with this.stuff = map.get(x);. This requires that your objects are stateless.

switch(x) {
    case "Foo":
        this.stuff = new Foo();
        break;
    case "Bar":
        this.stuff = new Bar();
        break;
    // etc.
}

Or in a more elegant way if you can just use the same reference:

Map<String, IAttack> attackMap = new HashMap<>();  // Assuming IAttack is an interface implemented by your classes
public MyClass() {
    attackMap.put("Foo", new Foo());
    attackMap.put("Bar", new Bar());
}

public void create(String x) {
  this.stuff = attackMap.get(x);
}
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Is a switch ok in an OOP context? – Asperger May 12 '16 at 14:13
  • Well it's a lot more OK than the solution you were looking for. It's not very pretty, and the map approach is a bit nicer (especially if you need to add more options), but if you need a *new* object each time and you aren't looking to have more than 4 different ones, it's acceptable. Other possibilities could be available depending on the rest of your code, but this is one of the quicker solutions. – Kayaman May 12 '16 at 19:03
  • Right now im using the strategy pattern which kind of was a bad approach on my part. However, if I have a class named Attack with concrete attack methods then I can use Java reflection to dynamically select an attackmethod via incoming strings as an argument. No need to create new object every time. You thank thats even better? – Asperger May 12 '16 at 21:29
  • If you want to avoid bad approaches, just forget reflection. It's not to be used as a common tool, but it's so seductive to less experienced programmers. – Kayaman May 13 '16 at 06:01
  • Using reflection to dynamically select a method is even more horrible, from a design point of view. Also, there's nothing wrong with creating objects. The JVM can create millions per second, and get rid of them just as fast. There's no need to optimize for nothing. But if your classes are stateless, go with the map approach. I'll edit it in the answer. – Kayaman May 13 '16 at 06:07
  • I used the map approach and yes they are stateless. I really wanted to show you the full approach but there is so much code that im not sure how to isolate the important stuff for this question. I implemented the map approach and you are right, it is pretty elegant. – Asperger May 13 '16 at 07:59
  • I could even do this with 1 put function if I could get a list of all classes that implement my interface. Then I do a simple loop and things would get more dynamic. Not sure if thats easily possible though – Asperger May 13 '16 at 08:00