gpt4 book ai didi

design-patterns - 类之间切换的设计模式

转载 作者:行者123 更新时间:2023-12-04 07:11:02 24 4
gpt4 key购买 nike

假设我有这 4 个类:

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

我想在数据网格中显示类的属性。像这样的东西:
+----+------+---+---+-------+-----+
| id | type | i | b | e | f |
+----+------+---+---+-------+-----+
| 1 | A | 1 | | | |
| 2 | C | 3 | | Enum1 | |
| 3 | A | 4 | | | |
| 4 | D | 1 | | | 2.5 |
+----+------+---+---+-------+-----+

“类型”列的单元格必须是下拉菜单,因此可以将类型切换为另一种类型。

解决这个问题的好的设计模式是什么?

当然我有一些想法(但我不知道哪个最好):
从父类(super class) S 导出 A、B、C 和 D?在 S 中使用 TypeA、TypeB、TypeC、TypeD 创建一个枚举“Type”,这样我就可以将它用于列“type”?

我可以做一个组合,而不是从父类(super class)派生:
类 E 包含一个接口(interface) I。A、B、C 和 D 实现 I。E 再次具有枚举“类型”,所以当我切换枚举值时,接口(interface) I 成为另一个类的对象。
或者我应该跳过这个枚举,让列“类型”代表直接附加到接口(interface)的类吗?

有任何想法吗?

最佳答案

这里可以应用多态性。我们可以考虑两种继承层次——一种用于类类型信息,另一种用于实际类。具有类型信息的类可以实现为单例,因为它对于所有实例都是相同的。

当数据显示在网格中时,这种设计可以无缝地处理所有类。

为简单起见,示例代码仅显示了一个 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);
}
}
}
}

}

关于design-patterns - 类之间切换的设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22716951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com