Design Pattern - Facade(C#)
阿新 • • 發佈:2018-11-02
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
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
- 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 C#
This structural code demonstrates the Facade pattern which provides a simplified and uniform interface to a large subsystem of classes.
// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Facade Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{ using System; /// <summary> /// Startup class for Structural Facade Design Pattern. /// </summary> internal static class Program { #region Public Methods and Operators /// <summary> /// Entry point into console application. /// </summary> public static void Main() { var facade = new Facade(); facade.MethodA(); facade.MethodB(); } #endregion } /// <summary> /// The 'Subsystem ClassA' class /// </summary> internal class SubSystemOne { #region Public Methods and Operators /// <summary> /// The method one. /// </summary> public void MethodOne() { Console.WriteLine(" SubSystemOne Method"); } #endregion } /// <summary> /// The 'Subsystem ClassB' class /// </summary> internal class SubSystemTwo { #region Public Methods and Operators /// <summary> /// The method two. /// </summary> public void MethodTwo() { Console.WriteLine(" SubSystemTwo Method"); } #endregion } /// <summary> /// The 'Subsystem ClassC' class /// </summary> internal class SubSystemThree { #region Public Methods and Operators /// <summary> /// The method three. /// </summary> public void MethodThree() { Console.WriteLine(" SubSystemThree Method"); } #endregion } /// <summary> /// The 'Subsystem ClassD' class /// </summary> internal class SubSystemFour { #region Public Methods and Operators /// <summary> /// The method four. /// </summary> public void MethodFour() { Console.WriteLine(" SubSystemFour Method"); } #endregion } /// <summary> /// The 'Facade' class /// </summary> internal class Facade { #region Fields /// <summary> /// The one. /// </summary> private SubSystemOne one; /// <summary> /// The two. /// </summary> private SubSystemTwo two; /// <summary> /// The three. /// </summary> private SubSystemThree three; /// <summary> /// The four. /// </summary> private SubSystemFour four; #endregion #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="Facade"/> class. /// </summary> public Facade() { this.one = new SubSystemOne(); this.two = new SubSystemTwo(); this.three = new SubSystemThree(); this.four = new SubSystemFour(); } #endregion #region Public Methods and Operators /// <summary> /// The method a. /// </summary> public void MethodA() { Console.WriteLine("\nMethodA() ---- "); this.one.MethodOne(); this.two.MethodTwo(); this.four.MethodFour(); } /// <summary> /// The method b. /// </summary> public void MethodB() { Console.WriteLine("\nMethodB() ---- "); this.two.MethodTwo(); this.three.MethodThree(); } #endregion }}// Output:/*MethodA() ---- SubSystemOne Method SubSystemTwo Method SubSystemFour MethodMethodB() ---- SubSystemTwo Method SubSystemThree 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.
// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Facade Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{ using System; /// <summary> /// Startup class for Real-World Facade Design Pattern. /// </summary> internal static class Program { #region Methods /// <summary> /// Entry point into console application. /// </summary> private static void Main() { // Facade var mortgage = new Mortgage(); // Evaluate mortgage eligibility for customer var customer = new Customer("Ann McKinsey"); bool eligible = mortgage.IsEligible(customer, 125000); Console.WriteLine("\n" + customer.Name + " has been " + (eligible ? "Approved" : "Rejected")); } #endregion } /// <summary> /// The 'Subsystem ClassA' class /// </summary> internal class Bank { #region Public Methods and Operators /// <summary> /// The has sufficient savings. /// </summary> /// <param name="c"> /// The c. /// </param> /// <param name="amount"> /// The amount. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public bool HasSufficientSavings(Customer c, int amount) { Console.WriteLine("Check bank for " + c.Name); return true; } #endregion } /// <summary> /// The 'Subsystem ClassB' class /// </summary> internal class Credit { #region Public Methods and Operators /// <summary> /// The has good credit. /// </summary> /// <param name="c"> /// The c. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public bool HasGoodCredit(Customer c) { Console.WriteLine("Check credit for " + c.Name); return true; } #endregion } /// <summary> /// The 'Subsystem ClassC' class /// </summary> internal class Loan { #region Public Methods and Operators /// <summary> /// The has no bad loans. /// </summary> /// <param name="c"> /// The c. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public bool HasNoBadLoans(Customer c) { Console.WriteLine("Check loans for " + c.Name); return true; } #endregion } /// <summary> /// Customer class /// </summary> internal class Customer { #region Fields #endregion // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="Customer"/> class. /// </summary> /// <param name="name"> /// The name. /// </param> public Customer(string name) { this.Name = name; } #endregion // Gets the name #region Public Properties /// <summary> /// Gets the name. /// </summary> public string Name { get; private set; } #endregion } /// <summary> /// The 'Facade' class /// </summary> internal class Mortgage { #region Fields /// <summary> /// The bank. /// </summary> private Bank bank = new Bank(); /// <summary> /// The credit. /// </summary> private Credit credit = new Credit(); /// <summary> /// The loan. /// </summary> private Loan loan = new Loan(); #endregion #region Public Methods and Operators /// <summary> /// The is eligible. /// </summary> /// <param name="cust"> /// The customer. /// </param> /// <param name="amount"> /// The amount. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public bool IsEligible(Customer cust, int amount) { Console.WriteLine("{0} applies for {1:C} loan\n", cust.Name, amount); bool 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; } #endregion }}// Output:/*Ann McKinsey applies for $125,000.00 loanCheck bank for Ann McKinseyCheck loans for Ann McKinseyCheck credit for Ann McKinseyAnn McKinsey has been Approved*/