Wrapping a map with instance or static method

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.

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.

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.

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật