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