📄 +
字号:
<html><!-- #BeginTemplate "/Templates/module.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" -->
<title> 设计模式之Flyweight</title>
<!-- #EndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../jdon.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="http://www.jdon.com/favicon.ico">
<meta http-equiv="refresh" content="1200">
</head>
<body bgcolor="#014E82" text="#000000">
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="129" rowspan="3" align="center" valign="bottom" bgcolor="#FFFFFF">
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td align="center" >
<img src="http://www.jdon.com/images/jdon.gif" width="120" height="60"></td>
</tr>
<tr>
<td height="9"></td>
</tr>
</table>
</td>
<td valign="bottom" >
<table cellpadding="0" cellspacing="0" height="100%" width="100%" >
<tr bgcolor="#CAC9BB">
<td height="70" align="right" valign="bottom" bgcolor="#CAC9BB">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="619" height="60">
<param name="movie" value="../images/jd.swf">
<param name="quality" value="high"><param name="BGCOLOR" value="#CAC9BB">
<embed src="../images/jd.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="619" height="60"></embed>
</object></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" >
<tr>
<td height="26" bgcolor="#707070" width="222"> </td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/index.html" class="a03">首 页</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/design.htm" class="a03">设计研究</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/product.htm" class="a03">构件产品</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/trainning.htm" class="a03">培训咨询</a></div></td>
<td height="26" width="70" bgcolor="#707070"> <div align="center" class="unnamed2"><a href="http://www.jdon.com/jdonframework/" class="a03">Jdon框架</a></div></td>
<td height="26" width="70" bgcolor="#707070"> <div align="center" class="unnamed2"><a href="http://www.jdon.com/jive/index.jsp" class="a03">论坛</a></div></td>
</tr></table>
</td>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="11" colspan="7" bgcolor="#FFFFFF" class="bianxian">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="1" bordercolor="#000000">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="2" bgcolor="#000000">
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="130" valign="top" bgcolor="#FFFFD7"><table width="100%" border="0" cellspacing="0" cellpadding="3" height="200" align="center">
<tr>
<td align="left" valign="top"><!-- #BeginEditable "Menu" --> <!-- #EndEditable --> </td>
</tr>
<tr>
<td align="right">
<p><a href="http://www.jdon.com/trainning/jiaocheng.htm" target="_blank"><font color="#FF9999"><strong>在线教程</strong></font><br>
<font color="#555555"> 原创教程区</font></a></p>
<p><a href="http://www.jdon.com/jdonframework/app.htm" target="_blank"><font color="#FF9999"><strong>Jdon框架</strong></font><br>
<font color="#555555"> 在线运行演示</font></a></p>
<br>
<div id=vgad ></div>
<!-- #BeginEditable "Menu2" --> <!-- #EndEditable -->
</td>
</tr>
</table>
</td>
<td width="1" height="100%" >
<table height="100%" cellpadding="0" cellspacing="0" background="../images/blackpoint.gif" >
<tr><td> <img name="" src="" width="1" height="1" alt="" ></td></tr></table>
</td >
<td valign="top" >
<table width="100%" border="0" cellspacing="13" cellpadding="2">
<tr>
<td class="article"><!-- #BeginEditable "main" -->
<h3 align="center">设计模式之Flyweight(享元) FlyWeight模式</h3>
<p align="center"><a href="http://www.jdon.com/aboutme.htm">板桥里人</a> http://www.jdon.com 2002/05/02</p>
<p align="center"><a href="http://www.jdon.com/trainning/sh_trainning.htm" target="_blank"><strong>点按这里参加每月举办的设计模式讲座</strong></a></p>
<p><b>Flyweight模式定义:</b><br>
避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类).</p>
<p><b>为什么使用?</b><br>
面向对象语言的原则就是一切都是对象,但是如果真正使用起来,有时对象数可能显得很庞大,比如,字处理软件,如果以每个文字都作为一个对象,几千个字,对象数就是几千,无疑耗费内存,那么我们还是要"求同存异",找出这些对象群的共同点,设计一个元类,封装可以被共享的类,另外,还有一些特性是取决于应用(context),是不可共享的,这也Flyweight中两个重要概念内部状态intrinsic和外部状态extrinsic之分.</p>
<p>说白点,就是先捏一个的原始模型,然后随着不同场合和环境,再产生各具特征的具体模型,很显然,在这里需要产生不同的新对象,所以Flyweight模式中常出现Factory模式.Flyweight的内部状态是用来共享的,Flyweight
factory负责维护一个Flyweight pool(模式池)来存放内部状态的对象.</p>
<p>Flyweight模式是一个提高程序效率和性能的模式,会大大加快程序的运行速度.应用场合很多:比如你要从一个数据库中读取一系列字符串,这些字符串中有许多是重复的,那么我们可以将这些字符串储存在Flyweight池(pool)中.</p>
<p><b>如何使用?</b><br>
</p>
<p>我们先从Flyweight抽象接口开始:<br>
</p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC">
<p>public interface Flyweight <br>
{<br>
public void operation( ExtrinsicState state );<br>
}</p>
<p>//用于本模式的抽象数据类型(自行设计)<br>
public interface ExtrinsicState { }</p>
</td>
</tr>
</table>
<p>下面是接口的具体实现(ConcreteFlyweight) ,并为内部状态增加内存空间, ConcreteFlyweight必须是可共享的,它保存的任何状态都必须是内部(intrinsic),也就是说,ConcreteFlyweight必须和它的应用环境场合无关.</p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC"> public class ConcreteFlyweight implements Flyweight
{<br>
private IntrinsicState state; <br>
<br>
public void operation( ExtrinsicState state ) <br>
{ <br>
//具体操作<br>
}
<p> } </p>
</td>
</tr>
</table>
<p>当然,并不是所有的Flyweight具体实现子类都需要被共享的,所以还有另外一种不共享的ConcreteFlyweight:</p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC">
<p>public class UnsharedConcreteFlyweight implements Flyweight {</p>
<p> public void operation( ExtrinsicState state ) { }</p>
<p> }</p>
</td>
</tr>
</table>
<p>Flyweight factory负责维护一个Flyweight池(存放内部状态),当客户端请求一个共享Flyweight时,这个factory首先搜索池中是否已经有可适用的,如果有,factory只是简单返回送出这个对象,否则,创建一个新的对象,加入到池中,再返回送出这个对象.池</p>
<p></p>
<table width="80%" border="0" cellspacing="3" cellpadding="3" bgcolor="#CCCCCC">
<tr>
<td>
<p>public class FlyweightFactory { <br>
//Flyweight pool<br>
private Hashtable flyweights = new Hashtable(); <br>
<br>
public Flyweight getFlyweight( Object key ) { <br>
</p>
<p> Flyweight flyweight = (Flyweight) flyweights.get(key); <br>
</p>
<p> if( flyweight == null ) {<br>
//产生新的ConcreteFlyweight<br>
flyweight = new ConcreteFlyweight(); <br>
flyweights.put( key, flyweight ); <br>
} <br>
</p>
<p> return flyweight; <br>
} <br>
} </p>
</td>
</tr>
</table>
<p>至此,Flyweight模式的基本框架已经就绪,我们看看如何调用:</p>
<p>FlyweightFactory factory = new FlyweightFactory(); <br>
Flyweight fly1 = factory.getFlyweight( "Fred" ); <br>
Flyweight fly2 = factory.getFlyweight( "Wilma" );<br>
......<br>
</p>
<p>从调用上看,好象是个纯粹的Factory使用,但奥妙就在于Factory的内部设计上.</p>
<p><b>Flyweight模式在XML等数据源中应用</b><br>
我们上面已经提到,当大量从数据源中读取字符串,其中肯定有重复的,那么我们使用Flyweight模式可以提高效率,以唱片CD为例,在一个XML文件中,存放了多个CD的资料.</p>
<p>每个CD有三个字段:<br>
1.出片日期(year)<br>
2.歌唱者姓名等信息(artist)<br>
3.唱片曲目 (title)</p>
<p>其中,歌唱者姓名有可能重复,也就是说,可能有同一个演唱者的多个不同时期 不同曲目的CD.我们将"歌唱者姓名"作为可共享的ConcreteFlyweight.其他两个字段作为UnsharedConcreteFlyweight.</p>
<p>首先看看数据源XML文件的内容:</p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC">
<p><br>
<?xml version="1.0"?><br>
<collection><br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -