观察者设计模式 - 需要帮助来理解为什么这不起作用

时间:2016-12-20 16:37:26

标签: java swing jtable observer-pattern

有人会非常友好地看一下这个简单的程序并告诉我实施Observer设计模式有什么问题吗?

当我运行它时,表中没有任何内容显示(在实现之前显示)。

我刚开始学习设计模式,所以我不确定如何调试它。

我的老师给了我们一个带有Observer设计模式的程序示例......它似乎没有用!

程序只是jTable中数据的数据。

我提前感谢你 - > here's the complete code

以下是数据输入GUI:

package com.employe.listing;

import java.util.ArrayList;

public class addEmployeJFrame extends javax.swing.JFrame {

    ArrayList<addEmployeJFrameObserver> observers = new ArrayList<>();

    public addEmployeJFrame() {
        initComponents();

    }

    public synchronized void addAddEmployeJFrameObserver(addEmployeJFrameObserver observer) {
        observers.add(observer);
    }

    public synchronized void removeAddEmployeJFrameObserver(addEmployeJFrameObserver observer) {
        observers.remove(observer);
    }

    public synchronized void notifyAddEmployeJFrameObserver(Employe e) {

        for(addEmployeJFrameObserver o : observers) {
            o.NotifyAddEmployeJFrame(e);
        }

    }                     

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        Employe employe = new Employe();
        employe.setName(jTextField1.getText());
        employe.setJob(jTextField2.getText());
        employe.setSalary(Integer.parseInt(jTextField3.getText()));
        employe.setYearsAtWork(Integer.parseInt(jTextField4.getText()));

        notifyAddEmployeJFrameObserver(employe);
    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration                   
}

这是控制器:

package com.employe.listing;

import java.util.ArrayList;

public class Controller implements addEmployeJFrameObserver {

    ArrayList<Employe> employes = new ArrayList<>();
    ArrayList<ListingEmployesJFrameObserver> observers = new ArrayList<>();
    addEmployeJFrame EmployeJFrame;
    ListingEmployesJFrame listingJFrame;

    public Controller() {

        listingJFrame = new ListingEmployesJFrame();
        EmployeJFrame = new addEmployeJFrame();
        EmployeJFrame.addAddEmployeJFrameObserver(this);
        listingJFrame.setVisible(true);
        EmployeJFrame.setVisible(true);

    }

    public synchronized void addListingEmployesJFrameObserver(ListingEmployesJFrameObserver o) {
        observers.add(o);
    }

    public synchronized void removeListingEmployesJFrameObserver(ListingEmployesJFrameObserver o) {
        observers.remove(o);
    }

    public synchronized void notifyListingEmployesJFrameObserver(ArrayList<Employe> employes) {
        for(ListingEmployesJFrameObserver o : observers) {
            o.notifyListingEmployesJFrame(employes);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Controller controler = new Controller();

    }

    public void addEmploye(Employe employe) {

        employes.add(employe);

        notifyListingEmployesJFrameObserver(employes);

    }

    public ArrayList<Employe> getListEmployee() {
        return employes;
    }


    @Override
    public void NotifyAddEmployeJFrame(Employe e) {
        this.addEmploye(e);
    }
}

这里是jTable(数据输出):

package com.employe.listing;


import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class ListingEmployesJFrame extends javax.swing.JFrame implements ListingEmployesJFrameObserver {

    ArrayList<Employe> employes = new ArrayList<>();

    /**
     * Creates new form ListingEmployesJFrame
     */
    public ListingEmployesJFrame() {
        initComponents();

        AbstractTableModel model = new AbstractTableModel() {

            @Override
            public String getColumnName(int column) {
                if(column == 0) {
                    return "Name";
                } else if(column == 1) {
                    return "Job";
                } else if(column == 2) {
                     return "Salary";
                } else {
                    return "Years";
                }
            }

            @Override
            public int getRowCount() {
                return employes.size();
            }

            @Override
            public int getColumnCount() {
                return 4;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {

                if(columnIndex == 0) {
                    return employes.get(rowIndex).getName();
                } else if(columnIndex == 1) {
                    return employes.get(rowIndex).getJob();
                } else if(columnIndex == 2) {
                     return employes.get(rowIndex).getSalary();
                } else {
                    return employes.get(rowIndex).getYearsAtWork();
                }
            }
        };

        jTable1.setModel(model);
    }


    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   

    @Override
    public void notifyListingEmployesJFrame(ArrayList<Employe> employes) {
        this.employes = employes;
        jTable1.updateUI();
    }

}

1 个答案:

答案 0 :(得分:0)

我在代码中的任何地方都没有看到您正在调用addListingEmployesJFrameObserver()来注册观察者,因此无法通知。

更新Controller.java的构造函数,如下所示:

public Controller() {
    listingJFrame = new ListingEmployesJFrame();
    EmployeJFrame = new addEmployeJFrame();
    EmployeJFrame.addAddEmployeJFrameObserver(this);

    listingJFrame.setVisible(true);
    EmployeJFrame.setVisible(true);

    // * add the line below to register listingJFrame as an observer *
    addListingEmployesJFrameObserver(listingJFrame);
}

编辑:值得指出的是,调用addListingEmployesJFrameObserver()并非推荐做法。有关详细说明,请参阅What's wrong with overridable method calls in constructors?