久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

關(guān)注點(diǎn)分離;MVC;為什么?

Separation of concerns; MVC; why?(關(guān)注點(diǎn)分離;MVC;為什么?)
本文介紹了關(guān)注點(diǎn)分離;MVC;為什么?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

在開(kāi)始我的下一個(gè)主要項(xiàng)目之前,我目前正在閱讀 OO.為了讓您快速了解一些背景知識(shí),我是一名 PHP 開(kāi)發(fā)人員,從事 Web 應(yīng)用程序的工作.

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.

我特別感興趣的一個(gè)領(lǐng)域是用戶(hù)界面;特別是如何構(gòu)建它并將其連接到我的 OO模型".

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

我一直在閱讀這方面的內(nèi)容.我的最?lèi)?ài)之一是這樣的:為面向?qū)ο笙到y(tǒng)構(gòu)建用戶(hù)界面

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

所有對(duì)象都必須提供自己的用戶(hù)界面"

"All objects must provide their own UI"

考慮到我的問(wèn)題,我可以看到它運(yùn)行良好.例如,我構(gòu)建了我的用戶(hù)"對(duì)象來(lái)代表登錄我網(wǎng)站的人.我的方法之一是display_yourself"或類(lèi)似方法.我可以在整個(gè)代碼中使用它.也許一開(kāi)始就是他們的名字.后來(lái),如果我需要調(diào)整顯示他們的名字+小頭像,我可以只更新這個(gè)方法,嘿嘿,我的應(yīng)用程序更新了.或者,如果我需要將他們的名字作為指向他們個(gè)人資料的鏈接,嘿嘿,我可以從一個(gè)地方輕松地再次更新.

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.

就面向?qū)ο笙到y(tǒng)而言;我認(rèn)為這種方法效果很好.查看其他 StackOverflow 線程,我在關(guān)注點(diǎn)分離"下發(fā)現(xiàn)了這一點(diǎn):社會(huì)

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

"在計(jì)算機(jī)科學(xué)中,分離關(guān)注(SoC)是一個(gè)過(guò)程將計(jì)算機(jī)程序分解為重疊的不同特征功能盡量少.一個(gè)關(guān)注是任何感興趣或?qū)W⒂谝粋€(gè)程序.通常,關(guān)注點(diǎn)是特征的同義詞或行為.SoC 的進(jìn)展是傳統(tǒng)上通過(guò)模塊化和封裝性信息隱藏的幫助."

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

在我看來(lái),我已經(jīng)做到了這一點(diǎn).我的用戶(hù)對(duì)象隱藏了它的所有信息.我的代碼中沒(méi)有任何地方在顯示之前說(shuō) $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.

然而,這似乎與其他人認(rèn)為的最佳實(shí)踐"背道而馳.

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

引用同一問(wèn)題中的選定"(綠色)答案:

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

"關(guān)注點(diǎn)分離是保持每個(gè)問(wèn)題的代碼分離.改變界面不應(yīng)該要求改變業(yè)務(wù)邏輯代碼,反之亦然.模型-視圖-控制器 (MVC) 設(shè)計(jì)模式是一個(gè)很好的例子更好地分離這些問(wèn)題軟件可維護(hù)性."

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

為什么這會(huì)提高軟件的可維護(hù)性?當(dāng)然,對(duì)于 MVC,我的視圖必須非常了解模型?閱讀 JavaWorld 文章以詳細(xì)討論這一點(diǎn):為面向?qū)ο笙到y(tǒng)構(gòu)建用戶(hù)界面

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

不管怎樣……總算說(shuō)到點(diǎn)子上了!

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

1. 誰(shuí)能推薦一本詳細(xì)討論這個(gè)問(wèn)題的書(shū)?我不想要一本 MVC 的書(shū);我不賣(mài) MVC.我想要一本討論 OO/UI、潛在問(wèn)題、潛在解決方案等的書(shū).(可能包括 MVC)Arthur Riel 的面向?qū)ο蟮脑O(shè)計(jì)啟發(fā)式

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

觸及它(也是一本很棒的書(shū)!),但我想要更詳細(xì)的東西.

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

2. 誰(shuí)能提出一個(gè)像 Allen Holub 的 JavaWorld 文章那樣解釋清楚的論點(diǎn),解釋為什么 MVC 是一個(gè)好主意?

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?

非常感謝任何能幫助我得出結(jié)論的人.

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

推薦答案

這是 OOP 教學(xué)經(jīng)常失敗的地方,使用了像 rectangle.draw() 和恐龍.show() 這樣的例子,這些例子完全沒(méi)有意義.

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

當(dāng)您談?wù)摀碛酗@示自身的用戶(hù)類(lèi)時(shí),您幾乎是在回答自己的問(wèn)題.

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

以后,如果我需要調(diào)整顯示他們的名字+小頭像,我可以只更新這一種方法,嘿嘿,我的應(yīng)用程序更新了."

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

暫時(shí)只考慮那一小塊.現(xiàn)在看看 Stack Overflow 并注意您的用戶(hù)名出現(xiàn)的所有位置.在每種情況下看起來(lái)都一樣嗎?不,在頂部,您的用戶(hù)名旁邊有一個(gè)信封,后面是您的聲譽(yù)和徽章.在一個(gè)問(wèn)題線程中,您的頭像后面是您的用戶(hù)名,下面是您的聲譽(yù)和徽章.你認(rèn)為有一個(gè)用戶(hù)對(duì)象有像 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.

一個(gè)對(duì)象與它所代表的數(shù)據(jù)和作用于該數(shù)據(jù)的方法有關(guān).您的用戶(hù)對(duì)象可能具有 firstName 和 lastName 成員,以及檢索這些部分所需的 getter.它也可能有一個(gè)像 toString()(在 Java 術(shù)語(yǔ)中)這樣的便捷方法,它會(huì)以一種通用格式返回用戶(hù)名,比如名字后跟一個(gè)空格,然后是姓氏.除此之外,用戶(hù)對(duì)象不應(yīng)該做太多其他事情.由客戶(hù)決定要對(duì)對(duì)象做什么.

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.

以您為我們提供的用戶(hù)對(duì)象為例,然后考慮如果您在其中構(gòu)建了UI",您將如何執(zhí)行以下操作:

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. 創(chuàng)建顯示所有用戶(hù)的 CSV 導(dǎo)出,按姓氏排序.例如.姓氏,名字.
  2. 提供重量級(jí)的 GUI 和基于 Web 的界面來(lái)處理用戶(hù)對(duì)象.
  3. 在一處顯示用戶(hù)名旁邊的頭像,而在另一處僅顯示用戶(hù)名.
  4. 提供用戶(hù)的 RSS 列表.
  5. 在一處顯示用戶(hù)名粗體,在另一處顯示為斜體,并在另一處顯示為超鏈接.
  6. 在適當(dāng)?shù)牡胤斤@示用戶(hù)的中間名首字母.

如果你考慮一下這些需求,它們都?xì)w結(jié)為提供一個(gè)用戶(hù)對(duì)象,它只關(guān)心它應(yīng)該關(guān)心的數(shù)據(jù).它不應(yīng)該試圖對(duì)每個(gè)人都適用,它應(yīng)該只是提供一種獲取用戶(hù)數(shù)據(jù)的方法.您將創(chuàng)建的多個(gè)視圖中的每一個(gè)都決定如何顯示用戶(hù)數(shù)據(jù).

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.

您關(guān)于在一處更新代碼以在多處更新您的視圖的想法是個(gè)好主意.這仍然是可能的,而無(wú)需在太低的水平上搞砸.您當(dāng)然可以創(chuàng)建類(lèi)似小部件的類(lèi)來(lái)封裝東西"的各種常見(jiàn)視圖,并在整個(gè)視圖代碼中使用它們.

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.

這篇關(guān)于關(guān)注點(diǎn)分離;MVC;為什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動(dòng)作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類(lèi)/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應(yīng)用程序中的所有視圖都可以訪問(wèn)?) - IT屋-程序員軟件開(kāi)發(fā)技術(shù)
Having a single entry point to a website. Bad? Good? Non-issue?(有一個(gè)網(wǎng)站的單一入口點(diǎn).壞的?好的?沒(méi)問(wèn)題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務(wù)層在 Zend 或 PHP 中常見(jiàn)嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 不卡视频一区二区 | 欧美在线一区二区 | 久久艳片www.17c.com | 久久综合一区 | 中文字幕手机在线观看 | txvlog.com | 狠狠狠狠干 | 四虎wz | 欧美激情一区 | 香蕉网在线 | 99视频免费在线观看 | 欧美在线视频一区二区 | 激情av网 | 中文字幕第2页 | 99在线视频免费观看 | 操日本老女人 | 亚洲伊人影院 | 日韩av在线不卡 | 日韩av在线不卡 | 国产精品剧情 | 小sao货撅起屁股扒开c微博 | 亚洲精品a | 欧美精品在线视频 | 波多野结衣乳巨码无在线观看 | 成人黄性视频 | 人人干人人看 | 国产成人在线观看免费网站 | 黄色国产精品 | 嫩草视频在线观看 | 午夜视频福利 | 成人毛片在线 | 一级片在线播放 | 一级片免费网站 | 五月天综合网 | 久久久久亚洲 | 亚洲一区二区三区在线播放 | 午夜视频免费观看 | 日本欧美在线 | 亚洲成人国产 | 婷婷国产| 日本在线视频一区二区 |