设计模式学习笔记——抽象工厂(Abstract Factory)

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/// <summary>
    /// 抽象工厂类
    /// </summary>
    public interface IDatabaseFactory
    {
 
        IEmployee CreateEmployee();
 
        IUser CreateUser();
    }
/// <summary>
    /// 与SqlServer相关产品的实现
    /// </summary>
    public class SqlServerDatabaseFactory : IDatabaseFactory
    {
 
        public IEmployee CreateEmployee()
        {
 
            return new SqlEmployee();
        }
 
        public IUser CreateUser()
        {
 
            return new SqlUser();
        }
 
    }
 
 
    /// <summary>
    /// 与Access相关产品的实现
    /// </summary>
    public class AccessDatabaseFactory : IDatabaseFactory
    {
 
        public IEmployee CreateEmployee()
        {
 
            return new AccessEmployee();
        }
 
        public IUser CreateUser()
        {
 
            return new AccessUser();
        }
 
    }
/// <summary>
    /// 抽象产品
    /// </summary>
    public interface IUser
    {
        User GetUser();
 
        bool SaveUser();
    }
 
    /// <summary>
    /// 抽象产品
    /// </summary>
    public interface IEmployee
    {
 
        Employee GetEmployee();
 
        bool SaveEmployee();
    }
/// <summary>
    /// 与SqlServer相关的具体产品User
    /// </summary>
    public class SqlUser : IUser
    {
        public User GetUser()
        {
 
            return null;
        }
 
        public bool SaveUser()
        {
 
            return false;
        }
 
    }
 
    /// <summary>
    /// 与SqlServer相关的具体产品Employee
    /// </summary>
    public class SqlEmployee : IEmployee
    {
 
        public SqlEmployee()
        {
 
        }
 
        public Employee GetEmployee()
        {
 
            return null;
        }
 
        public bool SaveEmployee()
        {
 
            return false;
        }
 
    }
 
    /// <summary>
    /// 与Access相关的具体产品User
    /// </summary>
    public class AccessUser : IUser
    {
 
        public AccessUser()
        {
 
        }
 
        public User GetUser()
        {
 
            return null;
        }
 
        public bool SaveUser()
        {
 
            return false;
        }
 
    }
 
    /// <summary>
    /// 与Access相关的具体产品Employee
    /// </summary>
    public class AccessEmployee : IEmployee
    {
 
        public AccessEmployee()
        {
 
        }
 
        public Employee GetEmployee()
        {
 
            return null;
        }
 
        public bool SaveEmployee()
        {
 
            return false;
        }
 
    }

  

posted @   ice_baili  阅读(155)  评论(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语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示