?? adapterpattern2.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>Adapter 模式 - Class Adapter</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: Adapter 模式 - Class
Adapter</a></h1>
Adapter模式的另一種作法是Class Adapter模式,在這個模式下,Adapter直接繼承Adaptee(要引進的新類別),以擁有當中的成員及方法,在C++中的話可以這么作:<br>
<div style="text-align: center;"><img style="width: 343px; height: 217px;" alt="Adapter" title="Adapter" src="images/adapter-3.jpg"><br>
</div>
<br>
C++中可以多重繼承,但在Java中不行,所以在Java中若要采用Class Adapter,必須作點修改,一方面繼承Adaptee,一方面實作Target的介面:<br>
<div style="text-align: center;"><img style="width: 365px; height: 224px;" alt="Adapter" title="Adapter" src="images/adapter-4.jpg"><br>
<br>
<div style="text-align: left;">代碼的實現是這樣的: <br>
</div>
<div style="text-align: left;">
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public class Adapter extends Adaptee implements Target {</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;">}</span><br>
</div>
<br>
當然,這必須您原先的Target定義了共同的介面,所以Class Adapter在Java中適用的場合較少,事實上,也比較建議使用Object
Adapter,這樣的Adapter模式比較有彈性,例如,您可以在Adapter上加上個setter,以隨時可以抽換Adaptee。<br>
<br>
在Java中,Class Adapter的一個應用場合是達到多重繼承的效果,您一定在很多時候聽別人說,介面(interface)可以達到多重繼承的效果,這是怎么回事?<br>
<br>
其實要討論這個問題,首先您對于C++中多重繼承要先有認識,新手看了書說介面可以達到多重繼承,切莫人云亦云,尤其是沒有學過C++的新手們,如果您對
于C++多重繼承想要有所認識,請先看看 <a href="http://caterpillar.onlyfun.net/GossipCN/CppGossip/MultiInheritance1.html">多
重繼承(一)</a>與 <a href="http://caterpillar.onlyfun.net/GossipCN/CppGossip/MultiInheritance2.html">多
重繼承(二)</a>。<br>
<br>
Java不能多重繼承,但為何說Java中可以使用介面(interface)來達到多重繼承的效果,首先效果之一,就如 <a href="http://caterpillar.onlyfun.net/GossipCN/CppGossip/MultiInheritance2.html">多
重繼承(二)</a> 中描述的“
多重繼承時通常其中一個基底類別作為private實作體,而其它的用以表現完全的抽象介面。”,在Java中這個效果可以使用介面來達到,介面此時所扮
演的即 <a href="http://caterpillar.onlyfun.net/GossipCN/CppGossip/MultiInheritance2.html">多
重繼承(二)</a> 中的抽象類別,一個完全的抽象介面,這個效果的達成方式,如 <a href="http://caterpillar.onlyfun.net/GossipCN/JavaGossip-V1/InterfaceType.htm">介
面(interface)型態</a>
中所介紹的,您可以直接對應這兩個主題中的程式實作來了解,了解Java中如何使用介面(interface)來達到C++中所謂多重繼承的“一種”效
果。<br>
<br>
來看看另一個情況。<br>
<br>
如果有SomeClass類別與OtherClass類別,您想要SomeAndOther類別可以同時擁有SomeClass類別與
OtherClass類別中已定義好的操作,并可以進行多型操作,在C++中可以用多重繼承來達到,但在Java中顯然的無法使用多重繼承,怎么辦?您可
以在設計上先繞個彎,先使用兩個介面分別定義好SomeClass與OtherClass兩個類別的公開方法,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public interface ISome {</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 doSome();</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;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public interface IOther {</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 doOther();</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>
接著讓SomeClass與OtherClass分別實作兩個介面:<br>
<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">public class SomeClass implements
ISome {</span><br style="font-weight: bold;">
<span style="font-weight: bold;">
public void doSome() {</span><br style="font-weight: bold;">
<span style="font-weight: bold;">
....</span><br style="font-weight: bold;">
<span style="font-weight: bold;"> }</span><br style="font-weight: bold;">
<span style="font-weight: bold;">}</span><br>
</div>
<br style="font-family: Courier New,Courier,monospace;">
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public class OtherClass
implements IOther {</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 doOther() {</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;"> }</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>
SomeAndOther如何同時擁有兩個SomeClass與OtherClass類別已定義好的操作?并可以多型操作?SomeAndOther可以
繼承其中之一,并擁有其中之一,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public class SomeAndOther extends
SomeClass implements IOther {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
private IOther other = new OtherClass();</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;">
public void doOther() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
other.doOther();</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;">}</span><br>
</div>
<br>
雖不滿意,但至少解決了目前的問題,當然這邊只是其中一例,畢竟C++是C++,Java是Java,兩者語法并不是一對一的關系,視實際需求還可以變化
一下。<br>
<br>
</div>
</div>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -