设计模式- 外观模式(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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 介绍 1、使用频率高,常见的是第三方库。 2、对外可见的只有一两个类,而内部涉及可能是个很庞大很复杂的系统。 定义...
    小菜_charry阅读 3,649评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,095评论 19 139
  • 外观模式是为了解决类与类之家的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就...
    晓疯阅读 2,540评论 0 0
  • 其实说起来 黎小姐算是我最好的朋友 但是我却放在第三篇才来写她 因为我总是组织不好自己的语言 我不知道要怎样写才能...
    宝贝你阅读 3,811评论 2 3
  • Updating Your Image Resource Files Apps running in iOS 4 ...
    whlpkk阅读 5,805评论 2 2

友情链接更多精彩内容