简体   繁体   English

应用程序设计到​​MVC模式

[英]App design to MVC pattern

The answer on this question Swing: how to get container bind to JRadioButton? 关于这个问题的答案Swing:如何将容器绑定到JRadioButton? made me think of MVC in a simple app design. 让我在一个简单的应用程序设计中想到了MVC。 I describe the general idea of an app and my thoughts on MVC pattern for this case. 我描述了应用程序的一般概念以及我对此案例的MVC模式的想法。

Small app description : 小应用说明

This app lets user to add simple records that consist of name and description. 此应用程序允许用户添加由名称和描述组成的简单记录。 After pressing "add" button, they are added to the panel as two labels and radio button to let edit the record. 按“添加”按钮后,它们将作为两个标签和单选按钮添加到面板中,以便编辑记录。 User can save his list in a profile (serialize to xml, properties or somewhere else). 用户可以将其列表保存在配置文件中(序列化为xml,属性或其他位置)。

My thoughts on how to apply MVC here : 关于如何在这里应用MVC的想法

Model 模型

  • Record with name and description fields 记录名称和描述字段

  • Profile with serialization mechanism 配置序列机制的配置文件

View 视图

  • Panel that contains multiple panels (records list) - one for each record (radio button + 2 labels for name and description data) 包含多个面板的面板(记录列表) - 每个记录一个(单选按钮+ 2个名称和描述数据标签)

Controller 调节器

  • 2 text boxes with labels and a button to add record 2个带有标签的文本框和一个用于添加记录的按钮

  • button to edit a record 按钮编辑记录

At the moment there's no code samples (I'll provide them a little bit later). 目前没有代码样本(稍后我会提供它们)。 I don't want to hurry, I want to understand whether I go in a right MVC direction or something should be changed before implementation. 我不想着急,我想了解我是否进入了正确的MVC方向,或者在实施之前应该更改某些内容。

Updated 更新

Code sample 代码示例

*MainClass*:
public class RecordsControl extends JFrame {
    private RecordsModel model;
    private RecordsController controller;
    private RecordsControlView view;

    public RecordsControl() {
        super("Records Control");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        initMVC();
        getContentPane().add(view);

        pack();
        setMinimumSize(new Dimension(250, 500));
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    private void initMVC() {
        model = new RecordsModel();
        view = new RecordsControlView(controller);
        controller = new RecordsController(model, view);
    }
}

*Model*:
public class RecordsModel {
    //Record class has only two fields String::name and String::description
    private List<Record> RecordsList;

    public RecordsModel() {
        RecordsList = new ArrayList<Record>();
    }

    public void addRecord(String name, String description) {
        RecordsList.add(new Record(name, description));
    }

    public List<Record> getRecordsList() {
        return RecordsList;
    }
}

*View*:
public class RecordsControlView extends JPanel {
    private final RecordsController controller;

    private JLabel nameLabel;
    private JLabel descrLabel;
    private JTextField nameField;
    private JTextField descrField;
    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;
    private JPanel recordsListPanel;

    public RecordsControlView(RecordsController controller) {
        super();
        this.controller = controller;
        achievNameLabel = new JLabel("Name: ");
        achievDescrLabel = new JLabel("Description: ");
        achievNameField = new JTextField(15);
        achievDescrField = new JTextField(15);
        addButton = new JButton("Add");

        initGUI();
        initListeners();
    }

    private void initListeners() {
        addButton.addActionListener(controller);
    }

    private void initGUI() {
        //Main Panel
        this.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();

        //name panel
        //...BoxLayout label + panel

        //description panel
        //...BoxLayout label + panel

        //Records list Panel
        //...Vertical BoxLayout

        //Add widgets to GridBagLayout
        //Name panel
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.insets = new Insets(5, 5, 2, 2);
        add(namePanel, constraints);

        //Description Panel
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.insets = new Insets(0, 5, 5, 2);
        add(descrPanel, constraints);

        //Add button
        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.gridheight = 2;
        constraints.gridwidth = 1;
        constraints.insets = new Insets(5, 0, 5, 5);
        constraints.fill = GridBagConstraints.VERTICAL;
        add(addButton, constraints);

        //Records List panel
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.gridheight = GridBagConstraints.REMAINDER;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.insets = new Insets(0, 5, 5, 5);
        add(recordsListPanel, constraints);
    }

    public JButton getAddButton() {
        return addButton;
    }

    public void addRecord(JPanel record) {
        recordsListPanel.add(record);
    }
}

public class RecordsView extends JPanel {
    private static ButtonGroup radioButtons = new ButtonGroup();

    private JRadioButton radioButton;
    private JLabel name;
    private JLabel description;

    public RecordsView() {
        super();
        radioButton = new JRadioButton();
        name = new JLabel();
        description = new JLabel();

        initGUI();
    }

    private void initGUI() {
        radioButtons.add(radioButton);

        setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        add(radioButton, constraints);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.weightx = 1.0;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(name, constraints);

        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.weightx = 1.0;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(description, constraints);
}

*Controller*:
public class RecordsController implements ActionListener{
    private final RecordsModel model;
    private final RecordsControlView view;

    public RecordsController(RecordsModel model, RecordsControlView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == view.getAddButton()) {
            RecordsView record = new RecordsView();
            view.add(record);
            view.updateUI();
        }
    }
}

First of all, let's start by saying what actually MVC is : 首先,让我们首先说一下MVC究竟什么:

Model 模型

It's an abstraction layer that contain a lot of classes to deal with application logic. 它是一个抽象层,包含许多处理应用程序逻辑的类。 And since then its an abstracted concern, that means, the are no strict rules of a Model definition until its scoped in business logic 从那时起它就是一个抽象的关注点,也就是说,在它定义为business logic之前,它们不是模型定义的严格规则

View 视图

A view should read data from a Model directly and prepare an output. 视图应直接从模型中读取数据并准备输出。 For each model there should be singular view. 对于每个模型,应该有单一的视图。

Controller 调节器

Also known as Editor , its responsible for changing state of a Model , that means it should only be responsible for defining/re-defining common variables you are dealing with. 也称为Editor ,它负责更改Model状态,这意味着它应该只负责定义/重新定义您正在处理的公共变量。

If your application satisfies something like this, 如果你的应用程序满足这样的,

- A controller writes to either a Model or a View and does nothing else - 控制器写入ModelView ,不执行任何操作

- A view contains only display logic - 视图仅包含显示逻辑

- A model consist of application core classes - 模型由应用程序核心类组成

Then you are on the right track - you are applying MVC correctly. 然后你走在正确的轨道上 - 你正确地应用MVC。

An example of basic implementation, 基本实现的一个例子,

class ModelLayer
{
    public void ModelLayer()
    {
        this.age = 1;
    }

    public int getAgeFromDb()
    {
        return this.age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }
}

class View
{
     public void View(ModelLayer modelLayer)
     {
          this.modelLayer = modelLayer;
     }

     public string render()
     {
         return this.modelLayer.getAgeFromDb();
     }
}

class Controller
{
     public void Controller(ModelLayer modelLayer)
     {
          this.modelLayer = modelLayer;
     }

     public void onSaveBtnClick()
     {
          this.modelLayer.setAge(2);
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM