Design Pattern - Facade(C#)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!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 (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 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*/
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值