Facade 模式(外观模式)

为子系统中的一组接口提供一个一致的界面。Facade模式定义了一个高层接口,这个接口使得这一系统更容易使用。

Facade 模式(外观模式)
Facade(MortgageApplication)

知道哪些子系统类负责处理请求。

将客户的请求代理给适当的子系统对象。

Subsystem classes((Bank, Credit, Loan)

实现子系统的功能。

处理有facade对象指派的任务。

没有facade的任何相关信息;即没有引用facade。

代码

 

package design.pattern.facade;

 

/**

 * Subsystem ClassA

 * @author tyrone

 *

 */

public class Bank {

       // Methods

       /** @modelguid {0204A2F3-F161-4786-B0AE-34388CFBD12B} */

       public boolean SufficientSavings( Customer c )

       {

        System.out.println('Check bank for '+ c.getName() );

        return true;

       }

 

}

 

package design.pattern.facade;

 

/**

 * Subsystem ClassB

 * @author tyrone

 *

 */

public class Credit {

       // Methods

       public boolean GoodCredit( int amount, Customer c )

       {

        System.out.println( 'Check credit for '+ c.getName() );

        return true;

       }

 

}

 

package design.pattern.facade;

 

/**

 * Subsystem ClassC

 * @author tyrone

 *

 */

public class Loan {

       // Methods

       public boolean GoodLoan( Customer c )

       {

              System.out.println( 'Check loan for '+ c.getName());

        return true;

       }

 

}

 

 

package design.pattern.facade;

/**

 * @author tyrone

 *

 */

public class Customer {

       private String name;

      

public Customer(String name){

              this.name=name;

       }

       public void setName(String name){

              this.name=name;

       }

      

       public String getName(){

              return this.name;

       }

}

 

package design.pattern.facade;

 

/**

 * Facade

 * @author tyrone

 *

 */

public class MortgageApplication {

       // Fields

        int amount;

        private Bank bank = new Bank();

        private Loan loan = new Loan();

        private Credit credit = new Credit();

 

        // Constructors

        public MortgageApplication( int amount )

        {

          this.amount = amount;

        }

 

        // Methods

        public boolean IsEligible( Customer c )

        {

          // Check creditworthyness of applicant

          if( !bank.SufficientSavings( c ) ) 

               return false;

          if( !loan.GoodLoan( c ) ) 

               return false;

          if( !credit.GoodCredit( amount, c )) 

               return false;

 

          return true;

        }

}

 

package design.pattern.facade;

 

/**

 * <summary>

 * Facade Client

 * </summary>

 * @author tyrone

 */

public class FacadeApp {

       /** @modelguid {67B55D21-888A-422E-B88E-1FBC34D3AA5C} */

       public static void main(String[] args)

        {

          // Create Facade

          MortgageApplication mortgage =

                              new MortgageApplication( 125000 );

          // Call subsystem through Facade

          mortgage.IsEligible(

                           new Customer( 'Gabrielle McKinsey' ) );

 

        }

 

}

 

Output

Check bank for Gabrielle McKinsey
Check loan for Gabrielle McKinsey
Check credit for Gabrielle McKinsey

-----------------------------------------------------------------------------------------------------------------------------------------------------

Facade模式的定义: 为子系统中的一组接口提供一个一致的界面.

Facade一个典型应用就是数据库JDBC的应用,如下例对数据库的操作:

public class DBCompare {

  Connection conn = null;
  PreparedStatement prep = null;
  ResultSet rset = null;
  try {
     Class.forName( "<driver>" ).newInstance();
     conn = DriverManager.getConnection( "<database>" );
    
     String sql = "SELECT * FROM <table> WHERE <column name> = ?";
     prep = conn.prepareStatement( sql );
     prep.setString( 1, "<column value>" );
     rset = prep.executeQuery();
     if( rset.next() ) {
        System.out.println( rset.getString( "<column name" ) );
     }
  } catch( SException e ) {
     e.printStackTrace();
  } finally {
     rset.close();
     prep.close();
     conn.close();
  }
}

上例是Jsp中最通常的对数据库操作办法.

在应用中,经常需要对数据库操作,每次都写上述一段代码肯定比较麻烦,需要将其中不变的部分提炼出来,做成一个接口,这就引入了facade外观对象.如果以后我们更换Class.forName中的<driver>也非常方便,比如从Mysql数据库换到Oracle数据库,只要更换facade接口中的driver就可以.

我们做成了一个Facade接口,使用该接口,上例中的程序就可以更改如下:

public class DBCompare {

  String sql = "SELECT * FROM <table> WHERE <column name> = ?";  

  try {
     Mysql msql=new mysql(sql);
     prep.setString( 1, "<column value>" );
     rset = prep.executeQuery();
     if( rset.next() ) {
        System.out.println( rset.getString( "<column name" ) );
     }
  } catch( SException e ) {
     e.printStackTrace();
  } finally {
     mysql.close();
     mysql=null;
  }
}

可见非常简单,所有程序对数据库访问都是使用改接口,降低系统的复杂性,增加了灵活性.

如果我们要使用连接池,也只要针对facade接口修改就可以.

 

Facade 模式(外观模式)

由上图可以看出, facade实际上是个理顺系统间关系,降低系统间耦合度的一个常用的办法,也许你已经不知不觉在使用,尽管不知道它就是facade.

你可能感兴趣的:(Facade)