Design pattern for switching between classes

Let's say I have these 4 classes:

class A {
  int i; 
}
class B { 
  int i; 
  bool b;
}
class C { 
  int i;
  enum e; 
}
class D { 
  int i;
  float f;
}

I want to show class properties in a datagrid. Something like that:

+----+------+---+---+-------+-----+
| id | type | i | b |   e   |  f  |
+----+------+---+---+-------+-----+
|  1 |   A  | 1 |   |       |     |
|  2 |   C  | 3 |   | Enum1 |     |
|  3 |   A  | 4 |   |       |     |
|  4 |   D  | 1 |   |       | 2.5 |
+----+------+---+---+-------+-----+

The cells of the "type" column must be a drop-down menu, so the type can be switched to another type.

What is a good design pattern to solve this problem?

Of course, I had some ideas (but I don’t know which is better): Derive A, B, C and D from superclass S? Create an enum 'Type' with TypeA, TypeB, TypeC, TypeD in S, so can I use this for a column type?

, , : E I. A, B, C D I. E "", , enum, . "type" , ?

?

+4
1

. - , . , , singleton, .

, .

B.

interface ClassInfo {
    String getClassType();
    ArrayList<String> getPropertyNames();
    boolean hasProperty(String propertyName);
}

class BClassInfo implements ClassInfo {

    static ArrayList<String> propertyNames = new ArrayList<String>();
    private static BClassInfo instance = null;

    private BClassInfo() {
        propertyNames.add("i");
        propertyNames.add("b");
    }

    static public ClassInfo getInstance() {
        if (instance == null) {
            instance = new BClassInfo();
        }

        return instance;
    }

    @Override
    public String getClassType() {
        return "B";
    }

    @Override
    public ArrayList<String> getPropertyNames() {
        return propertyNames;
    }

    @Override
    public boolean hasProperty(String propertyName) {
        return (propertyName.equals("i") || propertyName.equals("b"));
    }
}

abstract class SuperClass {
    public ClassInfo classInfo;

    abstract public String getValue(String propertyName);
}

class B extends SuperClass {
    int i;
    boolean b;

    public B() {
        classInfo = BClassInfo.getInstance();
    }

    @Override
    public String getValue(String propertyName) {
        if (propertyName.equals("i") == true) {
            return Integer.toString(i);
        } else if (propertyName.equals("b") == true) {
            return Boolean.toString(b);
        }

        return "";
    }
}

public class Test {

    public static void main(String[] args) {
        B bClass = new B();
        // instances of other classes are created.

        ArrayList<String> bClassProperties = bClass.classInfo.getPropertyNames();
        // collect properties of other classes.

        HashSet<String> propertySet = new HashSet<String>();
        // Now put all the properties in a set so that duplicates are removed.

        ArrayList<SuperClass> data = new ArrayList<SuperClass>(); // data to display in the grid.

        // Display the column headers (property set values from propertySet)

        for (SuperClass superClass : data) {
            // display type of class from superClass.classInfo.getClassType()
            for (String propertyName : propertySet) {
                if (superClass.classInfo.hasProperty(propertyName) == true) {
                    // display value of property from superClass.getValue(propertyName);
                }
            }
        }
    }

}
+1

Source: https://habr.com/ru/post/1533938/


All Articles