描述
外觀模式(Fa?ade pattern)涉及到子系統的一些類
在真實的應用系統中
Figure
外觀模式(Fa?ade pattern)很適用於在上述情況
外觀是一個能為子系統和客戶提供簡單接口的類
Figure
從Figure
盡管客戶使用由外觀提供的簡單接口
例子
讓我們建立一個應用
(
(
(
這個應用有三個類
Listing
public class Account {
String firstName;
String lastName;
final String ACCOUNT_DATA_FILE =
public Account(String fname
firstName = fname;
lastName = lname;
}
public boolean isValid() {
/*
Let
here to keep the example simpler
*/
…
…
}
public boolean save() {
FileUtil futil = new FileUtil();
String dataLine = getLastName() +
return futil
true
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Listing
public class Address {
String address;
String city;
String state;
final String ADDRESS_DATA_FILE =
public Address(String add
address = add;
city = cty;
state = st;
}
public boolean isValid() {
/*
The address validation algorithm
could be complex in real
applications
Let
here to keep the example simpler
*/
if (getState()
return false;
return true;
}
public boolean save() {
FileUtil futil = new FileUtil();
String dataLine = getAddress() +
getState();
return futil
true
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
}
Listing
public class CreditCard {
String cardType;
String cardNumber;
String cardExpDate;
final String CC_DATA_FILE =
public CreditCard(String ccType
String ccExpDate) {
cardType = ccType;
cardNumber = ccNumber;
cardExpDate = ccExpDate;
}
public boolean isValid() {
/*
Let
here to keep the example simpler
*/
if (getCardType()
return (getCardNumber()
}
if (getCardType()
return (getCardNumber()
}
if (getCardType()
return (getCardNumber()
}
return false;
}
public boolean save() {
FileUtil futil = new FileUtil();
String dataLine =
getCardType() +
getCardExpDate();
return futil
true);
}
public String getCardType() {
return cardType;
}
public String getCardNumber() {
return cardNumber;
}
public String getCardExpDate() {
return cardExpDate;
}
}
讓我們建立一個客戶AccountManager
Listing
public class AccountManager extends JFrame {
public static final String newline =
public static final String VALIDATE_SAVE =
…
…
public AccountManager() {
super(
cmbCardType = new JComboBox();
cmbCardType
cmbCardType
cmbCardType
…
…
//Create buttons
JButton validateSaveButton =
new JButton(AccountManager
…
…
}
public String getFirstName() {
return txtFirstName
}
…
…
}//End of class AccountManager
當客戶AccountManage運行的時候
Figure
為了驗證和保存輸入的數據
(
(
(
下面是對象間的交互順序圖
Figure
在這個例子中應用外觀模式是一個很好的設計
CustomerFacade
address:String
city:String
state:String
cardType:String
cardNumber:String
cardExpDate:String
fname:String
lname:String
setAddress(inAddress:String)
setCity(inCity:String)
setState(inState:String)
setCardType(inCardType:String)
setCardNumber(inCardNumber:String)
setCardExpDate(inCardExpDate:String)
setFName(inFName:String)
setLName(inLName:String)
saveCustomerData()
Figure
Listing
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27533.html