开发者

Class for storing data in java, simulating a mysql table

I want to use a class in Java for storing data which can be accessed for all other classes. Just like if it was a Mysql table.

This class should have only one instance, this way whenever its content is changed, all classes see the same content.

What is the best way to do it? I have read about the singleton patter开发者_如何学运维n, but I don't know if it would be a design error to use it.


Create a singleton class with a data structure that meets your needs (Map, or List, or combo). Like so (example was just handcoded, dont know if it will compile):

public class FakeTable {
    private Map vals = new HashMap();
    private static final FakeTable INSTANCE = new FakeTable();
    private FakeTable() {}   // Private constructor
    public static FakeTable getInstance() {
        return INSTANCE;
    }

    public void setValue(String col, Object val) {
        vals.put(col, val);
    }

    public Object getVal(String col) {
        return vals.get(col);
    }

}


Either have the data accessible as a static member or use a singleton. I would extend TableModel. TableModel is designed well already, and you can display your data easily in a JTable later if you wish.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜