软件设计模式之抽象工厂模式
抽象工厂模式概念:
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
抽象工厂优点:
主要解决了接口选择的问题;在系统的产品有多于一个的产品族,而系统只消费其中某一族的产品时使用抽象工厂。
实现:
这里我做了一个衣服搭配的例子,首先衣服种类是一个简单工厂,然后衣服颜色是一个简单工厂;最后再来个抽象工厂来对所有的工厂进行管理;
首先衣服种类简单工厂实现:
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 | 服装接口: public interface IClothing { /// <summary> /// 获取服装 /// </summary> /// <returns></returns> string GetClothing(); } 实现服装接口: /// <summary> /// 夹克类 /// </summary> public class Jacket:IClothing { public string GetClothing() { return "夹克" ; } } /// <summary> /// 牛仔衣 /// </summary> public class JeansWear:IClothing { public string GetClothing() { return "牛仔衣服" ; } } 然后枚举控制: public enum ClothingEnum { jacket, //夹克 JeansWear, //牛仔衣 } 然后简单工厂: /// <summary> /// 对服装进行统一管理的工厂类 /// </summary> public class FacClothing { private static IClothing cloth; public static IClothing GetClothing(ClothingEnum clo) { switch (clo) { case ClothingEnum.jacket: //夹克 cloth = new Jacket(); break ; case ClothingEnum.JeansWear: //牛仔衣 cloth = new JeansWear(); break ; } return cloth; } } |
然后颜色工厂实现:
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 | 颜色接口: /// <summary> /// 颜色接口 /// </summary> public interface IColor { string GetColor(); } 实现颜色接口: /// <summary> /// 绿色 /// </summary> public class Green:IColor { public string GetColor() { return "绿色" ; } } /// <summary> /// 黄色 /// </summary> public class Yellow:IColor { /// <summary> /// 颜色 /// </summary> /// <returns></returns> public string GetColor() { return "黄色" ; } } 枚举控制: public enum ColorEnum { yellow, //黄色 green //绿色 } 颜色简单工厂: /// <summary> /// 颜色工厂类 /// </summary> public class FacColor { private static IColor color; public static IColor GetColor(ColorEnum col) { switch (col) { case ColorEnum.yellow: color = new Yellow(); break ; case ColorEnum.green:color = new Green(); break ; } return color; } } |
到这里颜色,服装已经可以全局进行更改了,那么有一个问题,如果要进行统一搭配呢?比如qq的换皮肤,一换换一整套的;或者说空调配置,修改空调配置有几种方式,如tcp、udp,如组播、广播;如485、以太网;要想进行统一调配就需要再建一共大工厂,对这些小工厂进行管理;
首先定义抽象工厂接口: /// <summary> /// 抽象工厂 /// </summary> public interface IFactory { /// <summary> /// 获取颜色 /// </summary> /// <returns></returns> IColor GetColor(); /// <summary> /// 获取衣服 /// </summary> /// <returns></returns> IClothing GetClothing(); } 然后实现它: public class WithA : IFactory { string type = System.Configuration.ConfigurationManager.AppSettings["AbstractFication"].ToString(); /// <summary> /// 根据配置获取颜色搭配 /// </summary> /// <returns></returns> public IColor GetColor() { IColor color = null; switch (type) { case "1": color = FacColor.GetColor(ColorEnum.green); break; case "2": color = FacColor.GetColor(ColorEnum.yellow); break; } return color; } /// <summary> /// 根据配置获取衣服搭配 /// </summary> /// <returns></returns> public IClothing GetClothing() { IClothing clothing = null; switch (type) { case "1": clothing = FacClothing.GetClothing(ClothingEnum.jacket); break; case "2": clothing = FacClothing.GetClothing(ClothingEnum.JeansWear); break; } return clothing; } } 上面这里呢我是直接用配置进行控制的: <!--抽象工厂类参数 1为搭配一--> <add key="AbstractFication" value="1"/>
那如果不用配置去控制那就需要多创建几种实现来完成了,我也不知道哪种更好,感觉多创建几种实现类来对应不同的操作会好些,但是又觉得那样会创建很多类,用配置信息来控制就会减少一些类
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!