JavaMvc分层模式

/ Tec / 没有评论 / 140浏览
转载请标明出处:
原文首发于:http://www.zhangruibin.com
本文出自RebornChang的博客

原文: Model–view–controller

Model–View–Controller (MVC) is a software architectural pattern for implementing user interfaces on computers. It divides a given application into three interconnected parts in order to separate internal representations of information from the ways that information is presented to and accepted from the user. The MVC design pattern decouples these major components allowing for efficient code reuse and parallel development. Traditionally used for desktop graphical user interfaces (GUIs), this architecture has become popular for designing web applications and even mobile, desktop and other clients. Popular programming languages like Java, C#, Ruby, PHP and others have popular MVC frameworks that are currently being used in web application development straight out of the box. Description As with other software architectures, MVC expresses the “core of the solution” to a problem while allowing it to be adapted for each system. Particular MVC architectures can vary significantly from the traditional description here. Components MVC in English or Model-View-Co1ntroller, an application that is input, process,output process in accordance with the Model, View, Controller isolated manner, such an application is divide into three layers-model layer, view layer, control layer. The model is the central component of the pattern. It expresses the application’s behavior in terms of the problem domain, independent of the user interface. It directly manages the data, logic and rules of the application. A view can be any output representation of information, such as a chart or a diagram. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants. The third part, the controller, accepts input and converts it to commands for the model or view. Mode1 (Model): is the business process/ status of the processing and business rules. Business process layer is the other black-box operation, the model view to accept the request of the data, and return the results of the final. The design of business models can be said to be the most important core of MVC. Currently popular model of EJB applications is a typical example of the application of technology from the perspective of the model further delineation in order to make full use of existing components, but it can not be used as a framework for application design model. It only tell you that according to the design of this model will be able to use certain technology components, thereby reducing the. Technical difficulties. Example of a developer, you can focus on business model design. MVC design pattern tells us that the application of the model according to certain rules of taking away the level of extraction is very important, which is to determine whether the development in accordance with good design. Abstract and concrete can not be separated too far, nor too close. MVC model did not provide the design method, but only tell you that the management of these models should be organized in order to facilitate reconstruction and improve the mode1 reusability. We can make an analogy with object programming, MVC defines a top-level category, the sub-class to tell it you have to do these, bat you can not do these restrictions. This is e developer of the programming is very important. There is also a business model of the model is very important that the data model. Data model mainly refers to the object data entities(continued of). For example, an order will be saved to the database, to obtain orders from the database. We can separate this mode1, all the operation of the database is only. limited to the model. Control (Controller) can be interpreted as a request received from the user, matching the model and view together to complete the user’s request. The role of division of control layer is also very clear that it clearly te11 you that it is a distributed, and what kind of model to choose, choose what kind of view, to complete what the user requests. Control layer does not do any data processing. For example, the user clicks on a link and control layer to receive are quest, does not deal with business. Interactions In addition to dividing the application into three kinds of components, the model–view–controller design defines the interactions between them. A model stores data that is retrieved according to commands from the controller and displayed in the view. A view generates new output to the user based on changes in the model. A controller can send commands to the model to update the model’s state (e.g., editing a document). It can also send commands to its associated view to change the view’s presentation of the model (e.g., scrolling through a document). History One of the seminal insights in the early development of graphical user interfaces, MVC became one of the first approaches to describe and implement software constructs in terms of their responsibilities. Trygve Reenskaug introduced MVC into Smalltalk-76 while visiting the Xerox Palo Alto Research Center (PARC) in the 1970s. In the 1980s, Jim Althoff and others implemented a version of MVC for the Smalltalk-80 class library. Only later did a 1988 article in The Journal of Object Technology (JOT) express MVC as a general concept. The MVC pattern has subsequently evolved, giving rise to variants such as hierarchical model–view–controller (HMVC), model–view–adapter (MVA), model–view–presenter (MVP), model–view–viewmodel (MVVM), and others that adapted MVC to different contexts.[citation needed] The use of the MVC pattern in web applications exploded in popularity after the introduction of Apple’s WebObjects in 1996, which was originally written in Objective-C (that borrowed heavily from Smalltalk) and helped enforce MVC principles. Later, the MVC pattern became popular with Java developers when WebObjects was ported to Java. Later frameworks for Java, such as Spring (released in October 2002), continued the strong bond between Java and MVC. The introduction of the frameworks and Django (July 2005, for Python) and Rails (December 2005, for Ruby), both of which had a strong emphasis on rapid deployment, increased MVC’s popularity outside the traditional enterprise environment in which it has long been popular. MVC web frameworks now hold large market-shares relative to non-MVC web toolkits. Recently, with the maturity of J2EE, it is becoming a recommendation in the J2EE platform,a design model,the majority of Java developersarealso very interested in the design model. MVC model is gradually developed in PHP and ColdFusion are in use, and growth trends, With the rapid increase in Web applications, MVC model for the development of Web applications is a very advanced design idea, no matter what language you choose, no matter how complicated the application, it can be for you to understand and provide the most basic application model analytical methods, structural products for you to provide a clear framework for the design, for your software projects in accordance with norms. Use in web applications Although originally developed for desktop computing, MVC has been widely adopted as an architecture for World Wide Web applications in major programming languages. Several commercial and noncommercial web frameworks have been created that enforce the pattern. These software frameworks vary in their interpretations, mainly in the way that the MVC responsibilities are divided between the client and server. Early web MVC frameworks took a thin client approach that placed almost the entire model, view and controller logic on the server. This is still reflected in popular frameworks such as Django, Rails and ASP.NET MVC. In this approach, the client sends either hyperlink requests or form input to the controller and then receives a complete and updated web page (or other document) from the view; the model exists entirely on the server. As client technologies have matured, frameworks such as AngularJS, EmberJS, JavaScriptMVC and Backbone have been created that allow the MVC components to execute partly on the client (also see Ajax) Goals of MVC Simultaneous development. Because MVC decouples the various components of an application, developers are able to work in parallel on different components without impacting and/or blocking one another. For example, a team might divide their developers between the front-end and the back-end. The back-end developers can design the structure of the data and how the user interacts with it without requiring the user interface to be completed. Conversely, the front-end developers are able to design and test the layout of the application prior to the data structure being available. Code reuse By creating components that are independent of one another, developers are able to reuse components quickly and easily in other applications. The same (or similar) view for one application can be refactored for another application with different data because the view is simply handling how the data is being displayed to the user. Advantages & disadvantages Advantages Simultaneous development - Multiple developers can work simultaneously on the model, controller and views. High cohesion - MVC enables logical grouping of related actions on a controller together. The views for a specific model are also grouped together. Low coupling - The very nature of the MVC framework is such that there is low coupling among models, views or controllers. Ease of modification - Because of the separation of responsibilities, future development or modification is easier Multiple views for a model - Models can have multiple views. Disadvantages Code navigability - The framework navigation can be complex, hence it introduces new layers of abstractions and requires users to adapt its decomposition criteria to MVC. Multi-artifact consistency - Decomposing a feature into three artifacts causes scattering. Thus, requiring developer(s) to maintain the consistency of multiple representations at once. Pronounced learning curve - Knowledge on multiple technologies becomes the norm. Developers using MVC need to be skilled in multiple technologies. 译文:(字数3050) 模型-视图-控制

模型-视图-控制(MVC)是一种使用电脑实现用户界面设计的软件设计模式。这种设计模式是依据用户提出并且能够接受的信息将需求划分为三个有内在联系的部分。MVC设计开发模式是分离出来主要的部分,这些部分可以允许重用代码和并行开发。 这种架构传统上用于桌面图形用户界面(GUIs),现在已经流行进行web应用程序甚至移动,桌面和其他客户的设计。一些流行的编程语言,例如Java、c#、Ruby、PHP和其他流行的MVC框架目前正在创造性的直接将其应用于web应用程序开发。 产品描述: 与其他的软件体系架构一样,MVC对于一个问题表现出的思想是“解决方案的核心”,这种思想适用于每一个操作系统。在此进行解释, 特定的MVC体系结构从传统的描述上可以有很大的不同。 组件: MVC英文即 Model-View-Controller,即把一个应用的输入、处理、输出流程按照Model、View、Contro1ler的方式进行分离,这样一个应用被分成三个层——模型层、视图层、控制层。 这种设计模式的核心是模型。在独立的用户界面问题上,它表达了应用程序的行为问题领域。它直接管理数据和应用程序的逻辑以及规则。 一个视图可以是任何的输出形式,比如表或者图。多个表可能有相同的信息,比如管理人员的条形图和会计人员的表格图。 第三个部分,控制层,进行命令的接收并且会将接收到的命令转化为模型或者是视图命令。 模型(Model) : 就是业务流程/状态的处理以及业务规则的制定。业务流程的处理过程对其它层来说是黑箱操作,模型接受视图请求的数調,并返回最终的处理结果。业务模型的设计可以说是MVC最主要的核心。目前流行的EJB模型就是一个典型的应用例子,它从应用技术实现的角度对模型做了进一步的划分,以便充分利用现有的组件,但它不能作为应用设计模型的框架。它仅仅告诉你按这种模型设计就可以利用某些技术组件,从而減少了技术上的困难。对一个开发者来说,就可以专注于业务模型的设计。MVC设计模式告诉我们,把应用的模型按一定的规则抽取出来,抽取的层次很重要,这也是判断开发人员是否优秀的设计依据。抽象与具体不能隔得太远,也不能太近。MVC并投有提供模型的设计方法,而只告诉你应该组织管理这些模型,以便于模型的重构和提高重用性。我们可以用对象编程来做比喻,MVC定又了一个顶级类,告诉它的子类你只能做这些,但没法限制你能做这些。这点对编程的开发人员非常重要。 业务模型还有一个很重要的模型那就是数据模型。数据模型主要指实体对象的数据保存(持续化)。比如将一张订单保存到数据库,从数据库获取订单。我们可以将这个模型单独列出,所有有关数据库的操作只限制在该模型中。 控制(Controner)可以理解为从用户接收请求,将模型与视图匹配在一起, 共同完成用户的请求。划分控制层的作用也很明显,它清楚地告诉你,它就是一个分发器,选择什么样的模型,选择什么样的视图,可以完成什么样的用户请求。 控制层并不做任何的数据处理。 例如,用户点击一个连接,控制层接受请求后,并不处理业务信息,它只把用户的信息传通给模型,告诉模型做什么,选择符合要求的视图返回给用户。 因此,一个模型可能对应多个视图,一个视图可能对应多个模型。 交互: 除了将应用划分为三种组件,模型-视图-控制设计模式还定义了它们之间的交互。一个模型存储了数据,并且会根据控制器的命令重新取回数据并且在视图层中进行显示。 一个视图会根据模型的变化产生新的输出。 一个控制层可以对模型层发送命令来进行模型层数据的更新(比如,编辑文档)。它还可以对跟自身有联系的视图发送命令,改变视图层和模型层之间的关联(比如,通过一个文档实现滚动效果)。 历史: MVC在早期的用户图形界面的早期发展的一个开创新的见解,是首要的对软件职责方面进行描述和执行软件构造的方法。 Trygve Reenskaug在1970年参观施乐帕洛阿尔托研究中心(PARC)时将MVC这种设计思想带进了Smalltalk-80。在十九世纪八十年代,吉姆•奥托福和其他的一些人实现了MVC的Smalltalk-80版本类库的语言。后来在1988年的《华尔街日报》上的技术(JOT)专栏,把MVC作为一个普通的概念进行了简单的介绍。 随后MVC的版本概念发生了变化,设计者对版本模型进行了提升,比如分层模型-视图-控制模式(HMVC), 模型-视图-适配器模式(MVA), 模型-视图-发言者模式(MVP), 模型-视图-视图模型(MVVM)和其他的一些适合不同环境的MVC设计模式。[需求引用] 在1996年,苹果公司在WebObject项目中对MVC设计模式进行了引用,最初是在Objective-C(从Smalltalk- 80大批量的进行引用)并且实现了MVC设计的原则,从而使MVC这种设计模式形成爆发式的流行。之后,在Web项目被引入到Java中后,这种MVC的设计模式受到了Java开发者的热捧。后来的Java框架,比如Spring框架(在2002年10月发布的),加强了Java和MVC设计模式之间的联系。Django(Python,2005年7月)和Rails(Ruby,2005年12月)这两种语言都在对这种设计模式使用时进行了强调,增强了MVC在传统企业环境中的声望,使这种设计模式一直被企业所欢迎。相对于non-MVC网络工具包来说,MVC的网络框架现在持有市场上的大部分份额。 近来,随着 J2EE的成熟,它正在成为在 J2EE平台上推荐的一种设计模型,也是广大 Java开发者非常感兴趣的设计模型。MVC模式也逐新在 PHP和 Cold于usion开发者中道用,并有增长趋势。随者网络应用的快速增加,MVC模式对于web应用的开发无疑是一种非常先进的设计思想,无论你造择哪种语言,无论应用多复杂,它都能为你理解分析应用模型时提供最基本的分析方法,为你构造产品提供清晰的设计框架,为你的软件工程提供规范的依据。 在web应用程序中使用: 虽然最初是为了电脑桌面的开发,但是现在主要编程语言都把MVC这种设计模式作为是一种技术然后在进行互联网应用设计中进行使用。几种商业性的和非商业性的web框架都使用了这种设计模式。这些软件框架都有不同的解释,主要是MVC的客户机和服务器之间的责任划分。 早期web MVC设计框架采用瘦客户端的方法,把几乎整个模型,视图和控制器逻辑都放置在服务器上。在当下流行的框架比如Django、Rails和ASP也都反映了这个现象。在这种设计模式下,客户端发送请求链接或表单输入到控制器,然后从视图层接收到一个完整和更新web页面(或其他文档);这种模型完全的存处在服务器端。伴随着客户端技术的成熟,有些如AngularJS,EmberJS,JavaScriptMVC and Backbone框架都是为了使MVC设计模式可以在客户端执行部分功能所创造的(比如说Ajax)。 MVC的目标: 并行开发。 因为MVC应用程序的各种组件,开发人员可以在不同的组件的情况下进行并行工作而不影响和/或阻塞其他的进程。举例来说,一个团队的开发人员通常分为前端和后台。后台开发人员可以设计数据的结构和用户如何与它交互,而无需用户界面完成。相反的,前端开发人员能够在布局之前设计和测试应用程序的可用的数据结构。 代码复用: 通过创建独立的组件,开发人员可以快速、轻松地在其他应用程序中重用组件。设计者们普遍的有一个相同(或者是相似)的观点,那就是对于一个应用程序可以为另一个应用程序使用不同的数据,因为视图层可以轻易的处理数据然后显示给用户。 优势和弊端: 优势: 并行开发,多个开发人员可以同时对模型层进行控制和视图的设计。 高内聚-MVC使相关操作控制器的逻辑分组在一起。对于一个特定模式的视图可以分到一个组。 低耦合-对于MVC之间的不同的性质来说他们有低耦合模型,视图和控制器。 易于修改-因为职责的分离,未来的开发或者是修改将会更加的容易。 多个视图模型-模型可以有多个视图。 弊端: 代码的导航性-框架的导航可能是很复杂的,因此,在这种设计模式里面引入了新的层次的抽象,需要用户进行调整并且将其分解为标准的MVC。 Multi-artifact一致性,将功能分解为三个工作部分从而造成分散。因此,在设计的时候会要求开发人员维护多个表征的一致性,也可以说是代码的规范。 明显的学习曲线-认知在多样化的技术面前成为了规范。开发人员在使用MVC时需要在多个技术方面都很熟练。