关注点分离; MVC;为什么? [英] Separation of concerns; MVC; why?

查看:64
本文介绍了关注点分离; 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

所有对象都必须提供自己的UI"

"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.

就OO系统而言;我认为这种方法行之有效.查看其他StackOverflow线程,我在关注分离"下找到了这一点: Soc

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,我的View必须了解很多有关Model的知识?阅读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.

推荐答案

使用矩形,绘制()和恐龙.show()之类的示例,这在通常的OOP教学中是一个失败,完全没有道理.

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成员,以及检索这些片段所需的获取器.它也可能有一个方便的方法,例如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;为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆