C# Design Pattern --- Appearance Pattern

Facade Pattern

The Facade Pattern provides a simple and common interface to handle complex subsystems without reducing the functionality of the subsystems. Reduce the complexity of the subsystem, avoid the direct link between the client and the subsystem, it also reduces the connection between the subsystem and the subsystem, each subsystem has its Facade mode, and each subsystem uses the Facade mode to access other subsystems . This facade class provides a common external interface for the subsystem, and the client object reads and writes the data resources of each interface in the subsystem through a facade interface. The disadvantage of the facade pattern is that it limits the freedom of the client and reduces variability. It is also a structural model.

using System;

namespace ConsoleApplication
{
    //一般每个接口或类都写在单独的.cs文件中
    //本示例为了执行查看方便才写在一起  
    public class Facade
    {
        //被委托的对象
        SubSystemA a;
        SubSystemB b;
        SubSystemC c;
        SubSystemD d;
        public Facade()
        {
            a = new SubSystemA();
            b = new SubSystemB();
            c = new SubSystemC();
            d = new SubSystemD();
        }
        //提供给外部访问的方法
        public void methodA()
        {
            this.a.dosomethingA();
        }
        public void methodB()
        {
            this.b.dosomethingB();
        }
        public void methodC()
        {
            this.c.dosomethingC();
        }
        public void methodD()
        {
            this.d.dosomethingD();
        }
    }
    public class SubSystemA
    {
        public void dosomethingA()
        {
            Console.WriteLine("子系统方法A");
        }
    }
    public class SubSystemB
    {
        public void dosomethingB()
        {
            Console.WriteLine("子系统方法B");
        }
    }
    public class SubSystemC
    {
        public void dosomethingC()
        {
            Console.WriteLine("子系统方法C");
        }
    }
    public class SubSystemD
    {
        public void dosomethingD()
        {
            Console.WriteLine("子系统方法D");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Facade facade = new Facade();
            facade.methodA();
            facade.methodB();
            Console.ReadKey();
        }
    }
}

 

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/131756503