• 外观模式(Facade)


    重点概念回顾

    1.为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一个子系统更觉容易使用。

    2009-12-01_183417

    2. 即使不知道外观模式也一定使用过外观模式,他完美的体现了依赖倒置(面向接口编程)原则和迪米特法则(功能松耦合)的思想,十分常用。

    3. 外观模式的核心是,通过将分散的功能包含在外观类中,从而提供一致的操作接口,封装实现,起到了松耦合的目的

    4. 在设计的初期阶段,应该要有意识的建立层次上的分离。

    5.  可以为封装复杂的接口,也可以为封装众多功能的接口,使用外观模式。

    6.  外观模式就是封装复杂。

    演示例题

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 外观模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                Facade facade = new Facade();
    
                facade.MethodA();
                facade.MethodB();
    
                Console.Read();
    
            }
        }
    
        class SubSystemOne
        {
            public void MethodOne()
            {
                Console.WriteLine(" 子系统方法一");
            }
        }
    
        class SubSystemTwo
        {
            public void MethodTwo()
            {
                Console.WriteLine(" 子系统方法二");
            }
        }
    
        class SubSystemThree
        {
            public void MethodThree()
            {
                Console.WriteLine(" 子系统方法三");
            }
        }
    
        class SubSystemFour
        {
            public void MethodFour()
            {
                Console.WriteLine(" 子系统方法四");
            }
        }
    
        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 MethodA()
            {
                Console.WriteLine("\n方法组A() ---- ");
                one.MethodOne();
                two.MethodTwo();
                four.MethodFour();
            }
    
            public void MethodB()
            {
                Console.WriteLine("\n方法组B() ---- ");
                two.MethodTwo();
                three.MethodThree();
            }
        }
    }
    冯瑞涛
  • 相关阅读:
    Python拼接字符串的7种方法
    jieba结巴分词
    nginx配置文件的性能优化
    nginx默认的配置文件详解
    CentOS怎样安装Python3.6
    Scrapy爬去哪儿~上海一日游门票并存入MongoDB数据库
    Scrapy模拟登录GitHub
    Scrapy爬豆瓣电影Top250并存入MySQL数据库
    Scrapy爬博客园
    创建第一个Scrapy项目
  • 原文地址:https://www.cnblogs.com/finehappy/p/1614853.html
Copyright © 2020-2023  润新知