Design Pattern - Facade(Java)
作者:互联网
分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net
Definition
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Participants
The classes and/or objects participating in this pattern are:
- Facade (MortgageApplication)
- Knows which subsystem classes are responsible for a request.
- Delegates client requests to appropriate subsystem objects.
- Subsystem classes (Bank, Credit, Loan)
- Implement subsystem functionality.
- Handle work assigned by the Facade object.
- Have no knowledge of the facade and keep no reference to it.
Sample Code in Java
This structural code demonstrates the Facade pattern which provides a simplified and uniform interface to a large subsystem of classes.
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.sample;
/**
* The 'Facade' class.
*
* @author Chimomo
*/
class Facade {
// The sub system one.
private SubSystemOne one;
// The sub system two.
private SubSystemTwo two;
// The sub system three.
private SubSystemThree three;
// The sub system four.
private SubSystemFour four;
/**
* Initializes a new instance of the "Facade" class.
*/
public Facade() {
this.one = new SubSystemOne();
this.two = new SubSystemTwo();
this.three = new SubSystemThree();
this.four = new SubSystemFour();
}
/**
* The method a.
*/
public void methodA() {
System.out.println("MethodA() ------ ");
this.one.methodOne();
this.two.methodTwo();
this.four.methodFour();
}
/**
* The method b.
*/
public void methodB() {
System.out.println("MethodB() ------ ");
this.two.methodTwo();
this.three.methodThree();
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.sample;
/**
* Startup class for Structural Facade Design Pattern.
*
* @author Chimomo
*/
class Program {
/**
* Entry point into console application.
*
* @param args
*/
public static void main(String[] args) {
Facade facade = new Facade();
facade.methodA();
facade.methodB();
}
}
/*
Output:
MethodA() ------
SubSystemOne Method
SubSystemTwo Method
SubSystemFour Method
MethodB() ------
SubSystemTwo Method
SubSystemThree Method
*/
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.sample;
/**
* The 'Subsystem ClassD' class.
*
* @author Chimomo
*/
class SubSystemFour {
/**
* The method four.
*/
public void methodFour() {
System.out.println(" SubSystemFour Method");
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.sample;
/**
* The 'Subsystem ClassA' class.
*
* @author Chimomo
*/
class SubSystemOne {
/**
* The method one.
*/
public void methodOne() {
System.out.println(" SubSystemOne Method");
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.sample;
/**
* The 'Subsystem ClassC' class.
*
* @author Chimomo
*/
class SubSystemThree {
/**
* The method three.
*/
public void methodThree() {
System.out.println(" SubSystemThree Method");
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.sample;
/**
* The 'Subsystem ClassB' class.
*
* @author Chimomo
*/
class SubSystemTwo {
/**
* The method two.
*/
public void methodTwo() {
System.out.println(" SubSystemTwo Method");
}
}
This real-world code demonstrates the Facade pattern as a Mortgage Application object which provides a simplified interface to a large subsystem of classes measuring the credit worthiness of an applicant.
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.realworld;
/**
* The 'Subsystem ClassA' class.
*
* @author Chimomo
*/
class Bank {
/**
* The has sufficient savings.
*
* @param c
* @param amount
* @return
*/
public boolean hasSufficientSavings(Customer c, int amount) {
System.out.println("Check bank for " + c.getName());
return true;
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.realworld;
/**
* The 'Subsystem ClassB' class.
*
* @author Chimomo
*/
class Credit {
/**
* The has good credit.
*
* @param c
* @return
*/
public boolean hasGoodCredit(Customer c) {
System.out.println("Check credit for " + c.getName());
return true;
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.realworld;
/**
* The Customer class.
*
* @author Chimomo
*/
class Customer {
// The name.
private String name;
/**
* Initializes a new instance of the "Customer" class.
*
* @param name
*/
public Customer(String name) {
this.name = name;
}
/**
* Get name.
*/
public String getName() {
return this.name;
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.realworld;
/**
* The 'Subsystem ClassC' class.
*
* @author Chimomo
*/
class Loan {
/**
* The has no bad loans.
*
* @param c
* @return
*/
public boolean hasNoBadLoans(Customer c) {
System.out.println("Check loans for " + c.getName());
return true;
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.realworld;
/**
* The 'Facade' class.
*
* @author Chimomo
*/
class Mortgage {
// The bank.
private Bank bank = new Bank();
// The credit.
private Credit credit = new Credit();
// The loan.
private Loan loan = new Loan();
/**
* The is eligible.
*
* @param cust
* @param amount
* @return
*/
public boolean isEligible(Customer cust, int amount) {
System.out.println(String.format("%s applies for %d loan", cust.getName(), amount));
boolean eligible = true;
// Check credit worthiness of applicant.
if (!this.bank.hasSufficientSavings(cust, amount)) {
eligible = false;
} else if (!this.loan.hasNoBadLoans(cust)) {
eligible = false;
} else if (!this.credit.hasGoodCredit(cust)) {
eligible = false;
}
return eligible;
}
}
/*
* Chimomo's Blog: https://blog.csdn.net/chimomo/
*/
package chimomo.learning.java.designpattern.facade.realworld;
/**
* Startup class for Real-World Facade Design Pattern.
*
* @author Chimomo
*/
class Program {
/**
* Entry point into console application.
*
* @param args
*/
public static void main(String[] args) {
// Facade.
Mortgage mortgage = new Mortgage();
// Evaluate mortgage eligibility for customer.
Customer customer = new Customer("Ann McKinsey");
boolean eligible = mortgage.isEligible(customer, 125000);
System.out.println(customer.getName() + " has been " + (eligible ? "Approved" : "Rejected"));
}
}
/*
Output:
Ann McKinsey applies for 125000 loan
Check bank for Ann McKinsey
Check loans for Ann McKinsey
Check credit for Ann McKinsey
Ann McKinsey has been Approved
*/
标签:Java,chimomo,Pattern,public,facade,Facade,class,Chimomo 来源: https://blog.csdn.net/chimomo/article/details/80974411