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
- columnNames
- data
Now, I have six tables, each has diff. column names 开发者_如何学Gobut should have all the instance methods of the CommonTableModel class. So to pass an instance of the CommonTableModel to a JTable instance, I should first initialize both of the instance variables (columnNames and data).
Q1. Should I make six TableModels, each corresponding to each table and then extends them to the 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.
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.
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.
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. You can only initialize them directly after declaration. E.g.
String[] strings = {"foo", "bar"};
Thus, you should replace the particular lines by (assuming those are already protected
fields of CommonTableModel
):
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. The advantage is that this improves the degree of encapsulation, i.e. you don't need to declare the fields protected
, but they can now be declared 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). You should only NOT make the properties static
!! It would affect every instance of the same class. You really don't want to have that. Also see my comment here below.
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"}});
}
}
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. Use the same class for all of your tables, but different data.
精彩评论