关注点分离;MVC;为什么?

本文介绍了关注点分离;MVC;为什么?的处理方法,对大家解决问题具有一定的参考价值

问题描述

在开始我的下一个主要项目之前,我目前正在阅读 OO.为了让您快速了解一些背景知识,我是一名 PHP 开发人员,从事 Web 应用程序的工作.

I'm currently reading up on OO before I embark upon my next major project. To give you some quick background, I'm a PHP developer, working on web applications.

我特别感兴趣的一个领域是用户界面;特别是如何构建它并将其连接到我的 OO模型".

One area that particularly interests me is the User Interface; specifically how to build this and connect it to my OO "model".

我一直在阅读这方面的内容.我的最爱之一是这样的:为面向对象系统构建用户界面

I've been doing some reading on this area. One of my favourites is this: Building user interfaces for object-oriented systems

所有对象都必须提供自己的用户界面"

"All objects must provide their own UI"

考虑到我的问题,我可以看到它运行良好.例如,我构建了我的用户"对象来代表登录我网站的人.我的方法之一是display_yourself"或类似方法.我可以在整个代码中使用它.也许一开始就是他们的名字.后来,如果我需要调整显示他们的名字+小头像,我可以只更新这个方法,嘿嘿,我的应用程序更新了.或者,如果我需要将他们的名字作为指向他们个人资料的链接,嘿嘿,我可以从一个地方轻松地再次更新.

Thinking about my problem, I can see this working well. I build my "user" object to represent someone who has logged into my website, for example. One of my methods is then "display_yourself" or similar. I can use this throughout my code. Perhaps to start with this will just be their name. Later, if I need to adjust to show their name+small avatar, I can just update this one method and hey-presto, my app is updated. Or if I need to make their name a link to their profile, hey-presto I can update again easily from one place.

就面向对象系统而言;我认为这种方法效果很好.查看其他 StackOverflow 线程,我在关注点分离"下发现了这一点:社会

In terms of an OO system; I think this approach works well. Looking on other StackOverflow threads, I found this under "Separation of Concerns": Soc

"在计算机科学中,分离关注(SoC)是一个过程将计算机程序分解为重叠的不同特征功能尽量少.一个关注是任何感兴趣或专注于一个程序.通常,关注点是特征的同义词或行为.SoC 的进展是传统上通过模块化和封装性信息隐藏的帮助."

"In computer science, separation of concerns (SoC) is the process of breaking a computer program into distinct features that overlap in functionality as little as possible. A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors. Progress towards SoC is traditionally achieved through modularity and encapsulation, with the help of information hiding."

在我看来,我已经做到了这一点.我的用户对象隐藏了它的所有信息.我的代码中没有任何地方在显示之前说 $user->get_user_name().

To my mind I have achieved this. My user object hides all it's information. I don't have any places in my code where I say $user->get_user_name() before I display it.

然而,这似乎与其他人认为的最佳实践"背道而驰.

However, this seems to go against what other people seem to think of as "best practice".

引用同一问题中的选定"(绿色)答案:

To quote the "selected" (green one) answer from the same question:

"关注点分离是保持每个问题的代码分离.改变界面不应该要求改变业务逻辑代码,反之亦然.模型-视图-控制器 (MVC) 设计模式是一个很好的例子更好地分离这些问题软件可维护性."

"The separation of concerns is keeping the code for each of these concerns separate. Changing the interface should not require changing the business logic code, and vice versa. Model-View-Controller (MVC) design pattern is an excellent example of separating these concerns for better software maintainability."

为什么这会提高软件的可维护性?当然,对于 MVC,我的视图必须非常了解模型?阅读 JavaWorld 文章以详细讨论这一点:为面向对象系统构建用户界面

Why does this make for better software maintainability? Surely with MVC, my View has to know an awful lot about the Model? Read the JavaWorld article for a detailed discussion on this point: Building user interfaces for object-oriented systems

不管怎样……总算说到点子上了!

Anyway... getting to the actual point, finally!

1. 谁能推荐一本详细讨论这个问题的书?我不想要一本 MVC 的书;我不卖 MVC.我想要一本讨论 OO/UI、潜在问题、潜在解决方案等的书.(可能包括 MVC)Arthur Riel 的面向对象的设计启发式

1. Can anyone recommend any books that discuss this in detail? I don't want an MVC book; I'm not sold on MVC. I want a book that discusses OO / UI, the potential issues, potential solutuions etc.. (maybe including MVC) Arthur Riel's Object-Oriented Design Heuristics

触及它(也是一本很棒的书!),但我想要更详细的东西.

touches on it (and is an excellent book as well!), but I want something that goes into more detail.

2. 谁能提出一个像 Allen Holub 的 JavaWorld 文章那样解释清楚的论点,解释为什么 MVC 是一个好主意?

2. Can anyone put forward an argument that is as well-explained as Allen Holub's JavaWorld article that explains why MVC is a good idea?

非常感谢任何能帮助我得出结论的人.

Many thanks for anyone who can help me reach a conclusion on this.

推荐答案

这是 OOP 教学经常失败的地方,使用了像 rectangle.draw() 和恐龙.show() 这样的例子,这些例子完全没有意义.

This is a failure in how OOP is often taught, using examples like rectangle.draw() and dinosaur.show() that make absolutely no sense.

当您谈论拥有显示自身的用户类时,您几乎是在回答自己的问题.

You're almost answering your own question when you talk about having a user class that displays itself.

以后,如果我需要调整显示他们的名字+小头像,我可以只更新这一种方法,嘿嘿,我的应用程序更新了."

"Later, if I need to adjust to show their name+small avatar, I can just update this one method and hey-presto, my app is updated."

暂时只考虑那一小块.现在看看 Stack Overflow 并注意您的用户名出现的所有位置.在每种情况下看起来都一样吗?不,在顶部,您的用户名旁边有一个信封,后面是您的声誉和徽章.在一个问题线程中,您的头像后面是您的用户名,下面是您的声誉和徽章.你认为有一个用户对象有像 getUserNameWithAvatarInFrontOfItAndReputationAndBadgesUnderneath() 这样的方法吗?不.

Think about just that little piece for moment. Now take a look at Stack Overflow and notice all of the places that your username appears. Does it look the same in each case? No, at the top you've just got an envelope next to your username followed by your reputation and badges. In a question thread you've got your avatar followed by your username with your reputation and badges below it. Do you think that there is a user object with methods like getUserNameWithAvatarInFrontOfItAndReputationAndBadgesUnderneath() ? Nah.

一个对象与它所代表的数据和作用于该数据的方法有关.您的用户对象可能具有 firstName 和 lastName 成员,以及检索这些部分所需的 getter.它也可能有一个像 toString()(在 Java 术语中)这样的便捷方法,它会以一种通用格式返回用户名,比如名字后跟一个空格,然后是姓氏.除此之外,用户对象不应该做太多其他事情.由客户决定要对对象做什么.

An object is concerned with the data it represents and methods that act on that data. Your user object will probably have firstName and lastName members, and the necessary getters to retrieve those pieces. It might also have a convenience method like toString() (in Java terms) that would return the user's name in a common format, like the first name followed by a space and then the last name. Aside from that, the user object shouldn't do much else. It is up to the client to decide what it wants to do with the object.

以您为我们提供的用户对象为例,然后考虑如果您在其中构建了UI",您将如何执行以下操作:

Take the example that you've given us with the user object, and then think about how you would do the following if you built a "UI" into it:

  1. 创建显示所有用户的 CSV 导出,按姓氏排序.例如.姓氏,名字.
  2. 提供重量级的 GUI 和基于 Web 的界面来处理用户对象.
  3. 在一处显示用户名旁边的头像,而在另一处仅显示用户名.
  4. 提供用户的 RSS 列表.
  5. 在一处显示用户名粗体,在另一处显示为斜体,并在另一处显示为超链接.
  6. 在适当的地方显示用户的中间名首字母.

如果你考虑一下这些需求,它们都归结为提供一个用户对象,它只关心它应该关心的数据.它不应该试图对每个人都适用,它应该只是提供一种获取用户数据的方法.您将创建的多个视图中的每一个都决定如何显示用户数据.

If you think about these requirements, they all boil down to providing a user object that is only concerned with the data that it should be concerned with. It shouldn't try to be all things to everyone, it should just provide a means to get at user data. It is up to each of the many views you will create to decide how it wants to display the user data.

您关于在一处更新代码以在多处更新您的视图的想法是个好主意.这仍然是可能的,而无需在太低的水平上搞砸.您当然可以创建类似小部件的类来封装东西"的各种常见视图,并在整个视图代码中使用它们.

Your idea about updating code in one place to update your views in many places is a good one. This is still possible without mucking with things at a too low of a level. You could certainly create widget-like classes that would encapsulate your various common views of "stuff", and use them throughout your view code.

这篇关于关注点分离;MVC;为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1170

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1071

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:808

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:900

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:877

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:864

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:834

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1730

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1308