📄 lib0110.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Component Usage Guidelines</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0109.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0111.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch16"></a>
<div class="section">
<h2 class="first-section-title"><a name="531"></a><a name="ch16lev1sec5"></a>Component Usage Guidelines</h2><p class="first-para">Whether a third-party component is open source or not, one of your goals should be minimizing business risk. A large part of that is keeping switching costs small so that if a mistake is made, it will be easier to correct.</p>
<p class="para">
<b class="bold">Minimize the classes directly using third-party components.</b> I typically accomplish this by using a proxy pattern. For example, consider a Java FTP <a name="532"></a><a name="IDX-220"></a>client. I mentioned previously that I switched out this component for one with lower memory requirements. I was able to switch components easily because most of my application did not use the FTP component directly. One class in my application called <span class="fixed">FtpClient</span> performed all FTP requests.</p>
<p class="para">Since <span class="fixed">FtpClient</span> was the only class that used the open source component, I could switch products easily with no impact to the rest of the application. <a class="internaljump" href="#ch16list04">Listing 16.4</a> shows the source for this client.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 16.4: </span>Sample Component Delegate</span><a name="533"></a><a name="ch16list04"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1:import com.enterprisedt.net.ftp.*;
2:
3:public class FtpClient
4:{
5: public FtpClient ( String hostName,
6: String userName,
7: String password)
8: throws ApplicationException
9: {
10: // arg check omitted.
11: try {
12: _ftpClient = new FTPClient(hostName);
13: _ftpClient.login(userName, password);
14: _ftpClient.setTimeout(DEFAULT_TIMEOUT_IN_MILLIS);
15: _ftpClient.setConnectMode(FTPConnectMode.ACTIVE);
16: }
17: catch (Throwable e)
18: {
19: throw new ApplicationException(
20: "Error creating FTP client. ", e);
21: }
22: }
23:
24: public void put( String localFileName,
25: String hostFileName)
26: throws ApplicationException
27: {
28: // arg check omitted
29: try{_ftpClient.put(localFileName, hostFileName);}
30: catch (Throwable e)
31: {
32: throw new ApplicationException(
33: "Error with FTP put: local=" + localFileName +
34: ", remote=" + hostFileName, e);
35: }
36: }
37:<a name="534"></a><a name="IDX-221"></a>
38: public void get(String hostFileName)
39: throws ApplicationException
40: {
41: // arg check omitted
42: try{_ftpClient.get(hostFileName);}
43: catch (Throwable e)
44: {
45: throw new ApplicationException(
46: "Error with FTP get: file=" + hostFileName, e);
47: }
48:
49: private FTPClient _ftpClient = null;
50: }
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">You can use this technique with virtually any software component. Remember that you also must front any component-based exception so that your application relies on internal classes, not external ones.</p>
<a name="535"></a><a name="IDX-222"></a>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0109.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0111.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -