1

我创建了一个使用 MVC 架构版本的程序。该代码的目的是抓取网页列表的 h1 标题并将结果返回到 JTable。

到目前为止,我的程序运行良好。它按照我想要的方式返回结果,但直到最后它才会更新表格。我希望它在结果出现时更新表格。我想以一种在我刚刚学习时考虑到最佳实践原则的方式来做到这一点。

我假设要根据需要更新它,我将不得不稍微更改我的代码。我不确定动态更新 GUI 的最佳方式(线程、观察者,还是其他东西?)。我什至不确定“这段代码应该放在我的 MVC 模式中的哪个位置?”这个问题。说得通?

无论如何,这是我的观点:

public class SearchView extends JFrame{
//Components
private JLabel selectElementLabel = new JLabel("Element Selector:");
private JTextField selectElement = new JTextField("h1");;
private JComboBox<String> selectLocale; 

private DefaultTableModel tableModel = new DefaultTableModel();
private JTable resultTable = new JTable(tableModel);

private JLabel statusLabel;
private JButton runButton = new JButton("Run");
private JButton clearButton = new JButton("Clear");

private SearchModel s_model;

//Constructor
public SearchView(SearchModel model) {
    //Set the Logic here(model)
    s_model = model;    

    //Initialise Components here(model)
    selectLocale = new JComboBox<>(s_model.getLocales());
    selectLocale.setSelectedIndex(13);

    //Layout Components
    JPanel userInputPanel = new JPanel();
    userInputPanel.setLayout(new BoxLayout(userInputPanel, BoxLayout.X_AXIS));
    userInputPanel.add(selectElementLabel);
    userInputPanel.add(selectElement);
    userInputPanel.add(selectLocale);

    tableModel.addColumn("Page");
    tableModel.addColumn("Data");
    resultTable.setFillsViewportHeight(true);

    JScrollPane resultScroller = new JScrollPane(resultTable);
    resultScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    resultScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    resultScroller.setAlignmentX(Component.LEFT_ALIGNMENT);

    JPanel controlButtons = new JPanel();
    controlButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));
    controlButtons.add(statusLabel = new JLabel(s_model.getState()));
    controlButtons.add(clearButton);
    controlButtons.add(runButton);


    this.setTitle("Element Searcher");
    this.add(BorderLayout.NORTH, userInputPanel);
    this.add(BorderLayout.CENTER, resultScroller);
    this.add(BorderLayout.SOUTH, controlButtons);
    this.setExtendedState(Frame.MAXIMIZED_BOTH); 
    this.setMinimumSize(new Dimension(900, 600));
    this.setVisible(true);  
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
}

void reset(){
    tableModel.setRowCount(0);
}

String getSelectedElement(){
    return selectElement.getText();
}

String getSelectedLocale(){
    return selectLocale.getSelectedItem().toString();
}

void setResults(Object[] result){
    tableModel.addRow(result);
}

void addRunListener(ActionListener run){
    runButton.addActionListener(run);
}

void addClearListerner(ActionListener clear){
    clearButton.addActionListener(clear);
}

}

控制器:

public class SearchController {

private SearchModel s_model;
private SearchView s_view;

public SearchController(SearchModel model, SearchView view) {
    s_model = model;
    s_view = view;

    s_view.addRunListener(new RunListener());
    s_view.addClearListerner(new ClearListener());

}

class RunListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        String selectedLocale = null;
        try {
            selectedLocale = s_view.getSelectedLocale();
            s_model.setPageList(selectedLocale);
            for (String pageUrl : s_model.getPageList()){
                s_view.setResults(s_model.getResults(pageUrl));
            }
        } catch (Exception e1) {
            System.out.println(e1);
        }
    }
}

class ClearListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        s_model.reset();
        s_view.reset();
    }
}

}

最后是我的模型:

public class SearchModel {
//Constants
private static final String[] localeStrings = { "cs-cz", "da-dk", "de-at", "de-ch", "de-de", "el-gr", "en-ae", "en-au", "en-ca", "en-gb", "en-ie", "en-in", "en-nz", "en-us", "en-za", "es-cl", "es-co", "es-es", "es-mx", "fi-fi", "fr-be", "fr-ca", "fr-ch", "fr-fr", "hu-hu", "it-it", "ja-jp", "ko-kr", "nb-no", "nl-be", "nl-nl", "pl-pl", "pt-br", "pt-pt", "ru-ru", "sk-sk", "sv-se", "zh-hk", "zh-sg", "zh-tw" };
private static final String INITIAL_STATE = "idle";
private HashSet<String> pageList;
private Object[] scrapeResult;
private String locale = "en-us";

//Search State
private String searchState;

public SearchModel() {
    reset();
}

public void setPageList(String loc){
    locale = loc;
    ScrapeXML scraper = new ScrapeXML(locale);
    pageList = scraper.getUrls();
}

public void setResults(String page){
    ScrapeElements scraper = new ScrapeElements(page, locale);
    scrapeResult = scraper.getResults();
}

public void reset(){
    searchState = INITIAL_STATE;
}

public String[] getLocales(){
    return localeStrings;
}

public String getState(){
    return searchState;
}

public HashSet<String> getPageList(){
    return pageList;
}

public Object[] getResults(String page){
    setResults(page);
    return scrapeResult;
}

}

如果您对代码本身有任何意见或建议,请告诉我!

谢谢!

4

1 回答 1

1

如果没有双重调度,你就无法真正实现 MVC,这也称为侦听器模式。对于模型,您需要添加

 public void addListener(ModelListener listener) {
 }

和(所以当“你的”模型发生变化时,你可以停止收听旧模型)

 public void removeListener(ModelListener listener) {
 }

因此您可以让几乎未知的对象添加自己以接收模型更新,这些更新通常通过“侦听器”接口传递。

 public interface ModelListener {

   public void modelChanged(ModelChangeEvent event);

 }

ModelChangeEvent 通常类似于

 public class ModelChangeEvent {

   private Model source;

   public ModelChangeEvent(Model source, <possibly other fields here>) {
     this.source = source;
   }

   public Model getSource() {
     return source;
   }
 }

有了这个,听众可以像这样

 public ModelView implements ModelListener {

  ... stuff ...

   public void modelChanged(ModelChangeEvent event) {
     if (event.getSource() == model) {
       // this is my model!
       name = model.getName();
       age = model.getAge();
       ... and any other updates that this listener cares about ...
     }
   }

监听器在更改处理程序中的行为有一定的灵活性,最重要的部分是灵活性包含在监听器的范围内,模型现在基本上可以忽略正在监听的内容,只关注在监听

  ... in the model class ...

  private void notifyListeners() {
    ModelChangeEvent event = new ModelChangeEvent(this);
    for (ModelListener listener : listeners) {
      listener.modelChanged(event);
    }
  }

同样,通知侦听器的方式有很大程度的灵活性,但关键是当模型中可观察到的“元素”发生变化时,所有侦听对象都应该收到调用。它通常放在私有方法中的原因是因为它使重用模型变得更加容易,就像这样

  ... in the model class ...
  public void setName(String name) {
    this.name = name;
    notifyListeners();
  }

并且在每个听力课程中,它可能会(或可能不会)读出名称,具体取决于它所显示的内容。

  ... in a name and age sensitive listening class ...
   public void modelChanged(ModelChangeEvent event) {
     if (event.getSource() == model) {
       name = model.getName();
       age = model.getAge();
     }
   }


  ... in a name insensitive listening class ...
   public void modelChanged(ModelChangeEvent event) {
     if (event.getSource() == model) {
       // this is my model!
       age = model.getAge();
     }
   }

一旦你有了这样的东西,你的视图应该听你的模型,所以控制器不必对模型的可观察“元素”进行某种轮询,并结合定期刷新视图感兴趣的表示项目。

是的,这意味着您的视图通常在您的模型中有数据的副本,这是一件好事;因为,如果您决定需要修改演示文稿,您就有一份数据要修改。在“名称显示”视图中,您可能希望强制使用一致的大小写,如下所示

  ... in a name and age sensitive listening class ...
  ... note that _name_ is the name in the _view_ not the model! ...
   public void modelChanged(ModelChangeEvent event) {
     if (event.getSource() == model) {
       name = capitalize(model.getName());
       age = model.getAge();
     }
   }

现在您的模型和视图将始终保持同步。这大大简化了控制器代码,现在它只需要处理命令。对于每个命令,它可能会执行以下一项(或多项):

  1. 调用模型中的方法(视图将自动更新)。
  2. Lookup and switch a view's model (like a button for "next customer", don't destroy the view, just set the view's model). This requires writing a setModel(Model) method in the View.
  3. Create or destroy views (possibly setting their models).
于 2013-03-22T16:09:17.933 回答