结构型模式 外观模式
Definition
UML class diagram
![facade[1].gif]()
Participants
Sample code in C#
Output
| Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. |
![facade[1].gif](http://www.cnblogs.com/images/cnblogs_com/lw/facade[1].gif)
Participants
The classes and/or objects participating in the Façade pattern are:
|
Sample code in C#
This structural code demonstrates the Facade pattern which provides a simplified and uniform interface to a large subsystem of classes.
// Facade pattern -- Structural example |
using System;{ public void MethodOne() { Console.WriteLine("SubSystemOne Method"); }}{ public void MethodTwo() { Console.WriteLine("SubSystemTwo Method"); }}{ public void MethodThree() { Console.WriteLine("SubSystemThree Method"); }}{ public void MethodFour() { Console.WriteLine("SubSystemFour Method"); }}{ // Fields SubSystemOne one; SubSystemTwo two; SubSystemThree three; SubSystemFour four;// Constructors public Facade() { one = new SubSystemOne(); two = new SubSystemTwo(); three = new SubSystemThree(); four = new SubSystemFour(); }// Methods public void MethodA() { Console.WriteLine( "\nMethodA() ---- " ); one.MethodOne(); two.MethodTwo(); four.MethodFour(); } { Console.WriteLine( "\nMethodB() ---- " ); two.MethodTwo(); three.MethodThree(); }}/// Client test/// </summary>public class Client{ public static void Main(string[] args) {// Create and call Facade Facade f = new Facade(); f.MethodA(); f.MethodB(); }} |
MethodA() ---- |
This real-world code demonstrates the Facade pattern as a MortgageApplication object which provides a simplified interface to a large subsystem of classes measuring the creditworthyness of an applicant.
// Facade pattern -- Real World example |
|
using System;{ // Methods public bool SufficientSavings( Customer c ) { Console.WriteLine("Check bank for {0}", c.Name ); return true; }}{ // Methods public bool GoodCredit( int amount, Customer c ) { Console.WriteLine( "Check credit for {0}", c.Name ); return true; }}{ // Methods public bool GoodLoan( Customer c ) { Console.WriteLine( "Check loan for {0}", c.Name ); return true; }}{ // Fields private string name;// Constructors public Customer( string name ) { this.name = name; }// Properties public string Name { get{ return name; } }}{ // 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 bool IsEligible( Customer c ) {// Check creditworthyness of applicant if( !bank.SufficientSavings( c ) ) return true; }}/// <summary>/// Facade Client/// </summary>public class FacadeApp{ public static void Main(string[] args) { // Create Facade MortgageApplication mortgage = new MortgageApplication( 125000 ); // Call subsystem through Facade mortgage.IsEligible( new Customer( "Gabrielle McKinsey" ) );} |
|
Output
|
浙公网安备 33010602011771号