美文网首页设计模式
设计模式- 外观模式(Facade)

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

作者: Mitchell | 来源:发表于2016-03-20 17:42 被阅读45次
    • 核心思想是分割不同的层级,降低耦合。

    • 代码示例

    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)

        本文链接:https://www.haomeiwen.com/subject/upqklttx.html