如何为每个列表重构具有多个Lists + getter和setter的类

时间:2015-07-10 09:16:35

标签: java refactoring

我有以下课程:

.jqGrid({
            datatype : "local",
            data : JSONData,
            height : ($(window).height() - 250),
            width : ($(window).width() - 32),
            scroll : true, 
            rowNum:20, // the number of row's to be displayed the first time
...
..
});

我尝试使用访问者模式重构它但由于泛型而无法使其工作..有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

在@ user902383上使用Map,这是Java 7中的解决方案:

public class RefactorMe {
  class Event<K> {
    public K getNewObject() {
      return null;
    }
  }

  private static Map<Class<?>, List<Event<?>>> eventLists = new HashMap<>();

  public static <E> List<Event<E>> getEventList(Class<E> clazz) {
    return (List) eventLists.get(clazz);
  }

  public static <E extends Event<E>> void addEvent(Event<E> pEvent) {
    Class<E> key = (Class<E>) pEvent.getNewObject().getClass();
    List<Event<?>> events = eventLists.get(key);
    if (events == null) {
      events = new ArrayList<>();
      eventLists.put(key, events);
    }
    events.add(pEvent);
  }
}