国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

外觀設計模式組圖(Fa?ade pattern)

2019-11-18 12:15:46
字體:
來源:轉載
供稿:網友

  描述
  
  外觀模式(Fa?ade pattern)涉及到子系統的一些類。所謂子系統,是為提供一系列相關的特征(功能)而緊密關聯的一組類。例如,一個Account類、Address類和CreditCard類相互關聯,成為子系統的一部分,提供在線客戶的特征。
  
  在真實的應用系統中,一個子系統可能由很多類組成。子系統的客戶為了它們的需要,需要和子系統中的一些類進行交互。客戶和子系統的類進行直接的交互會導致客戶端對象和子系統(Figure 22.1)之間高度耦合。任何的類似于對子系統中類的接口的修改,會對依靠于它的所有的客戶類造成影響。
  
 外觀設計模式組圖(Fa?ade pattern)(圖一)


  Figure 22.1: Client Interaction with Subsystem Classes before Applying the Fa?ade Pattern
  

  外觀模式(Fa?ade pattern)很適用于在上述情況。外觀模式(Fa?ade pattern)為子系統提供了一個更高層次、更簡單的接口,從而降低了子系統的復雜度和依靠。這使得子系統更易于使用和治理。
  
  外觀是一個能為子系統和客戶提供簡單接口的類。當正確的應用外觀,客戶不再直接和子系統中的類交互,而是與外觀交互。外觀承擔與子系統中類交互的責任。實際上,外觀是子系統與客戶的接口,這樣外觀模式降低了子系統和客戶的耦合度(Figure 22.2).
  
 外觀設計模式組圖(Fa?ade pattern)(圖二)
  Figure 22.2: Client Interaction with Subsystem Classes after Applying the Fa?ade Pattern
  

  從Figure 22.2中我們可以看到:外觀對象隔離了客戶和子系統對象,從而降低了耦合度。當子系統中的類進行改變時,客戶端不會像以前一樣受到影響。
  
  盡管客戶使用由外觀提供的簡單接口,但是當需要的時候,客戶端還是可以視外觀不存在,直接訪問子系統中的底層次的接口。這種情況下,它們之間的依靠/耦合度和原來一樣。
  
  例子:
  
  讓我們建立一個應用:
  
  (1)  接受客戶的具體資料(賬戶、地址和信用卡信息)
  
  (2)  驗證輸入的信息
  
  (3)  保存輸入的信息到相應的文件中。
  
  這個應用有三個類:Account、Address和CreditCard。每一個類都有自己的驗證和保存數據的方法。
  
  Listing 22.1: AccountClass
  
  public class Account {
  String firstName;
  String lastName;
  final String ACCOUNT_DATA_FILE = "AccountData.txt";
  public Account(String fname, String lname) {
  firstName = fname;
  lastName = lname;
  }
  public boolean isValid() {
  /*
  Let's go with simpler validation
  here to keep the example simpler.
  */
  …
  …
  }
  public boolean save() {
  FileUtil futil = new FileUtil();
  String dataLine = getLastName() + ”," + getFirstName();
  return futil.writeToFile(ACCOUNT_DATA_FILE, dataLine,
  true, true);
  }
  public String getFirstName() {
  return firstName;
  }
  public String getLastName() {
  return lastName;
  }
  }
  
  Listing 22.2: Address Class
  
  public class Address {
  String address;
  String city;
  String state;
  final String ADDRESS_DATA_FILE = "Address.txt";
  public Address(String add, String cty, String st) {
  address = add;
  city = cty;
  state = st;
  }
  public boolean isValid() {
  /*
  The address validation algorithm
  could be complex in real-world
  applications.
  Let's go with simpler validation
  here to keep the example simpler.
  */
  if (getState().trim().length() < 2)
  return false;
  return true;
  }
  public boolean save() {
  FileUtil futil = new FileUtil();
  String dataLine = getAddress() + ”," + getCity() + ”," +
  getState();
  return futil.writeToFile(ADDRESS_DATA_FILE, dataLine,
  true, true);
  }
  public String getAddress() {
  return address;
  }
  public String getCity() {
  return city;
  }
  public String getState() {
  return state;
  }
  }
  
  Listing 22.3: CreditCard Class
  
  public class CreditCard {
  String cardType;
  String cardNumber;
  String cardEXPDate;
  final String CC_DATA_FILE = "CC.txt";
  public CreditCard(String ccType, String ccNumber,
  String ccExpDate) {
  cardType = ccType;
  cardNumber = ccNumber;
  cardExpDate = ccExpDate;
  }
  public boolean isValid() {
  /*
  Let's go with simpler validation
  here to keep the example simpler.
  */
  if (getCardType().equals(AccountManager.VISA)) {
  return (getCardNumber().trim().length() == 16);
  }
  if (getCardType().equals(AccountManager.DISCOVER)) {
  return (getCardNumber().trim().length() == 15);
  }
  if (getCardType().equals(AccountManager.MASTER)) {
  return (getCardNumber().trim().length() == 16);
  }
  return false;
  }
  public boolean save() {
  FileUtil futil = new FileUtil();
  String dataLine =
  getCardType() + ,”" + getCardNumber() + ”," +
  getCardExpDate();
  return futil.writeToFile(CC_DATA_FILE, dataLine, true,
  true);
  }
  public String getCardType() {
  return cardType;
  }
  public String getCardNumber() {
  return cardNumber;
  }
  public String getCardExpDate() {
  return cardExpDate;
  }
  }
  
 外觀設計模式組圖(Fa?ade pattern)(圖三)

  讓我們建立一個客戶AccountManager,它提供用戶輸入數據的用戶界面。
  
  Listing 22.4: Client AccountManager Class
  
  public class AccountManager extends JFrame {
  public static final String newline = "/n";
  public static final String VALIDATE_SAVE = "Validate & Save";
  …
  …
  public AccountManager() {
  super(" Facade Pattern - Example ");
  cmbCardType = new JComboBox();
  cmbCardType.addItem(AccountManager.VISA);
  cmbCardType.addItem(AccountManager.MASTER);
  cmbCardType.addItem(AccountManager.DISCOVER);
  …
  …
  //Create buttons
  JButton validateSaveButton =
  new JButton(AccountManager.VALIDATE_SAVE);
  …
  …
  }
  public String getFirstName() {
  return txtFirstName.getText();
  }
  …
  …
  }//End of class AccountManager
  
  當客戶AccountManage運行的時候,展示的用戶接口如下:
  
 外觀設計模式組圖(Fa?ade pattern)(圖四)
  Figure 22.4: User Interface to Enter the Customer Data
  

  為了驗證和保存輸入的數據,客戶AccountManager需要:
  
  (1)  建立Account、Address和CreditCard對象。
  
  (2)  用這些對象驗證輸入的數據
  
  (3)  用這些對象保存輸入的數據。
  
  下面是對象間的交互順序圖:
  
 外觀設計模式組圖(Fa?ade pattern)(圖五)
  Figure 22.5: How a Client Would Normally Interact (Directly) with Subsystem Classes to Validate and Save the Customer Data
  

  在這個例子中應用外觀模式是一個很好的設計,它可以降低客戶和子系統組件(Address、Account和CreditCard)之間的耦合度。應用外觀模式,讓我們定義一個外觀類CustomerFacade (Figure 22.6 and Listing 22.5)。它為由客戶數據處理類(Address、Account和CreditCard)所組成的子系統提供一個高層次的、簡單的接口。
  
  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()
  
 外觀設計模式組圖(Fa?ade pattern)(圖六)
  Figure 22.6: Fa?ade Class to Be Used by the Client in the Revised Design
  

  Listing 22.5: CustomerFacade Cl

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武汉市| 鞍山市| 临潭县| 全椒县| 扶沟县| 邵阳市| 盐山县| 大兴区| 平利县| 房山区| 永济市| 自贡市| 邯郸县| 博湖县| 囊谦县| 田林县| 信丰县| 固原市| 黎川县| 淮北市| 淅川县| 于田县| 溆浦县| 灵武市| 鄂伦春自治旗| 安岳县| 乌海市| 宁津县| 崇仁县| 依安县| 开鲁县| 文山县| 东方市| 从江县| 吉林省| 巴林右旗| 化隆| 宜阳县| 永州市| 炉霍县| 柳江县|