vendredi 28 août 2020

Java class design for redundant variable property

Consider

Class A {
    private Map<String, String> AXMap;
    private Map<String, String> A1Map;
    private Map<String, String> A2Map;
    private Map<String, String> A3Map;
}
ClassB {
    public void someMethod() {

    List<ClassA> classAList = new ArrayList...
    for i..0 to n {
        ClassA classA = new ClassA();
        populate A1Map;
        populate A2Map;
        populate A3Map;
        classAList.add(classA)
    }
}

The crude logic above is just that A1Map, A2Map, A3Map are populated for each ClassA of the loop and added to the final list. My design question is AXmap remains the same for all the iterations. Even if I populate once, the same redundant AXmap is in every object of ClassA list. I do need the AXmap along with other maps. Is there a better java design principle. All I could think was in terms of inheritance, final or Move AXMap to separate class and add as composition etc

Aucun commentaire:

Enregistrer un commentaire