0

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.

1

This is still bad practice as you still do castings in there.

If you want to store different types in the map then the chance is high that all of these objects share a common behaviour. In this case all of these should implement the interface that defines this behaviour so that you can store them typesafe and without casting in the same map.

If they dont share the same behaviour then why store all in the same map? They will have different meanings and therefore should be saved in different maps or you create a class that has different maps of these types as members.

The last case is that you are given the array and you can't change the way the objects are saved. In this case I would actually prefer a combination of both your options. Put the static method into the class above. In this way you can commonly use the static function for getting your object without first creating an instance. If you know that you will need to call this method often, you can create an object passing the map and you will not have to always pass the map each time you want to get your object. This is related to using the fabric method pattern or currying / partial application used in functional programming (in which case the static method alone would be sufficient).

Note however that it would be good to somehow check if the object you get from your map satisfies your expectations (if possible) and otherwise throw an exception.

0

Personally I would prefer the first option, mainly because it would give you the option to override the wrapper's methods or add additional "getMyObject" methods with an inherited class if need be. And strictly speaking, the second option would not really be a "wrapper". It's just a class with static utility methods.

But whichever option you decide to go with, definitely do type checking in the "getMyObject" methods before casting.

0

It really depends if you plan to have object types MyObject, MyObject2,... MyObjectN stored in a single map instance or multiple maps that store MyObject.

In the former case with multiple object types, it would make sense to choose Wrapper1 because you can hold the map in the Wrapper1, and you do not need parameters to the method retrieving it. In the latter case with multiple map instances, Wrapper2 makes sense; otherwise you would also need multiple instance of Wrapper1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.