设计模式- 外观模式(Facade)

  • 核心思想是分割不同的层级,降低耦合。

  • 代码示例

using System;
namespace Factory1
{
      //客户端只需要知道这个类调用的类
    public class Facade
    {
        SubSystemOne one;
        SubSystemTwo two;
        SubSystemThree three;
        SubSystemFour four;
        public Facade ()
        {
            one = new SubSystemOne();
            two = new SubSystemTwo ();
            three = new SubSystemThree ();
            four = new SubSystemFour ();
        }
        public void MethodOne(){
            Console.WriteLine ("MethodA");
            one.MethodOne ();
            two.MethodTwo ();
            three.MethodThree ();
            four.MethodFour ();
        }
        public void MethodTwo(){
            Console.WriteLine ("MethodB");
            one.MethodOne ();
            two.MethodTwo ();
            three.MethodThree ();
            four.MethodFour ();
        }
    }
    class SubSystemOne{
        public void MethodOne()
        {
            Console.WriteLine ("Method One");
        }
    }
    class SubSystemTwo{
        public void MethodTwo(){
            Console.WriteLine("Method Two");
        }
    }
    class SubSystemThree{
        public void MethodThree(){
            Console.WriteLine("Method Three");
        }
    }
    class SubSystemFour{
        public void MethodFour(){
            Console.WriteLine("Method Four");
        }
    }
    class MainClass
    {
        public static void Main (string[] args)
        {
            Facade f = new Facade ();
            f.MethodOne ();
            f.MethodTwo ();
            Console.Read ();
        }
    }
}
  • 运行结果

MethodA
Method One
Method Two
Method Three
Method Four
MethodB
Method One
Method Two
Method Three
Method Four

你可能感兴趣的:(设计模式- 外观模式(Facade))