为什么MVC设计模式广泛用于网站开发?

发布于 2024-10-16 14:20:48 字数 287 浏览 2 评论 0原文

我正在发展我的 OOP 设计模式知识,并且由于我的主要重点是网站开发和 Web 应用程序开发,我尝试在这些领域找到设计模式的示例,但似乎主要遇到 Web 框架(任何其他示例将不胜感激) 。在我看来,大多数(全部?)基于 PHP 的框架似乎都使用 MVC 设计模式。由于这是最广泛使用的,是否可以假设它是此类开发的最佳设计模式,或者它是否反映了与其他设计模式相比较浅的学习曲线?

我还注意到 codeigniter 框架同时使用单例模式和 MVC 模式。这种混合设计模式是否常见,是否有效,或者是否出于特定原因在 codeigniter 中使用?

I am developing my knowledge of OOP design patterns and as my main focus is website development and web app development, I have tried to find examples of design patterns in these fields but seem to come across web frameworks mainly (any other examples would be appreciated). It seems to me that the majority (all?) of PHP based frameworks appear to use the MVC design pattern. As this is the most widely used would it be right to assume that it is the best design pattern for this type of development or is it a reflection of a shallower learning curve as opposed to other design patterns?

I have also noticed that the codeigniter framework uses both a singleton pattern and an MVC pattern. Is this kind of hybrid design pattern common and is it effective or is was it used in codeigniter for a specific reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

计㈡愣 2024-10-23 14:20:48

他们按名称使用 MVC 模式。选择它是一个流行词,因为它广为人知。该概念用于 Web 应用程序的方式与原始模式非常相似。

它对于通用命名法很有用,因为人们对它应该做什么有一个模糊的理解。然而,还有更好的模式,例如 Model-View-Presenter 更好地描述了实践中所做的事情(该术语只是没有使用,因为它是未知的)。还有其他一些变体在技术上已经取代了 MVC。

单例是另一种代码结构思想。它与您如何设计一般应用程序流程无关。它是用于识别常见对象结构习语的几个描述性术语之一。由于其名气的扩大,该术语同样被过度使用。实际上,这对于良好的应用程序设计来说既不重要也不重要。

以下是常见对象 API 的设计模式名称的简短概述:
http://www.fluffycat.com/PHP-Design-Patterns/

一些关于 MVC 核心概念的讨论在这里(可能不相关,但非常有趣):
http://c2.com/cgi/wiki?MvcIsNotImplementable
http://c2.com/cgi/wiki?MvcIsNotObjectOriented

They use the MVC pattern by name. It's a buzzword picked because it's widely known. The way the concept is used for web applicatons has mediocre resemblence to the original pattern.

It's useful for a common nomenclature and because people have a vague understanding of what it is supposed to do. There are better patterns however, and for example Model-View-Presenter describes better what's done in practice (the term is just not used, because it's unknown). And there are other variations which have technically superseded MVC.

Singletons are another code structuring idea. It is unrelated to how you design the general application flow. It's one of a few descriptive terms intended for identifying common object structure idioms. The term is likelwise overused because of the extend of its fame. In practice it's neither important nor significant for good application design.

Here's a short overview for the design pattern names of common object APIs:
http://www.fluffycat.com/PHP-Design-Patterns/

Some discussions on the core concepts of MVC are here (maybe not relevant but highly interesting):
http://c2.com/cgi/wiki?MvcIsNotImplementable
http://c2.com/cgi/wiki?MvcIsNotObjectOriented

爱已欠费 2024-10-23 14:20:48

网站开发基本上有两个过程。您有服务器端,在其中收集和/或操作数据。然后是需要呈现某些内容的客户端。 (在此步骤中,一些数据将传递到视图)

正如您从 OOP 中知道的那样,向正在处理的数据添加结构是一种很好的做法。这就是模型发挥作用的地方。最后,控制器对模型进行处理,然后将数据传递到视图。

您可能会说 MVC 是两个原则的产物:服务器/客户端分离和 OOP。

至于单例类:它们确实用于共享数据库等资源。

With website development, there are basically two processes going on. You have the server side, where data is gathered and/or manipulated. Then there is the client side where something needs to be presented. (Some data is passed to Views in this step)

As you know from OOP, it is good practise to add structure to the data you are processing. This is where models come in. Finally, you have controllers who do processing on the models, and then pass data to the views.

You might say that MVC is the offspring of two principles: server/client side separation and OOP.

As for the singleton classes: these are indeed used for sharing resources like the database.

稀香 2024-10-23 14:20:48

因为它有道理! :)

这就是将应用程序的不同部分分成特定的层。每一层应该只做与特定关注点相关的事情。在 MVC 中,这些关注点是模型、视图和控制器。当您将应用程序划分为彼此独立工作的层时,您就拥有了所谓的解耦应用程序。这很好,因为它可以让您测试每一层而不涉及其他层。理论上,您也可以用新的实现完全替换一层,而无需更改其他层。

  • 模型 - 负责业务逻辑和持久化

  • 控制器 - 驱动应用程序并充当模型和视图之间的桥梁

  • 视图 - 向用户呈现模型

Because it makes sense! :)

It's all about separating different parts of your application into specific layers. Each layer should only do things related to a specific concern. In MVC those concerns are Model, View and Controller. When you have your application divided into layers that work independently from each other you have what is called a decoupled application. This is good as it lets you test each layer without involving the other layers. In theory you can also completely replace one layer with a new implementation without changing the other layers.

  • Model - Responsible for the business logic and persistence

  • Controller - Drives the application and works as a bridge between the model and the view

  • View - Presents the model to the user

-残月青衣踏尘吟 2024-10-23 14:20:48

我认为您在这里混淆了一些概念。首先,MVC 不完全是一种设计模式,而更多的是组织应用程序的一般概念。 B/c 没有一种或最好的 MVC 实现。 MVC 正是您的常识所告诉您的关于结构化数据处理的知识。将您所看到的内容与内部发生的情况以及处理的信息分开。

单例通常在 MVC 中使用,因为您有许多不同的对象使用相同的资源。为了组织起来,您可以使用作为单例实现的注册表来授予对数据库的访问权限。

最好的问候

Raffael

编辑:

实际上,对于 MVC 结构的有用实现应该是什么样子,有许多不同的观点。

例如,在书籍中,您通常会看到三个框,标签为“模型”、“视图”和“控制器”。并且它们都是通过箭头连接起来的。 IMO 应该忽略“视图”和“模型”之间的连接,因为模型应该由与视图通信的控制器处理。

另外我认为区分业务逻辑和控制器非常重要。很容易混淆。但BL属于模型级别。从这个意义上说,我不认为 MVC 是一个类似三角形的东西,而更像是一个三层系统 V/C/M。

I think you are confusing some concepts here. First of all, MVC is not exactly a design pattern but more a general concept of organizing an application. B/c there is not one or the best implementation for an MVC. MVC is just what your common sense tells you about structuring data processing. Seperating what you see, from what is going on internally and what information is processed at all.

Singletons are usually used b/c in an MVC you have lots of different objects using the same resources. To organize that you can use Registry implemented as a singelton to give access to the DB for example.

Best regards

Raffael

EDIT:

actually there are many different views on what a useful realization of an MVC-structure should look like.

For example, in books you usually see three boxes, labelles 'model', 'view' and 'controller'. And they are all connected by arrows. IMO the connection between 'view' and 'model' should be left out, cause the model should be handled by the controller which communicates with the view.

Also I think it is very important to distinguish between business logic and the controller. Get's easily mixed up. But BL belongs to the model tier. In that sense I don't think of the MVC as a something like a triangle but more of a three-tier-system V/C/M.

凶凌 2024-10-23 14:20:48

一般来说:

  • MVC(模型-视图-控制器)是一种软件架构。
    重要:没有 MVC 设计模式;)
  • 设计模式是针对软件设计中常见问题的通用可重用解决方案。换句话说:它是解决常见问题的最佳实践。
    • 示例:单例、工厂方法、适配器、装饰器等。

那么为什么 MVC 如此频繁地被使用?
原因很简单:如果项目的所有开发人员都熟悉 MVC(或其他架构),他们就会对在哪里放置代码或在哪里查找代码有共同的理解。这是关于高效并编写好的代码。所以最终还是要达成共识。

In general:

  • MVC (Model–View–Controller) is a software architecture.
    Important: there is no MVC design pattern ;)
  • A design pattern is a general reusable solution to a commonly occurring problem in software design. In other words: it as the best practice for a common problem.
    • Examples: Singleton, Factory Method, Adapter, Decorator, etc.

So why is MVC used that often?
The reason is quite simple: If all developers of a project are familiar with MVC (or another architecture) they have a common understanding on where to place code or where to look for code. It's about being effective and write good code. So at the end it is about having a common understanding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文