I have a java.util.Map<String, Object>
object which different types of values in it. I don't want to cast whereever I do a get operation over this. To do this, I created different classes wrapping this map and these classes provide different get methods which return values for different keys.
There are two options for this:
public class Wrapper1 { private final Map<String, Object> map; public Wrapper1(Map<String, Object> map) { this.map=map; } public MyObject getMyObject() { return (MyObject)this.map.get("someKey"); } }
public class Wrapper2 { private Wrapper2() { } public static MyObject getMyObject(Map<String, Object> map) { return (MyObject)map.get("someKey"); } }
As you see, in first one I'm creating an instance of Wrapper1 and access MyObject instance by instance method. In second, I give the map to a static method and access same MyObject instance.
Which one will you prefer more and why? Also if this is a bad practice, please let me know. Thank you.