﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-后羿射日-文章分类-JAVA</title><link>http://www.blogjava.net/youthyflyer/category/978.html</link><description>在Eclipse的海洋中一口接一口的喝水~~</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 03:46:06 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 03:46:06 GMT</pubDate><ttl>60</ttl><item><title>[转载][翻译]IAdaptable是什么？ </title><link>http://www.blogjava.net/youthyflyer/articles/19934.html</link><dc:creator>youthyflyer</dc:creator><author>youthyflyer</author><pubDate>Tue, 15 Nov 2005 10:14:00 GMT</pubDate><guid>http://www.blogjava.net/youthyflyer/articles/19934.html</guid><wfw:comment>http://www.blogjava.net/youthyflyer/comments/19934.html</wfw:comment><comments>http://www.blogjava.net/youthyflyer/articles/19934.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/youthyflyer/comments/commentRss/19934.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/youthyflyer/services/trackbacks/19934.html</trackback:ping><description><![CDATA[<DIV class=postText>
<P>转载地址：<A href="http://bjzhanghao.cnblogs.com/archive/2005/09/24/243312.html">http://bjzhanghao.cnblogs.com/archive/2005/09/24/243312.html</A><BR><BR>原文地址：<A href="http://www.eclipsezone.com/articles/what-is-iadaptable/"><FONT color=#000080>http://www.eclipsezone.com/articles/what-is-iadaptable/</FONT></A><BR></P>
<P><A href="http://help.eclipse.org/help31/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime/IAdaptable.html"><FONT color=#000080>IAdaptable</FONT></A>在Eclipse里是一个非常重要的接口。对于Eclipse开发老手来说，它就像异常处理和抽象类一样寻常；但是对新手而言，它却令人感到困惑和畏惧。这篇文章将向你解释IAdaptable到底是什么，以及它在Eclipse里起到的作用。</P>
<H3>类型转换</H3>
<P>Java是所谓的强类型语言，也就是说，每个实例都对应一个类型。其实类型分为两种：声明类型和运行时类型（也分别被称为静态类型和动态类型）。像Python这样的弱类型语言常被称为无类型的语言，其实严格说来不是这样，因为每个实例都对应一个运行时类型，只是你并不需要声明这一点而已。</P>
<P>现在回到Java，为了能够执行一个类的某个方法，这个方法必须在声明类型中可见，换句话说，即使在运行时实例是某个子类型，你也只能执行那些父类型里定义的方法。</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top><SPAN style="COLOR: #000000">List&nbsp;list&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList(); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top>list.add(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">data</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;正确，add是List里定义的方法</SPAN><SPAN style="COLOR: #008000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN><SPAN style="COLOR: #000000">list.ensureCapacity(</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;不正确，ensureCapacity()只在ArrayList被定义 <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<P>如果一定要执行特定类型的方法，我们必须先强制转换这个实例到正确的类型。对于上面的例子，我们可以将list转换为ArrayList（译注：原文In this case, we can cast ArrayList to List，怀疑是笔误），因为ArrayList实现了List接口，你甚至可以在运行时通过instanceof关键字检验list是否为ArrayList的一个实例。</P>
<H3>可扩展的接口</H3>
<P>不幸的是，一个类可能并没有实现你需要的接口，这样就无法进行强制类型转换了。原因有很多，比如只在少数情况下才需要这个接口，或者你需要的接口是在另一个不相关的库里，又或者接口是有了类以后才开发出来的，等等。</P>
<P>这时你就需要IAdaptable了。可以把IAdaptable想象为一个能够动态进行类型转换的途径。对比下面的直接类型转换：</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top><SPAN style="COLOR: #000000">Object&nbsp;o&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList(); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top>List&nbsp;list&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(List)o; <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<P>换一种方式，我们可以这样做：</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top><SPAN style="COLOR: #000000">IAdaptable&nbsp;adaptable&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList();</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">译注：这里的ArrayList应该不是指java.util.ArrayList</SPAN><SPAN style="COLOR: #008000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN><SPAN style="COLOR: #000000">List&nbsp;list&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(List)adaptable.getAdapter(java.util.List.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<P>这就是上面所说的动态类型转换，我们所做的事情是试图把adaptable转换为一个List实例。</P>
<P>那么，当可以直接转换的时候为什么要费这个力气通过getAdapter()来转换呢？其实这种机制可以让我们将目标类转换为它并没有实现的接口。举个例子，我们可能想把一个HashMap当作List来用，尽管这两个类的性质并不相同，可以这么做：</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top><SPAN style="COLOR: #000000">IAdaptable&nbsp;adaptable&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;HashMap();</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">译注：这里的HashMap应该不是指java.util.HashMap</SPAN><SPAN style="COLOR: #008000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN><SPAN style="COLOR: #000000">List&nbsp;list&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(List)adaptable.getAdapter(java.util.List.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<H3>实现IAdaptable接口</H3>
<P>大部分IAdaptable的实现是一些if语句的叠加，比如我们现在要实现HashMap的getAdapter()方法，它看起来可能是这样：</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_43_263_Open_Image onclick="this.style.display='none'; Codehighlighter1_43_263_Open_Text.style.display='none'; Codehighlighter1_43_263_Closed_Image.style.display='inline'; Codehighlighter1_43_263_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_43_263_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_43_263_Closed_Text.style.display='none'; Codehighlighter1_43_263_Open_Image.style.display='inline'; Codehighlighter1_43_263_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;HashMap&nbsp;</SPAN><SPAN style="COLOR: #0000ff">implements</SPAN><SPAN style="COLOR: #000000">&nbsp;IAdaptable&nbsp;</SPAN><SPAN id=Codehighlighter1_43_263_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_43_263_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG id=Codehighlighter1_85_252_Open_Image onclick="this.style.display='none'; Codehighlighter1_85_252_Open_Text.style.display='none'; Codehighlighter1_85_252_Closed_Image.style.display='inline'; Codehighlighter1_85_252_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_85_252_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_85_252_Closed_Text.style.display='none'; Codehighlighter1_85_252_Open_Image.style.display='inline'; Codehighlighter1_85_252_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Object&nbsp;getAdapter(Class&nbsp;clazz)&nbsp;</SPAN><SPAN id=Codehighlighter1_85_252_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_85_252_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG id=Codehighlighter1_126_231_Open_Image onclick="this.style.display='none'; Codehighlighter1_126_231_Open_Text.style.display='none'; Codehighlighter1_126_231_Closed_Image.style.display='inline'; Codehighlighter1_126_231_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_126_231_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_126_231_Closed_Text.style.display='none'; Codehighlighter1_126_231_Open_Image.style.display='inline'; Codehighlighter1_126_231_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(clazz&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;java.util.List.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_126_231_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_126_231_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&nbsp;list&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.size()); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.addAll(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.values()); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;list; <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">; <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;<IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN style="COLOR: #008000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" width=11 align=top></SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<P>所做的就是返回一个适配器（adapter，更确切的说是一个副本），而不是进行直接的类型转换。如果参数类型没有被支持，惯例是返回null值（而非抛出异常），代表这个方法失败了。因此，在调用这个方法时，不应该假定它总是返回非null值。</P>
<H3>PlatformObject</H3>
<P>当然，如果你希望增加一个新的被支持的adapter类型时必须编辑这个类才行（译注：在getAdapter()里增加更多的if语句），这会比较辛苦。而且，既然你已经知道了这个类型，何不直接修改接口声明呢？其实有很多原因使得你并不希望直接编辑这个类（例如更容易保持向下兼容性），也不想改变它的类型（HashMap虽然不是一个List，但可以转换过去）。</P>
<P>Eclipse通过PlatformObject抽象类来解决以上问题，它为你实现了IAdaptable接口，Eclipse平台（Platform）提供了IAdapterManager的一个实现，并且可以通过Platform.getAdapterManager()访问到，它把所有对getAdapter()的请求（调用）委托给一个名为IAdapterManager的东西。你可以将它想象为一个巨大的保存着类和adapter信息的Map，而PlatformObject的getAdapter()方法会查找这个Map。</P>
<H3>适配已存在的类</H3>
<P>这样，PlatformObject不需要重新编译就能够支持新的adapter类型，这一点在Eclipse里被大量使用以支持workspace的扩展点。</P>
<P>现在假设我们想要将一个只包含String类型元素的List转换为一个XMl节点，这个节点的格式如下：</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">List</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Entry</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">First&nbsp;String</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">Entry</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Entry</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">Second&nbsp;String</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">Entry</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Entry</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">Third&nbsp;String</SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">Entry</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN><SPAN style="COLOR: #000000">&lt;/</SPAN><SPAN style="COLOR: #000000">List</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<P>因为toString()方法可能有其他用途，我们不能通过覆盖toString()方法来实现这个功能。所以，我们要给List关联一个工厂类以处理XML节点类型的适配请求。要管理工厂类需要以下三个步骤：</P>
<P>1、由List生成一个Node，我们把这个转换过程用IAdapterFactory包装起来：</P>
<P></P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;nu.xom.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">; <BR><IMG id=Codehighlighter1_73_644_Open_Image onclick="this.style.display='none'; Codehighlighter1_73_644_Open_Text.style.display='none'; Codehighlighter1_73_644_Closed_Image.style.display='inline'; Codehighlighter1_73_644_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_73_644_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_73_644_Closed_Text.style.display='none'; Codehighlighter1_73_644_Open_Image.style.display='inline'; Codehighlighter1_73_644_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;NodeListFactory&nbsp;</SPAN><SPAN style="COLOR: #0000ff">implements</SPAN><SPAN style="COLOR: #000000">&nbsp;IAdapterFactory&nbsp;</SPAN><SPAN id=Codehighlighter1_73_644_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_73_644_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG id=Codehighlighter1_77_90_Open_Image onclick="this.style.display='none'; Codehighlighter1_77_90_Open_Text.style.display='none'; Codehighlighter1_77_90_Closed_Image.style.display='inline'; Codehighlighter1_77_90_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_77_90_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_77_90_Closed_Text.style.display='none'; Codehighlighter1_77_90_Open_Image.style.display='inline'; Codehighlighter1_77_90_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_77_90_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_77_90_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;可以转换到的类型&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG id=Codehighlighter1_131_151_Open_Image onclick="this.style.display='none'; Codehighlighter1_131_151_Open_Text.style.display='none'; Codehighlighter1_131_151_Closed_Image.style.display='inline'; Codehighlighter1_131_151_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_131_151_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_131_151_Closed_Text.style.display='none'; Codehighlighter1_131_151_Open_Image.style.display='inline'; Codehighlighter1_131_151_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">final</SPAN><SPAN style="COLOR: #000000">&nbsp;Class[]&nbsp;types&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_131_151_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_131_151_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;Node.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">, <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">; <BR><IMG id=Codehighlighter1_188_210_Open_Image onclick="this.style.display='none'; Codehighlighter1_188_210_Open_Text.style.display='none'; Codehighlighter1_188_210_Closed_Image.style.display='inline'; Codehighlighter1_188_210_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_188_210_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_188_210_Closed_Text.style.display='none'; Codehighlighter1_188_210_Open_Image.style.display='inline'; Codehighlighter1_188_210_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Class[]&nbsp;getAdapterList()&nbsp;</SPAN><SPAN id=Codehighlighter1_188_210_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_188_210_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;types; <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG id=Codehighlighter1_214_231_Open_Image onclick="this.style.display='none'; Codehighlighter1_214_231_Open_Text.style.display='none'; Codehighlighter1_214_231_Closed_Image.style.display='inline'; Codehighlighter1_214_231_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_214_231_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_214_231_Closed_Text.style.display='none'; Codehighlighter1_214_231_Open_Image.style.display='inline'; Codehighlighter1_214_231_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_214_231_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_214_231_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;转换到Node的功能代码&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG id=Codehighlighter1_286_642_Open_Image onclick="this.style.display='none'; Codehighlighter1_286_642_Open_Text.style.display='none'; Codehighlighter1_286_642_Closed_Image.style.display='inline'; Codehighlighter1_286_642_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_286_642_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_286_642_Closed_Text.style.display='none'; Codehighlighter1_286_642_Open_Image.style.display='inline'; Codehighlighter1_286_642_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Object&nbsp;getAdapter(Object&nbsp;list,&nbsp;Class&nbsp;clazz)&nbsp;</SPAN><SPAN id=Codehighlighter1_286_642_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_286_642_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG id=Codehighlighter1_341_606_Open_Image onclick="this.style.display='none'; Codehighlighter1_341_606_Open_Text.style.display='none'; Codehighlighter1_341_606_Closed_Image.style.display='inline'; Codehighlighter1_341_606_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_341_606_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_341_606_Closed_Text.style.display='none'; Codehighlighter1_341_606_Open_Image.style.display='inline'; Codehighlighter1_341_606_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(clazz&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;Node.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;list&nbsp;</SPAN><SPAN style="COLOR: #0000ff">instanceof</SPAN><SPAN style="COLOR: #000000">&nbsp;List)&nbsp;</SPAN><SPAN id=Codehighlighter1_341_606_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_341_606_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;root&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Element(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">List</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Iterator&nbsp;it&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;list.iterator(); <BR><IMG id=Codehighlighter1_448_581_Open_Image onclick="this.style.display='none'; Codehighlighter1_448_581_Open_Text.style.display='none'; Codehighlighter1_448_581_Closed_Image.style.display='inline'; Codehighlighter1_448_581_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_448_581_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_448_581_Closed_Text.style.display='none'; Codehighlighter1_448_581_Open_Image.style.display='inline'; Codehighlighter1_448_581_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">(it.hasNext())&nbsp;</SPAN><SPAN id=Codehighlighter1_448_581_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_448_581_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element&nbsp;item&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Element(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Entry</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.appendChild(it.next().toString()); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root.appendChild(item); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;root; <BR><IMG id=Codehighlighter1_613_638_Open_Image onclick="this.style.display='none'; Codehighlighter1_613_638_Open_Text.style.display='none'; Codehighlighter1_613_638_Closed_Image.style.display='inline'; Codehighlighter1_613_638_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_613_638_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_613_638_Closed_Text.style.display='none'; Codehighlighter1_613_638_Open_Image.style.display='inline'; Codehighlighter1_613_638_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_613_638_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_613_638_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">; <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" width=11 align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<P>2、把这个工厂类注册到Platform的AdapterManager，这样当我们希望从List的实例中获得一个Node实例时，就会找到我们的工厂类。注册一个工厂类的方式也很简单：</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top><SPAN style="COLOR: #000000">Platform.getAdapterManager().registerAdapters( <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;NodeListFactory(),&nbsp;List.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top>); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<P>这条语句将NodeListFactory关联到List类型。当从List里请求adapter时，Platform的AdapterManager会找到NodeListFactory，因为在后者的getAdapterList()方法的返回结果里包含了Node类，所以它知道从List实例得到一个Node实例是可行的。在Eclipse里，这个注册步骤一般是在plugin启动时完成的，但也可以通过org.eclipse.core.runtime.adapters扩展点来完成。</P>
<P>3、从List获得Node，下面是例子代码:</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_34_182_Open_Image onclick="this.style.display='none'; Codehighlighter1_34_182_Open_Text.style.display='none'; Codehighlighter1_34_182_Closed_Image.style.display='inline'; Codehighlighter1_34_182_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_34_182_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_34_182_Closed_Text.style.display='none'; Codehighlighter1_34_182_Open_Image.style.display='inline'; Codehighlighter1_34_182_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><SPAN style="COLOR: #000000">Node&nbsp;getNodeFrom(IAdaptable&nbsp;list)&nbsp;</SPAN><SPAN id=Codehighlighter1_34_182_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_34_182_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;Object&nbsp;adaptable&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;list.getAdapter(Node.</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">); <BR><IMG id=Codehighlighter1_111_165_Open_Image onclick="this.style.display='none'; Codehighlighter1_111_165_Open_Text.style.display='none'; Codehighlighter1_111_165_Closed_Image.style.display='inline'; Codehighlighter1_111_165_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_111_165_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_111_165_Closed_Text.style.display='none'; Codehighlighter1_111_165_Open_Image.style.display='inline'; Codehighlighter1_111_165_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(adaptable&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_111_165_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_111_165_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;Node&nbsp;node&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(Node)adaptable; <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;node; <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">; <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" width=11 align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<H3>总结</H3>
<P>综上所述，要在运行时为一个已有的类增加功能，所要做的只是定义一个用来转换的工厂类，然后把它注册到Platform的AdapterManager即可。这种方式在保持UI组件和非UI组件的分离方面特别有用。例如在org.rcpapps.rcpnews.ui和org.rcpapps.rcpnews这两个plugin里，前者的IPropertySource需要与后者的数据对象（data object）相关联，当前者初始化时，它将IPropertySource注册到Platform，当数据对象在导航器（navigator）里被选中的时候，属性视图里就会显示正确的属性。</P>
<P>显然，java.util.List并不是PlatformObject的子类，所以如果你希望能够编译这里所说的例子，必须建立一个List的子类型。注意，可以直接实现IAdaptable接口，而非必须继承PlatformObject抽象类。</P>
<P></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_55_279_Open_Image onclick="this.style.display='none'; Codehighlighter1_55_279_Open_Text.style.display='none'; Codehighlighter1_55_279_Closed_Image.style.display='inline'; Codehighlighter1_55_279_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_55_279_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_55_279_Closed_Text.style.display='none'; Codehighlighter1_55_279_Open_Image.style.display='inline'; Codehighlighter1_55_279_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;AdaptableList&nbsp;</SPAN><SPAN style="COLOR: #0000ff">implements</SPAN><SPAN style="COLOR: #000000">&nbsp;IAdaptable,&nbsp;List&nbsp;</SPAN><SPAN id=Codehighlighter1_55_279_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_55_279_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG id=Codehighlighter1_99_171_Open_Image onclick="this.style.display='none'; Codehighlighter1_99_171_Open_Text.style.display='none'; Codehighlighter1_99_171_Closed_Image.style.display='inline'; Codehighlighter1_99_171_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_99_171_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_99_171_Closed_Text.style.display='none'; Codehighlighter1_99_171_Open_Image.style.display='inline'; Codehighlighter1_99_171_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Object&nbsp;getAdapter(Class&nbsp;adapter)&nbsp;</SPAN><SPAN id=Codehighlighter1_99_171_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_99_171_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;Platform.getAdapterManager().getAdapter(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">,&nbsp;adapter); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;List&nbsp;delegate&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList(); <BR><IMG id=Codehighlighter1_236_268_Open_Image onclick="this.style.display='none'; Codehighlighter1_236_268_Open_Text.style.display='none'; Codehighlighter1_236_268_Closed_Image.style.display='inline'; Codehighlighter1_236_268_Closed_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" width=11 align=top><IMG id=Codehighlighter1_236_268_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_236_268_Closed_Text.style.display='none'; Codehighlighter1_236_268_Open_Image.style.display='inline'; Codehighlighter1_236_268_Open_Text.style.display='inline';" height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;size()&nbsp;</SPAN><SPAN id=Codehighlighter1_236_268_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN id=Codehighlighter1_236_268_Open_Text><SPAN style="COLOR: #000000">{ <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;delegate.size(); <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" width=11 align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/InBlock.gif" width=11 align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;<IMG height=20 src="http://bjzhanghao.cnblogs.com/Images/dot.gif" width=15></SPAN><SPAN style="COLOR: #008000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" width=11 align=top></SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000"> <BR><IMG height=16 src="http://bjzhanghao.cnblogs.com/Images/OutliningIndicators/None.gif" width=11 align=top></SPAN> </DIV>
<P>最后，例子里生成XML的部分使用了<A href="http://www.xom.nu/"><FONT color=#000080>XOM</FONT></A>的类库.</P></DIV><img src ="http://www.blogjava.net/youthyflyer/aggbug/19934.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/youthyflyer/" target="_blank">youthyflyer</a> 2005-11-15 18:14 <a href="http://www.blogjava.net/youthyflyer/articles/19934.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>