Skip to Content
JavaScript 設計模式
book

JavaScript 設計模式

by Stoyan Stefanov
March 2012
Intermediate to advanced
244 pages
4h 42m
Chinese
GoTop Information, Inc.
Content preview from JavaScript 設計模式
設計模式
   
173
Proxy 作為快取
在這個範例中,proxy 的客戶物件(
videos
)夠聰明而不會對同一個影音資訊請求第二
遍。但事情可不會總是這樣。而 proxy 可以將之前的請求結果儲存在一個快取屬性中,藉
此更進一步保護真正的對象
http
(請見圖 7-6)。那麼如果
videos
物件再次要求同一份
影音 ID 的資訊,
proxy
可以從快取中取出資訊並省下一次網路的往返。
7-6 Proxy 的快取
中介者模式(Mediator
應用程式—不論大小—都是由分開的物件所組成。這些所有的物件都需要一種方式彼此溝
通,且這方法不能損害可維護性,必須讓你可以安全的改變其中一部分,而不會把其他的
部分都搞壞。隨著應用程式的成長,你增加了越來越多物件,接著在重構的過程中,物件
被移來移去、重新整理。倘若物件認識太多其他的物件,又直接和這些物件溝通(例如
呼叫其他物件的方法或改變其屬性),就會導致我們所不希望看到的,緊密的耦合tight
coupling)。當物件緊密耦合時,要改變一個物件而不影響其他就會變得困難。接著就會
演變成,甚至在應用程式中最簡單的改變,也會變得一點都不單純,於是幾乎不可能在改
變前推估應該花費的時間。[167]
Mediator 模式可以減輕這種惡劣的情況,促使降低耦合性loose coupling),並幫助提
升可維護性。以圖 7-7 的範例來說,此模式可
以讓各個獨立物件(指 colleagues)不直接
相互溝通,而是透過一個 mediator 物件
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript 之美

JavaScript 之美

Anton Kovalyov
重構 JavaScript

重構 JavaScript

Evan Burchard

Publisher Resources

ISBN: 9789862764190