简体   繁体   English

对于这个问题,哪个是Java中最好的设计模式?

[英]Which would be the best design pattern in Java for this problem?

I have a class CommonTableModel that has several instance methods and each operate on the two instance variables 我有一个CommonTableModel类,它有几个实例方法,每个方法都对两个实例变量进行操作

  • columnNames COLUMNNAMES
  • data 数据

Now, I have six tables, each has diff. 现在,我有六个表,每个表都有差异。 column names but should have all the instance methods of the CommonTableModel class. 列名,但应具有CommonTableModel类的所有实例方法。 So to pass an instance of the CommonTableModel to a JTable instance, I should first initialize both of the instance variables (columnNames and data). 因此,要将CommonTableModel的实例传递给JTable实例,我应该首先初始化两个实例变量(columnNames和data)。

Q1. Q1。 Should I make six TableModels, each corresponding to each table and then extends them to the CommonTableModel? 我应该制作六个TableModel,每个TableModel对应于每个表,然后将它们扩展到CommonTableModel吗?

public class FirstTableModel extends CommonTableModel {

    public FirstTableModel() {
        columnNames = {"id", "name"};
        data = {{1, "John"}};
    }
}

In the above example, I tried to initialize inherited data members so that each of the six TableModel can populate columnNames according to the table that they denote. 在上面的示例中,我尝试初始化继承的数据成员,以便六个TableModel中的每一个都可以根据它们表示的表填充columnNames。

But I got an error which is restricting me to initialize the inherited members in this way. 但我收到一个错误,限制我以这种方式初始化继承的成员。 I think that we can't initialize instance variables in this way. 我认为我们不能以这种方式初始化实例变量。

Then how can I populate the instace variables of CommonTableModel so that the instance methods of the CommonTableModel process the data that I populate them later. 然后,我如何填充CommonTableModel的instace变量,以便CommonTableModel的实例方法处理我稍后填充它们的数据。

One of the solution is to pass the data in the constructor of CommonTableModel but in that way, I will have to pass the whole columnNames each time when I make a Table. 其中一个解决方案是在CommonTableModel的构造函数中传递数据,但是这样,每次制作表时我都必须传递整个columnNames。

I am very confused as I don't have much experience in programming and don't know good coding practices. 我很困惑,因为我没有太多的编程经验,也不知道好的编码实践。

Please, also refer some good design pattern books so that I can have a better understanding of design patterns. 请参考一些优秀的设计模式书籍,以便我能更好地理解设计模式。

But I got an error which is restricting me to initialize the inherited members in this way. 但我收到一个错误,限制我以这种方式初始化继承的成员。 I think that we can't initialize instance variables in this way. 我认为我们不能以这种方式初始化实例变量。

Arrays which aren't initialized with new are array constants . 未使用new初始化的数组数组常量 You can only initialize them directly after declaration. 您只能在声明后直接初始化它们。 Eg 例如

String[] strings = {"foo", "bar"};

Thus, you should replace the particular lines by (assuming those are already protected fields of CommonTableModel ): 因此,您应该替换特定的行(假设它们已经是CommonTableModel protected字段):

columnNames = new String[] {"id", "name"};
data = new Object[][] {{1, "John"}};

Edit as per the comments: you can of course also define a constructor for that and make use of the super() call. 根据注释进行编辑 :您当然也可以为其定义构造函数并使用super()调用。 The advantage is that this improves the degree of encapsulation, ie you don't need to declare the fields protected , but they can now be declared private . 优点是,这提高了封装程度,即您不需要声明protected的字段,但现在可以将它们声明为private Here's a kickoff example: 这是一个启动示例:

public abstract class CommonTableModel {
    private String[] columnNames;
    private Object[][] data;

    protected CommonTableModel(String[] columnNames, Object[][] data) {
        this.columnNames = columnNames;
        this.data = data;
    }
}

.

public class FirstTableModel extends CommonTableModel {
    public FirstTableModel() {
        super(new String[] {"id", "name"}, new Object[][] {{1, "John"}});
    }
}

Note that you still need the new keyword to instantiate them (rsp was wrong here in his answer). 请注意,您仍然需要new关键字来实例化它们(在他的回答中rsp错误)。 You should only NOT make the properties static !! 您只应不会使性能static It would affect every instance of the same class. 它会影响同一个类的每个实例 You really don't want to have that. 你真的不想拥有它。 Also see my comment here below. 另见下面的评论。

If the only differences between your table models are the column names I would just pass them to the constructor of your CommonTableModel as an array of strings. 如果表模型之间的唯一区别是列名,我只是将它们作为字符串数组传递给CommonTableModel的构造函数。 Use the same class for all of your tables, but different data. 对所有表使用相同的类,但使用不同的数据。

Your table models extend the common table model, the common table model can initialise the columns and data in its constructor, I think you are looking for a pattern like this: 您的表模型扩展了公共表模型,公共表模型可以在其构造函数中初始化列和数据,我认为您正在寻找这样的模式:

public class CommonTableModel {

    protected CommonTableModel (String[] n, Object[] d) {
        columnNames = n;
        data = d;
    }
}

public class FirstTableModel extends CommonTableModel {

    public FirstTableModel() {

        super(new String[] {"id", "name"}, new Object[][] {{1, "John"}});
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM