?? futurepattern.htm
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>Future 模式</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">Design Pattern: Future 模式</a></h1>
Future模式可以簡單的看成是 <a href="ProxyPattern.htm">Proxy 模式</a> 與 <a href="ThreadPerMessage.htm">Thread-Per-Message 模式</a>
的結合,在Proxy模式中,用一個Proxy來代替真正的目標(Subject)生成,目標的生成可能是費時的,例如在開啟一個內嵌圖片的文件中,希望
程式能盡快完成開啟文件的動作,并顯示一個可接受的畫面給使用者看,在還不需要看到圖片的頁面中先使用Proxy代替真正的圖片載入,只有在真正需要看到
圖片時,才由Proxy物件載入真正的圖片。<br>
<br>
考慮這樣一個情況,使用者可能快速翻頁瀏覽文件中,而圖片檔案很大,如此在瀏覽到有圖片的頁數時,就會導致圖片的載入,因而造成使用者瀏覽文件時會有停頓
的現象,所以我們希望在文件開啟之后,仍有一個背景作業持續載入圖片,如此使用者在快速瀏覽頁面時,所造成的停頓可以獲得改善。<br>
<br>
Future模式在請求發生時,會先產生一個Future物件給發出請求的客戶,它的作用就像是Proxy物件,而同時間,真正的目標物件之生成,由一個
新的執行緒持續進行(即Thread-Per-Message),真正的目標物件生成之后,將之設定至Future之中,而當客戶端真正需要目標物件時,
目標物件也已經準備好,可以讓客戶提取使用。 <br>
<br>
<div style="text-align: center;"><img style="width: 642px; height: 545px;" alt="Future" title="Future" src="images/future-1.jpg"><br>
</div>
<br>
一個簡單的Java程式片段示范可能像是這樣:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">....</span></div>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public Future request() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> final Future future = new Future();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> new Thread() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public void run() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> // 下面這個動作可能是耗時的</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> RealSubject subject = new RealSubject();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> future.setRealSubject(subject);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }.start();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> return future;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br>
</div>
<br>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -