抽象工厂模式,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

抽象工厂允许客户使用抽象的接口来创建一组相关产品,而不需要知道或关心实际生产出的具体产品是什么。这样客户就可以从具体产品中被解耦。下面通过抽象工模式的类图来了解各个类中之间的关系:

抽象工厂的分析

抽象工厂模式将具体产品的创建延迟到具体工厂的子类中,这样将对象的创建封装起来,可以减少客户端与具体产品类之间的依赖,从而使系统耦合度低,这 样更有利于后期的维护和扩展,这真是抽象工厂模式的优点所在,然后抽象模式同时也存在不足的地方。下面就具体看下抽象工厂的缺点(缺点其实在前面的介绍中 以已经涉及了):

抽象工厂模式很难支持新种类产品的变化。这是因为抽象工厂接口中已经确定了可以被创建的产品集合,如果需要添加新产品,此时就必须去修改抽象工厂的接口,这样就涉及到抽象工厂类的以及所有子类的改变,这样也就违背了“开发——封闭”原则。

知道了抽象工厂的优缺点之后,也就能很好地把握什么情况下考虑使用抽象工厂模式了,下面就具体看看使用抽象工厂模式的系统应该符合那几个前提:

  • 一个系统不要求依赖产品类实例如何被创建、组合和表达的表达,这点也是所有工厂模式应用的前提。
  • 这个系统有多个系列产品,而系统中只消费其中某一系列产品
  • 系统要求提供一个产品类的库,所有产品以同样的接口出现,客户端不需要依赖具体实现。

C#抽象工厂-多数据库:

  1. namespace 抽象工厂多数据库
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. User user = new User();
  8. Department dept = new Department();
  9.  
  10. //AbstractFactory factory = new SqlServerFactory();
  11. IFactory factory = new AccessFactory();
  12. IUser iu = factory.CreateUser();
  13.  
  14. iu.Insert(user);
  15. iu.GetUser();
  16.  
  17. IDepartment id = factory.CreateDepartment();
  18. id.Insert(dept);
  19. id.GetDepartment();
  20.  
  21. Console.Read();
  22. }
  23. }
  24.  
  25. class User
  26. {
  27. private int _id;
  28. public int ID
  29. {
  30. get { return _id; }
  31. set { _id = value; }
  32. }
  33.  
  34. private string _name;
  35. public string Name
  36. {
  37. get { return _name; }
  38. set { _name = value; }
  39. }
  40. }
  41.  
  42. class Department
  43. {
  44. private int _id;
  45. public int ID
  46. {
  47. get { return _id; }
  48. set { _id = value; }
  49. }
  50.  
  51. private string _deptName;
  52. public string DeptName
  53. {
  54. get { return _deptName; }
  55. set { _deptName = value; }
  56. }
  57. }
  58.  
  59. interface IUser
  60. {
  61. void Insert(User user);
  62.  
  63. User GetUser(int id);
  64. }
  65.  
  66. class SqlserverUser : IUser
  67. {
  68. public void Insert(User user)
  69. {
  70. Console.WriteLine("在Sqlserver中给User表增加一条记录");
  71. }
  72.  
  73. public User GetUser(int id)
  74. {
  75. Console.WriteLine("在Sqlserver中根据ID得到User表一条记录");
  76. return null;
  77. }
  78. }
  79.  
  80. class AccessUser : IUser
  81. {
  82. public void Insert(User user)
  83. {
  84. Console.WriteLine("在Access中给User表增加一条记录");
  85. }
  86.  
  87. public User GetUser(int id)
  88. {
  89. Console.WriteLine("在Access中根据ID得到User表一条记录");
  90. return null;
  91. }
  92. }
  93.  
  94. interface IDepartment
  95. {
  96. void Insert(Department department);
  97.  
  98. Department GetDepartment(int id);
  99. }
  100.  
  101. class SqlserverDepartment : IDepartment
  102. {
  103. public void Insert(Department department)
  104. {
  105. Console.WriteLine("在Sqlserver中给Department表增加一条记录");
  106. }
  107.  
  108. public Department GetDepartment(int id)
  109. {
  110. Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录");
  111. return null;
  112. }
  113. }
  114.  
  115. class AccessDepartment : IDepartment
  116. {
  117. public void Insert(Department department)
  118. {
  119. Console.WriteLine("在Access中给Department表增加一条记录");
  120. }
  121.  
  122. public Department GetDepartment(int id)
  123. {
  124. Console.WriteLine("在Access中根据ID得到Department表一条记录");
  125. return null;
  126. }
  127. }
  128.  
  129. interface IFactory
  130. {
  131. IUser CreateUser();
  132.  
  133. IDepartment CreateDepartment();
  134. }
  135.  
  136. class SqlServerFactory : IFactory
  137. {
  138. public IUser CreateUser()
  139. {
  140. return new SqlserverUser();
  141. }
  142.  
  143. public IDepartment CreateDepartment()
  144. {
  145. return new SqlserverDepartment();
  146. }
  147. }
  148.  
  149. class AccessFactory : IFactory
  150. {
  151. public IUser CreateUser()
  152. {
  153. return new AccessUser();
  154. }
  155.  
  156. public IDepartment CreateDepartment()
  157. {
  158. return new AccessDepartment();
  159. }
  160. }
  161. }

C#抽象工厂-用简单工厂来改进:

  1. namespace 抽象工厂用简单工厂来改进
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. User user = new User();
  8. Department dept = new Department();
  9.  
  10. IUser iu = DataAccess.CreateUser();
  11.  
  12. iu.Insert(user);
  13. iu.GetUser();
  14.  
  15. IDepartment id = DataAccess.CreateDepartment();
  16. id.Insert(dept);
  17. id.GetDepartment();
  18.  
  19. Console.Read();
  20. }
  21. }
  22.  
  23. class User
  24. {
  25. private int _id;
  26. public int ID
  27. {
  28. get { return _id; }
  29. set { _id = value; }
  30. }
  31.  
  32. private string _name;
  33. public string Name
  34. {
  35. get { return _name; }
  36. set { _name = value; }
  37. }
  38. }
  39.  
  40. class Department
  41. {
  42. private int _id;
  43. public int ID
  44. {
  45. get { return _id; }
  46. set { _id = value; }
  47. }
  48.  
  49. private string _deptName;
  50. public string DeptName
  51. {
  52. get { return _deptName; }
  53. set { _deptName = value; }
  54. }
  55. }
  56.  
  57. interface IUser
  58. {
  59. void Insert(User user);
  60.  
  61. User GetUser(int id);
  62. }
  63.  
  64. class SqlserverUser : IUser
  65. {
  66. public void Insert(User user)
  67. {
  68. Console.WriteLine("在Sqlserver中给User表增加一条记录");
  69. }
  70.  
  71. public User GetUser(int id)
  72. {
  73. Console.WriteLine("在Sqlserver中根据ID得到User表一条记录");
  74. return null;
  75. }
  76. }
  77.  
  78. class AccessUser : IUser
  79. {
  80. public void Insert(User user)
  81. {
  82. Console.WriteLine("在Access中给User表增加一条记录");
  83. }
  84.  
  85. public User GetUser(int id)
  86. {
  87. Console.WriteLine("在Access中根据ID得到User表一条记录");
  88. return null;
  89. }
  90. }
  91.  
  92. interface IDepartment
  93. {
  94. void Insert(Department department);
  95.  
  96. Department GetDepartment(int id);
  97. }
  98.  
  99. class SqlserverDepartment : IDepartment
  100. {
  101. public void Insert(Department department)
  102. {
  103. Console.WriteLine("在Sqlserver中给Department表增加一条记录");
  104. }
  105.  
  106. public Department GetDepartment(int id)
  107. {
  108. Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录");
  109. return null;
  110. }
  111. }
  112.  
  113. class AccessDepartment : IDepartment
  114. {
  115. public void Insert(Department department)
  116. {
  117. Console.WriteLine("在Access中给Department表增加一条记录");
  118. }
  119.  
  120. public Department GetDepartment(int id)
  121. {
  122. Console.WriteLine("在Access中根据ID得到Department表一条记录");
  123. return null;
  124. }
  125. }
  126.  
  127. class DataAccess
  128. {
  129. private static readonly string AssemblyName = "抽象工厂用简单工厂来改进";
  130. private static readonly string db = ConfigurationManager.AppSettings["DB"];
  131.  
  132. public static IUser CreateUser()
  133. {
  134. string className = AssemblyName + "." + db + "User";
  135. return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);
  136. }
  137.  
  138. public static IDepartment CreateDepartment()
  139. {
  140. string className = AssemblyName + "." + db + "Department";
  141. return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className);
  142. }
  143. }
  144. }

js抽象工厂模式的更多相关文章

  1. 浅谈js抽象工厂模式

    一.简单工厂 定义:简单工厂模式中,可以根据参数的不同返回不同类的实例.简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类. 比如你去专门卖鼠标的地方你可以买各种各样的 ...

  2. JS 简单工厂模式,工厂模式(二)

    一.什么是工厂模式: 工厂模式就是用来创建对象的一种最常用的设计模式,我们不暴露创建对象的具体逻辑,而是将逻辑封装到一个函数中,那么,这个函数 就可以被视为一个工厂.那么,在实际项目中,我们是不是可以 ...

  3. PHP设计模式(三)抽象工厂模式(Abstract Factory For PHP)

    一.什么是抽象工厂模式 抽象工厂模式的用意为:给客户端提供一个接口,可以创建多个产品族中的产品对象 ,而且使用抽象工厂模式还要满足以下条件: 系统中有多个产品族,而系统一次只可能消费其中一族产品. 同 ...

  4. 面向对象设计模式纵横谈:Abstract Factory 抽象工厂模式(笔记记录)

         今天是设计模式的第二讲,抽象工厂的设计模式,我们还是延续老办法,一步一步的.演变的来讲,先来看看一个对象创建的问题. 1.如何创建一个对象 常规的对象创建方法: 这样的创建对象没有任何问题, ...

  5. Objective-C 工厂模式(下) -- 抽象工厂模式

    相比简单工厂模式, 只有一个工厂 能生产的手机也是固定的 抽象工厂模式类似于有很多家工厂, 当用户要买什么手机就创建对应的工厂去生产 比如用户要买iPhone就创建一个Apple工厂来生产手机, 要买 ...

  6. Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...

  7. .NET设计模式(2):1.2 抽象工厂模式(Abstract Factory)

    概述 抽象工厂模式(Abstract Factory)是所有形态的工厂模式中最为抽象和最具一般性的一种形态.抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式.抽象工厂模式可以向客户端提供一个接口 ...

  8. 设计模式(四)抽象工厂模式(Abstract Factory Pattern)

    一.引言 在上一专题中介绍了工厂方法模式,工厂方法模式是为了克服简单工厂模式的缺点而设计出来的,简单工厂模式的工厂类随着产品类的增加需要增加额外的代码,而工厂方法模式每个具体工厂类只完成单个实例的创建 ...

  9. php实现设计模式之 抽象工厂模式

    <?php /*抽象工厂模式:提供一个创建一系统相关或相互依赖对象的接口,而无需指定它们具体的类 * 创建型模式 */ //抽象小米工厂,能制造小米一,小米二 abstract class mi ...

随机推荐

  1. Spring 事务机制详解(事务的隔离性和传播性)

    原文出处: 陶邦仁 Spring事务机制主要包括声明式事务和编程式事务,此处侧重讲解声明式事务,编程式事务在实际开发中得不到广泛使用,仅供学习参考. Spring声明式事务让我们从复杂的事务处理中得到 ...

  2. 密钥管理服务KMS

    密钥管理服务 KMS - 腾讯云 https://cloud.tencent.com/product/kms

  3. C++常备知识总结

    1.extern表示是外部函数或外部变量,比如: 1.extern void add(int x,inty);表示该函数主体不在当前模块中,在另一个模块中(文件)2.extern int total; ...

  4. 2014-08-28——移动端,触摸事件 touchstart、touchmove、touchend、touchcancel

    1.Touch事件简介在移动终端上的web页面触屏时会产生ontouchstart.ontouchmove.ontouchend.ontouchcancel 事件,分别对应了触屏开始.拖拽及完成触屏事 ...

  5. AWS入门-1

    对于 Amazon Linux AMI,用户名为 ec2-user. 对于 RHEL AMI,用户名称是 ec2-user 或 root. 对于 Ubuntu AMI,用户名称是 ubuntu 或 r ...

  6. ACM中使用 JAVA v2. 1

    ACM中使用JAVA v2.1 严明超 (Blog:mingchaoyan.blogbus.com Email:mingchaoyan@gmail.com) 0.前 言 文前声明:本文只谈java用于 ...

  7. COM 组件创建实例失败,原因是出现以下错误: c001f011 (Microsoft.SqlServer.ManagedDTS)

    从 IClassFactory 为 CLSID 为 {AA40D1D6-CAEF-4A56-B9BB-D0D3DC976BA2} 的 COM 组件创建实例失败,原因是出现以下错误: c001f011. ...

  8. 005-搭建框架-实现AOP机制【二】AOP技术

    一.什么是AOP aspect-oriented  programming,面向切面编程,对oop的一种补充. 著名示例,aspectj,spring+aspectj 二.aop之代码重构 2.1.代 ...

  9. comboBox绑定字典Dictionary 获取value中的值

    第一种 最简洁的方法 Dictionary<string, string> list = new Dictionary<string, string> { {"thi ...

  10. 4.1 使用STM32控制MC20拨打电话

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...