DEV Community

Jihao Deng
Jihao Deng

Posted on

DA06 Presentation Layer

本篇主要讲企业级应用的表现层相关pattern

Presentation Layer主要负责用户与系统的交互,通常是以GUI为基础的。

MVC and MVP

  • Model: represents the information about the domain;
  • View: deals with the display of information to the client via a user interface;
  • Controller / Presenter: handels the interactions between the View and the Model.

In MVC, all three elements may interact with each other (base on different application). While in MVP, a derivation of MVC, interactions only happen between M and P OR V and P.

Key features of MVC:

  • It separates the presentation layer from the domain model layer;
  • The Controller performs the action of invoking the Model and sending data to View;
  • The Model is not even aware that it is used by some web application or a desktop application.

Page Controller

An object that handles a request for a specific page or action on a Web site. Each web page or each action of the webpage (such as submit button) will have its own controller.

Page Controller的优缺点

Pros

  • 简单
  • 容易理解

Cons

  • 存在大量重复代码
  • 扩展性差

Front Controller

A controller that handles all the requests for a Web site.

A front controller has two parts:

  • a handler, and
  • a command hierarchy

The handler is responsible for receiving the request and dentifying the appropriate command class to execute; it is usually implemented as a servlet as opposed to a server page. It could be done statically or dynamically (reflection).

Page Controller的优缺点

Pros

  • Single entry point: Having a single controller makes the web server configuration much easier compared to the page controller.
  • 可扩展性

Cons

  • 复杂

Template View

Renders information into HTML by embedding markers in an HTML page.

Server page technologies, such as JSP, ASP and PHP, are examples where the template view pattern is used for generating views. These technologies allow scriptlets, containing programming logic to be embedded in the pages.

简单来说就是网页的html里面有后端的代码。

Template View的优缺点

Pros

  • 简单直接

Cons

  • 维护性差
  • Promotes bad design practises: Embedding code within pages results in designs that do not follow good design principles. Code is not well encapsulated and does not follow the modularity and layering principles, which further discourages reuse.
  • 难以测试,因为难以把静态的代码和动态的代码分离来做单元测试

Transform View

A view that processes domain data element by element and transforms it into HTML.

我们使用的XSLT和JavaScript就是典型的Transform View语言。它们先把后端的数据转换为前端的代码,然后再发给前端展示。

Transform View的优缺点

Pros

  • 便携,跨平台,前后端分离
  • Encapsulation: The logic in the view is limited to displaying, which is a good design practice.
  • 容易测试

Cons

  • 繁琐

Two Step View

Turns domain data into HTML in two steps: first by forming some kind of logical page, then rendering the logical page into HTML.

The first stage assembles the information in a logical screen structure that is suggestive of the display elements yet contains no HTML.

The second stage takes that presentation-oriented structure and renders it into HTML.

实现的方法是使用两步XSLT。

Two Step View的优缺点

Pros

  • Improved maintainability: Making global changes to the view format is much easier due to good encapsulation.

Cons

  • 复杂

Application Controller

A centralised point for handling screen navigation and the flow of an application.

The application controller has two main responsibilities:

  • deciding which domain logic object to run; and
  • deciding the view for displaying the response

Application Controller的优缺点

Pros

  • Simplified input controller: Enables handing complex screen navigation logic; avoid this being done in the input controller.

  • Less duplicated code: Code is less likely to be dispersed around the application due to the central design, resulting is less duplication.

Cons

  • 复杂

Top comments (0)