基于MVC模式的电子商务网站的分析与标准设计

上传人:hs****ma 文档编号:394962855 上传时间:2023-04-12 格式:DOC 页数:12 大小:133KB
返回 下载 相关 举报
基于MVC模式的电子商务网站的分析与标准设计_第1页
第1页 / 共12页
基于MVC模式的电子商务网站的分析与标准设计_第2页
第2页 / 共12页
基于MVC模式的电子商务网站的分析与标准设计_第3页
第3页 / 共12页
基于MVC模式的电子商务网站的分析与标准设计_第4页
第4页 / 共12页
基于MVC模式的电子商务网站的分析与标准设计_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《基于MVC模式的电子商务网站的分析与标准设计》由会员分享,可在线阅读,更多相关《基于MVC模式的电子商务网站的分析与标准设计(12页珍藏版)》请在金锄头文库上搜索。

1、Based on MVC Pattern Analysis and design of e-commerce (B2C)sitesBefore we start our journey into the internals of Spring MVC, we first need to understand the different layers of a web application. And well begin that discussion with a brief introduction of the MVC pattern in general, including what

2、 it is and why should we use it. After reviewing the MVC Pattern, we will go through the different layers in a web application and see what role each layer plays in the application. The Model View Controller pattern (MVC pattern) was first described by Trygve Reenskaug when he was working on Smallta

3、lk at Xerox. At that time, the pattern was aimed at desktop applications. This pattern divides the presentation layer into different kinds of components. Each component has its own responsibilities. The view uses the model to render itself. Based on a user action, the view triggers the controller, w

4、hich in turn updates the model. The model then notifies the view to (re)render itself.The MVC pattern is all about separation of concerns. As we mentioned previously, each component has its own role (see Table 3-1). Separation of concerns is important in the presentation layer because it helps us ke

5、ep the different components clean. This way, we dont burden the actual view with business logic, navigation logic, and model data. Following this approach keeps everything nicely separated, which makes it easier to maintain and test our application.What Is MVC:MVC is a design pattern that breaks an

6、application into three parts: the data (Model),the presentation layer (View), and the user interaction layer (Controller). In otherwords, the event flow goes like this:1. The user interacts with the application.2. The controllers event handlers trigger.3. The controller requests data from the model,

7、 giving it to the view.4. The view presents the data to the user.Or, to give a real example, Figure 1-1 shows how sending a new chat message wouldwork with Holla.Figure 1-1. Sending a new chat message from Holla1. The user submits a new chat message.2. The controllers event handlers trigger.3. The c

8、ontroller creates a new Chat Model record.4. The controller then updates the view.5. The user sees his new chat message in chat log.The MVC architectural pattern can even be implemented without libraries or frameworks.The key is to divide up the responsibilities of the MVC components into clearlydef

9、ined sections of code, keeping them decoupled. This allows for independent development,testing, and maintenance of each component.Lets explore the components of MVC in detail.The Model:The model is where all the applications data objects are stored. For example, we might have a User Model that conta

10、ins a list of users, their attributes, and any logic associated specifically with that model.A model does not know anything about views or controllers. The only thing a model should contain is data and the logic associated directly with that data. Any event handling code, view templates, or logic no

11、t specific to that model should be kept well clear of it. You know an applications MVC architecture is violated when you start seeing view code in the models. Models should be completely decoupled from the rest of your application.When controllers fetch data from servers or create new records, they

12、wrap them in model instances. This means that our data is object oriented, and any functions or logic defined on the model can be called directly on the data.So, rather than this:var user = usersfoo;destroyUser(user);We can do something like this:var user = User.find(foo);user.destroy();The first ex

13、ample is not namespaced or object oriented. If we have another destroyUser() function defined in our application, the two will conflict. Global variables and functions should always be kept to an absolute minimum. In the second example, the destroy() function is namespaced behind User instances, as

14、are all the stored records.This is ideal, since were keeping global variables to a minimum, exposing fewer areas to potential conflicts. The code is cleaner and can take advantage of inheritance so functions like destroy() dont have be defined separately on every model.Models are explored in much mo

15、re depth in Chapter 3, which covers topics such asloading in data from servers and creating object-relational mappers (ORMs).The View:The view layer is whats presented to the user and is what she interacts with. In a JavaScript application, the view would be made up mostly of HTML, CSS, and Java-Scr

16、ipt templates. Apart from simple conditional statements in templates, the views Should not contain any logic.In fact, like models, views should also be decoupled from the rest of the application.Views should not know anything about controllers and modelsthey should be independent.Mixing up views with logic is one of the surest paths to disaster.That is not to say MVC does not allow for pr

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号