private void registerAdapter(Class<?> adapterType, Object adapter) { RegisterStrategy registerStrategy = adapter.getClass().getAnnotation(RegisterStrategy.class); if ((registerStrategy != null) && (registerStrategy.value().equals(RegisterType.SINGLE))) { getGsonBuilder().registerTypeAdapter(adapterType, adapter); } else { getGsonBuilder().registerTypeHierarchyAdapter(adapterType, adapter); } }
/** * The purpose of this method is to register the resource based on their category. Because * different category has different fields. All the loops are supposed to be the same so by doing * this way we avoid duplicated code. To make this fit in the open-closed principle, we use * strategy pattern. Because every time we want to register new category or want to change the way * how we register it, we can make new classes. This Register class remains closed. Actually, all * the strategy class should be put into different folders. I didn't do that because Strategy * Pattern for this is already an overkill for this because or the register strategy class is so * simple. But this can be viewed as left for further extensibility. For example, one may come up * with new data format to register pictures. * * @param parser: the parser passed from model. * @param category: which part to initialize */ private void register(Parser parser, RegisterStrategy rs) { JSONArray items = parser.getJSONObject("resources").getJSONArray(rs.category); for (int i = 0; i < items.length(); i++) { JSONObject oneItem = items.getJSONObject(i); rs.register(oneItem); } }