相比于工厂模式,抽象工厂模式的每个工厂可以创建产品系列,而不是一个产品;
抽象工厂用到的技术:接口、多态、配置文件、反射;
抽象工厂模式的设计原则:
- 实现客户端创建产品和使用产品的分离,客户端无须了解创建的细节,符合迪米特法则;
- 客户端面向接口定义产品,符合依赖倒置原则;
- 客户端面向接口定义工厂,而实际根据配置文件生成工厂实现类,符合里氏替换原则;
- 增加新的产品族时,无须修改原有代码,符合开闭原则;
- 使用配置文件,而不是硬编码方式指定创建的具体工厂,符合迪米特法则;
- 相较于简单工厂模式,抽象工厂中,一个工厂只负责某一产品系列的创建,而不是所有产品的创建,符合单一职责原则;
类图 with SartUML

工厂接口和两个实现类
| internal interface SkinFactory |
| { |
| Button CreateButton(); |
| TextField CreateTextField(); |
| ComboBox CreateComboBox(); |
| } |
| internal class SpringSkinFactory : SkinFactory |
| { |
| public Button CreateButton() |
| { |
| return new SpringButton(); |
| } |
| |
| public ComboBox CreateComboBox() |
| { |
| return new SpringComboBox(); |
| } |
| |
| public TextField CreateTextField() |
| { |
| return new SpringTextField(); |
| } |
| } |
| internal class SummerSkinFactory : SkinFactory |
| { |
| public Button CreateButton() |
| { |
| return new SummerButton(); |
| } |
| |
| public ComboBox CreateComboBox() |
| { |
| return new SummerComboBox(); |
| } |
| |
| public TextField CreateTextField() |
| { |
| return new SummerTextField(); |
| } |
| } |
Button产品接口和两个实现类
| internal interface Button |
| { |
| void Display(); |
| } |
| internal class SpringButton : Button |
| { |
| public void Display() |
| { |
| Console.WriteLine("绿色按钮"); |
| } |
| } |
| internal class SummerButton : Button |
| { |
| public void Display() |
| { |
| Console.WriteLine("蓝色按钮"); |
| } |
| } |
TextField产品的接口和两个实现类
| internal interface TextField |
| { |
| void Display(); |
| } |
| internal class SpringTextField : TextField |
| { |
| public void Display() |
| { |
| Console.WriteLine("绿色文本框"); |
| } |
| } |
| internal class SummerTextField : TextField |
| { |
| public void Display() |
| { |
| Console.WriteLine("蓝色文本框"); |
| } |
| } |
ComboBox产品的接口和两个实现类
| internal interface ComboBox |
| { |
| void Display(); |
| } |
| internal class SpringComboBox : ComboBox |
| { |
| public void Display() |
| { |
| Console.WriteLine("绿色组合框"); |
| } |
| } |
| internal class SummerComboBox : ComboBox |
| { |
| public void Display() |
| { |
| Console.WriteLine("蓝色组合框"); |
| } |
| } |
配置文件
| <?xml version="1.0" encoding="utf-8" ?> |
| <configuration> |
| <appSettings> |
| <add key="factoryType" value="AbstractFactoryPattern.SpringSkinFactory"/> |
| </appSettings> |
| </configuration> |
客户端
| internal class Program |
| { |
| static void Main(string[] args) |
| { |
| |
| SkinFactory factory; |
| Button button; |
| TextField textField; |
| ComboBox comboBox; |
| |
| string factoryType = ConfigurationManager.AppSettings["factoryType"]; |
| |
| factory = Assembly.Load("AbstractFactoryPattern").CreateInstance(factoryType)as SkinFactory; |
| |
| button = factory.CreateButton(); |
| textField = factory.CreateTextField(); |
| comboBox = factory.CreateComboBox(); |
| button.Display(); |
| textField.Display(); |
| comboBox.Display(); |
| Console.Read(); |
| } |
| } |
运行结果:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律