通常
在這種情況下
在上面討論的接口
(
(
(
類適配器(Class Adapter)VS對象適配器(Object Adapter)
適配器總體上可以分為兩類
類適配器
類適配器是通過繼承類適配者類(Adaptee Class)實現的
對象適配器
對象適配器包含一個適配器者的引用(reference)
下表是類適配器(Class Adapter)和對象適配器(Object Adapter)的詳細不同
補充
類適配器(Class Adapter) 對象適配器(Object Adapter)
基於繼承概念 利用對象合成
只能應用在適配者是接口
注意
注意:字面上
客戶類對適配者中聲明為public的接口是可見的
在JAVA應用程序中
適用於期待的接口是JAVA接口的形式
適用於當客戶對象期望的接口是抽象類的形式
例子
讓我們建立一個驗證給定客戶地址的應用
讓我們定義一個Customer類
Customer
Figure
Listing
class Customer {
public static final String US =
public static final String CANADA =
private String address;
private String name;
private String zip
public boolean isValidAddress() {
…
…
}
public Customer(String inp_name
String inp_zip
String inp_type) {
name = inp_name;
address = inp_address;
zip = inp_zip;
state = inp_state;
type = inp_type;
}
}//end of class
不同的客戶對象創建Customer對象並調用(invoke)isValidAddress方法驗證客戶地址的有效性
Listing
public interface AddressValidator {
public boolean isValidAddress(String inp_address
String inp_zip
}//end of class
讓我們定義一個USAddress的驗證類
Listing
class USAddress implements AddressValidator {
public boolean isValidAddress(String inp_address
String inp_zip
if (inp_address
return false;
if (inp_zip
return false;
if (inp_zip
return false;
if (inp_state
return false;
return true;
}
}//end of class
USAddress類實現AddressValidator接口
Listing
class Customer {
…
…
public boolean isValidAddress() {
//get an appropriate address validator
AddressValidator validator = getValidator(type);
//Polymorphic call to validate the address
return validator
}
private AddressValidator getValidator(String custType) {
AddressValidator validator = null;
if (custType
validator = new USAddress();
}
return validator;
}
}//end of class
Figure
但是當驗證來自加拿大的客戶時
從下面的CAAdress類的實現
Listing
class CAAddress {
public boolean isValidCanadianAddr(String inp_address
String inp_pcode
if (inp_address
return false;
if (inp_pcode
return false;
if (inp_prvnc
return false;
return true;
}
}//end of class
CAAdress類提供了一個isValidCanadianAddr方法
接口的不兼容使得Customer對象利用現有的CAAdress類是困難的
應用適配器模式
Figure
Listing
public class CAAddressAdapter extends CAAddress
implements AddressValidator {
public boolean isValidAddress(String inp_address
String inp_zip
return isValidCanadianAddr(inp_address
inp_state);
}
}//end of class
因為適配器CAAdressAdapter實現了AddressValidator接口
在Customer類內部
Listing
class Customer {
…
…
public boolean isValidAddress() {
//get an appropriate address validator
AddressValidator validator = getValidator(type);
//Polymorphic call to validate the address
return validator
}
private AddressValidator getValidator(String custType) {
AddressValidator validator = null;
if (custType
validator = new USAddress();
}
if (type
validator = new CAAddressAdapter();
}
return validator;
}
}//end of class
CAAddressAdapter設計和對AddressValidator(聲明期望的接口)對象的多態調用使Customer可以利用接口不兼容CAAddress類提供的服務
Figure
Figure
作為對象適配器的地址適配器
當討論以類適配器來實現地址適配器時
Listing
public abstract class AddressValidator {
public abstract boolean isValidAddress(String inp_address
String inp_zip
}//end of class
Listing
class CAAddressAdapter extends AddressValidator {
…
…
public CAAddressAdapter(CAAddress address) {
objCAAddress = address;
}
public boolean isValidAddress(String inp_address
String inp_zip
…
…
}
}//end of class
因為多繼承在JAVA中不支持
應用對象適配器模式
(
(
Figure
Listing
class CAAddressAdapter extends AddressValidator {
private CAAddress objCAAddress;
public CAAddressAdapter(CAAddress address) {
objCAAddress = address;
}
public boolean isValidAddress(String inp_address
String inp_zip
return objCAAddress
inp_zip
}
}//end of class
當客戶對象調用CAAddressAdapter(adapter)上的isValidAddress方法時
Figure
從這個例子可以看出
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27273.html