MVC :: What is a model?

前端 未结 5 1852
情深已故
情深已故 2021-02-05 04:07

I am at a point where I must make a decision about models. I am aware that models are what you use to do all your database manipulation. But are models restricted to this?

<
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 04:30

    The MVC paradigm is a design pattern where you organize your application in with the following structure.

    The Model: this is where you should keep your data model, the algorithms. For example if you write a spreadsheet application, you would keep the data structure of your spreadsheet. You would have the computation engine in your model, you would have the code to save and load your spreadsheet in your model. These model class could potentially be reused in other applications, for example if you have code to do compression of data.

    The View or views: these are the part of your code to visualize the data (the UI), for a spreadsheet you have a typical spreadsheet view with cells A1 to Z100 etc. You can also visualize your data using a chart view. Etc. A view could be reused in other application as well for example you could reuse your fancy chart view.

    The controller is what connects the views to the model. This is probably the least reusable piece, the controller know about the model, know which views to displays. Typically the controller will setup the callback that the view will call when the user interact with the app. The controller will then get the information from the model and update the view.

    If you follow these guidelines you might be able to change your model, for example change from a model that save files to a disk to a model that save files in the cloud without changing the UI... in theory. You could also be able to add new views without changing your model. You can also write unit tests or regression test for your models.

    There is no strict rules, the best is to use common sense and your own judgment.

提交回复
热议问题