📄 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 + -