0

基本的に、私はクライアントサーバーアーキテクチャに取り組んでいるので、一部のクライアントはオブジェクトを外部から変更できます。

私は銀行を持っています:

public class Bank{
    private List<BankingOperation> operationList = new ArrayList<BankingOperation>();

    public void addOperation(BankingOperation op) {
        this.operationList.add(op);
//...
}

そして私のサーバー:

public class ServerBank extends JFrame {
    private Bank bank;
    private JTable table;
    private OperationTableModel model;

    public ServerBank() {
        this.bank = new Bank();
        this.model= new OperationTableModel(this.bank.getOperationList());
        table = new JTable(model);
        getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        pack();
    }

    public static void main (String args[]) throws Exception {

        ServerBank frame=new ServerBank();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,700);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


    }
    class OperationTableModel extends AbstractTableModel {

    private static final long serialVersionUID = 1L;
    private List<BankingOperation> operationList;
    public String[] colNames = { "Date", "Login", "Customer","Account", "Operation", "Amount", "Final Balance" };
    public Class<?>[] colTypes = { String.class, String.class, String.class, String.class, String.class, Integer.class,
            Integer.class };


    public OperationTableModel(List<BankingOperation> operationList) {
        super();
        this.operationList = operationList;
    }//...
}

クライアントは、addOperation()を呼び出すことにより、バンクのoperationListにオペレーションを追加できます。

問題は、JTableがそれをどのように検出し、表示を更新できるかということです。

クライアントは、操作を追加するためにTableModelメソッドを使用していないためです。彼らはこのクラスにアクセスできません。その上、TableModelコンストラクターでBankのoperationList全体を指定するのが良いかどうかはわかりません...

4

1 に答える 1

1

By giving the clients access to the internal list used by the bank, you allow them to do things behind the bank of the bank. A bit like if a real bank gave access to its internal database, instead of forcing all the clients to go through the online bank application.

You should give the clients a reference to an interface which allows them to perform their operations. The implementation of this interface would control that every operation they do is allowed, and does everything necessary.

For example, the addOperation() method of the interface implementation would not only add the operation to the list of operations, but also fire a table model event in order for the table to display this added operation.

This could be done directly, if the bank encapsulates the table model, or indirectly, by having the bank fire a custom "operation added" event. The table model would listen to those events, and fire its own table model event in order for the table to be updated.

于 2012-12-03T12:41:55.420 に答える