服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

外观模式(fa?ade pattern)(1)

描述

外观模式(façade pattern)涉及到子系统的一些类。所谓子系统,是为提供一系列相关的特征(功能)而紧密关联的一组类。例如,一个account类、address类和creditcard类相互关联,成为子系统的一部分,提供在线客户的特征。

在真实的应用系统中,一个子系统可能由很多类组成。子系统的客户为了它们的需要,需要和子系统中的一些类进行交互。客户和子系统的类进行直接的交互会导致客户端对象和子系统之间高度耦合。任何的类似于对子系统中类的接口的修改,会对依赖于它的所有的客户类造成影响。

外观模式(façade pattern)很适用于在上述情况。外观模式(façade pattern)为子系统提供了一个更高层次、更简单的接口,从而降低了子系统的复杂度和依赖。这使得子系统更易于使用和管理。

外观是一个能为子系统和客户提供简单接口的类。当正确的应用外观,客户不再直接和子系统中的类交互,而是与外观交互。外观承担与子系统中类交互的责任。实际上,外观是子系统与客户的接口,这样外观模式降低了子系统和客户的耦合度。

我们可以看到:外观对象隔离了客户和子系统对象,从而降低了耦合度。当子系统中的类进行改变时,客户端不会像以前一样受到影响。

尽管客户使用由外观提供的简单接口,但是当需要的时候,客户端还是可以视外观不存在,直接访问子系统中的底层次的接口。这种情况下,它们之间的依赖/耦合度和原来一样。

例子:

让我们建立一个应用:

(1)    接受客户的详细资料(账户、地址和信用卡信息)

(2)    验证输入的信息

(3)    保存输入的信息到相应的文件中。

这个应用有三个类:account、address和creditcard。每一个类都有自己的验证和保存数据的方法。

listing 22.1: accountclass 

  1. public class account { 
  2.   string firstname; 
  3.   string lastname; 
  4.   final string account_data_file = "accountdata.txt"; 
  5.   public account(string fname, string lname) { 
  6.     firstname = fname; 
  7.     lastname = lname; 
  8.   } 
  9.   public boolean isvalid() { 
  10.     /* 
  11.      let's go with simpler validation 
  12.      here to keep the example simpler. 
  13.     */ 
  14.         … 
  15.         … 
  16.   } 
  17.   public boolean save() { 
  18.     fileutil futil = new fileutil(); 
  19.     string dataline = getlastname() + ”," + getfirstname(); 
  20.     return futil.writetofile(account_data_file, dataline, 
  21.     truetrue); 
  22.   } 
  23.   public string getfirstname() { 
  24.     return firstname; 
  25.   } 
  26.   public string getlastname() { 
  27.     return lastname; 
  28.   } 
listing 22.2: address class 

  1. public class address { 
  2.   string address; 
  3.   string city; 
  4.   string state; 
  5.   final string address_data_file = "address.txt"; 
  6.   public address(string add, string cty, string st) { 
  7.     address = add; 
  8.     city = cty; 
  9.     state = st; 
  10.   } 
  11.   public boolean isvalid() { 
  12.      /* 
  13.        the address validation algorithm 
  14.        could be complex in real-world 
  15.        applications. 
  16.        let's go with simpler validation 
  17.        here to keep the example simpler. 
  18.       */ 
  19.     if (getstate().trim().length() < 2) 
  20.        return false
  21.     return true
  22.   } 
  23.   public boolean save() { 
  24.     fileutil futil = new fileutil(); 
  25.     string dataline = getaddress() + ”," + getcity() + ”," + 
  26.                       getstate(); 
  27.     return futil.writetofile(address_data_file, dataline, 
  28.            truetrue); 
  29.   } 
  30.   public string getaddress() { 
  31.     return address; 
  32.   } 
  33.   public string getcity() { 
  34.     return city; 
  35.   } 
  36.   public string getstate() { 
  37.     return state; 
  38.   } 
listing 22.3: creditcard class 

  1. public class creditcard { 
  2.   string cardtype; 
  3.   string cardnumber; 
  4.   string cardexpdate; 
  5.   final string cc_data_file = "cc.txt"; 
  6.   public creditcard(string cctype, string ccnumber, 
  7.                     string ccexpdate) { 
  8.     cardtype = cctype; 
  9.     cardnumber = ccnumber; 
  10.     cardexpdate = ccexpdate; 
  11.   } 
  12.   public boolean isvalid() { 
  13.     /* 

扫描关注微信公众号