外观模式(Facade)

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

FacadeMortgageApplication

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

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

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

你可能感兴趣的:(C++,c,C#)