What would be the best Java design template for this problem?

I have a CommonTableModel class that has several instance methods, and each of them works with two instance variables

  • columnNames
  • data

Now I have six tables, each of which has diff. column names, but must have all the instance methods of the CommonTableModel class. Therefore, in order to pass an instance of CommonTableModel to a JTable instance, I must first initialize both instance variables (columnNames and data).

Q1. Should I make six tabular models, each of which corresponds to each table, and then extends them to CommonTableModel?

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

In the above example, I tried to initialize the inherited data elements so that each of the six table models could fill in the column names according to the table that they designate.

But I have a bug that limits me to initializing the inherited members in this way. I think we cannot initialize instance variables this way.

Then how can I populate the instace variables for the CommonTableModel so that the methods of the CommonTableModel instance process the data that I populate them later.

One solution is to pass the data in the CommonTableModel constructor, but this way I have to pass all the column names every time I create a table.

I am very confused because I do not have much experience in programming and do not know good coding methods.

Please also name some good model template books so I can better understand design templates.

+4
source share
3 answers

But I have a bug that limits me to initializing the inherited members in this way. I think we cannot initialize instance variables this way.

Arrays not initialized with new are array constants . You can only initialize them after the declaration. For instance.

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

Thus, you must replace specific rows (if they are already protected by CommonTableModel fields):

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

Edit according to comments: you can also define a constructor for this and use the super() call. The advantage is that this improves the degree of encapsulation, i.e. You do not need to declare protected fields, but now they can be declared private . Here is an example run:

 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 to use the new keyword to create them (rsp was incorrect here in your answer). You should only NOT do static properties !! This would affect every instance of the same class. You really don't want that. Also see my comment below.

+2
source

If the only differences between your table models are column names, I would just pass them to your CommonTableModel constructor as an array of strings. Use the same class for all your tables, but different data.

+1
source

Table models extend the general table model, the general table model can initialize columns and data in its constructor, I think that you are looking for such a template:

 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"}}); } } 
+1
source

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


All Articles