使用线程在 Swing (Java) 中实现 MVC 范式



我正在尝试使用多线程在Java(Swing)中实现MVC范式。目前,我正在使用观察者类扩展视图类,并使用可观察类扩展模型类,并且它工作正常(我的代码在这个例子中是:http://austintek.com/mvc/)。控制器类只是调用相应的模型和视图函数,并且有一个主要的"胶水"程序来设置所有内容。但是,此方法不利用线程。有没有办法通过同时使用线程观察者/可观察类来实现这一点?

(我的目标是将 M、V 和 C 中的每一个实现为一个单独的线程)

以下是我的视图代码的一部分:

public class View implements Observer
{ 
/*************************************** View *************************************
        **
    **  This function is the constructor of the View class.
    **
    **      PRE: <nothing>
    **      POST: the GUI is created and the directory is displayed on the screen.
    **      RETURN: <N/A>
    **
    **/
    View(String name)
    {
        threadName = name;
        //frame in constructor and not an attribute as doesn't need to be visible to whole class
        JFrame frame = new JFrame("simple MVC");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addElements(frame); // this is implemented separately
        // frame.addWindowListener(new CloseListener());    
        frame.setSize(900, 732);
        // frame.setLocation(100,100);
        /** Display the window **/
        //frame.pack();
        frame.setVisible(true);
    }// View

以下是我的模型代码的一部分:

 public class Model extends java.util.Observable
    {
        /****************** variable DECLARATIONS *********************/
    // The hash table in which the directory is stored
    private Hashtable<String, Integer> tel_dir 
                    = new Hashtable<String, Integer>();
    // The path of the telephone directory during runtime
    private URL filePath
            = Model.class.getClassLoader().getResource("directory.txt");
    // the name of the thread
    private String  threadName;
                        /****** private Thread  t; ******/
    // the object in which the message is sent to the view
    private Message message = new Message();
    /** GETTERS and SETTERS **/
    ....

    /*************************************** Model *************************************
    **
    **  This function is the constructor of the model class.
    **
    **      PRE: <nothing>
    **      POST: the telephone directory is input from the file and stored in a hash
    **            table.
    **      RETURN: <N/A>
    **
    **/
    Model(String name)
    {
        int    phone_num;
        String customer_name;
        Scanner scanner = null;
        threadName      = name;
        /** Opening the handle to the file containing the telephone directory **/
        try
        {
            scanner = new Scanner(new File(filePath.getPath()));
        }
        catch (FileNotFoundException e1)
        {
            e1.printStackTrace();
        }
        /** Inputting from the file and storing it in the hash table **/
        while (scanner.hasNextInt())
        {
            phone_num     = scanner.nextInt();
            customer_name = scanner.nextLine();
            customer_name = customer_name.trim();
            tel_dir.put(customer_name, phone_num);
        }
    }// Model

我的目标是将 M、V 和 C 中的每一个实现为一个单独的线程。

这可能不是一个有用的划分。Swing 视图必须仅在事件调度线程上构造和操作,并且用户希望 Swing 控件保持响应,如此处所述。

相反,请在SwingWorker后台运行任何耗时的模型,并在process()的实现中更新侦听视图。在此示例中,视图组件直接更新,但您也可以为注册的侦听器触发自定义事件,如此处建议。在此示例中,chart通过SeriesChangeEvent侦听其数据模型 series ;它响应工作人员在 process() 中的更新。您可以通过 PropertyChangeEvent 显示进度,如此处或此处的建议。

最新更新