设计模式学习笔记——外观模式(Facade)

1.特点:子系统的高层接口,避免两个类直接关系的第三者。

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

3.类图:

4.程序实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/// <summary>
    /// 子系统中的一个类
    /// </summary>
    public class SubSystemOne
    {
        public void MethodeOne()
        {
            Console.WriteLine("Sub System first method.");
        }
    }
 
    /// <summary>
    /// 子系统中一个类
    /// </summary>
    public class SubSystemTwo
    {
        public void MethodTwo()
        {
            Console.WriteLine("Sub System second method.");
        }
    }
 
    /// <summary>
    /// 子系统中一个类
    /// </summary>
    public class SubSystemThree
    {
        public void MethodThree()
        {
            Console.WriteLine("Sub System third method.");
        }
    }
 
    /// <summary>
    /// 子系统中一个类
    /// </summary>
    public class SubSystemFour
    {
        public void MethodFour()
        {
            Console.WriteLine("Sub System fourth method.");
        }
    }
 
    /// <summary>
    /// 外观类
    /// </summary>
    public class Facade
    {
        private SubSystemOne one;
        private SubSystemTwo two;
        private SubSystemThree three;
        private SubSystemFour four;
 
        public Facade()
        {
            one = new SubSystemOne();
            two = new SubSystemTwo();
            three = new SubSystemThree();
            four = new SubSystemFour();
        }
 
        public void MethodA()
        {
            Console.WriteLine("\nMethod group A----");
            one.MethodeOne();
            two.MethodTwo();
            four.MethodFour();
        }
 
        public void MethodB()
        {
            Console.WriteLine("\nMethod group B----");
            two.MethodTwo();
            three.MethodThree();
        }
    }
class Program
    {
        static void Main(string[] args)
        {
            // 由于Facade的作用,客户端可以根本不知道子系统类的存在
            Facade facade = new Facade();
            facade.MethodA();
            facade.MethodB();
 
            Console.Read();
        }
    }

  

posted @   ice_baili  阅读(228)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示