概述

抽象工厂模式(Abstract Factory)是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。


定义

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


UML图解

  • AbstractFactory:声明一个创建抽象产品对象的操作接口

  • ConcreteFactory:实现创建具体产品对象的操作

  • AbstractProduct:声明一类产品对象接口

  • Product

    • 定义一个被相应具体工厂创建的产品对象

    • 实现AbstractProduct接口

  • Client:使用AbstractFactory和AbstractProduct类声明的接口

  在抽象工厂模式中,产品的创建由ConcreteFactory来完成,从结构图中可以看出,抽象工厂模式的ConcreteFactory不是负责一种具体Product的创建,而是负责一个Product族的创建。


实现

说明:

1.本次实现以博客系统为例,抽取其中的用户和文章两个对象进行说明。

2.采用三层架构,为了省事,去掉了业务逻辑层(BLL),希望理解。

示例项目结构图

示例项目类图

  • Blog.Models:模型层
  • Blog.IDAL:数据访问层接口(一类产品对象接口[AbstractProduct])
  • Blog.DAL:数据访问层分类实现(抽象产品具体分类实现)
    • MsSql:MsSql实现(抽象产品具体分类实现[ProductA1,ProductA2])
    • MySql:MySql实现(抽象产品具体分类实现[ProductB1,ProductB2])
  • Blog.Factory:抽象工厂层
    • lFactory:抽象工厂接口([AbstractFactory])
    • MsSqlFactory:MsSql具体工厂(创建具体产品对象的操作[ConcreteFactory1])
    • MySqlFactory:MySql具体工厂(创建具体产品对象的操作[ConcreteFactory2])

模型层(Blog.Models)

  • UserModel
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AbstractFactorySamlpe.Blog.Models
  7. {
  8. public class UserModel
  9. {
  10. public int UserId { get; set; }
  11. public string UserName { get; set; }
  12. public string Password { get; set; }
  13. }
  14. }
  • PostModel
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AbstractFactorySamlpe.Blog.Models
  7. {
  8. public class PostModel
  9. {
  10. public int PostId { get; set; }
  11. public string PostTitle { get; set; }
  12. public string PostContent { get; set; }
  13. public DateTime PostTime{ get; set; }
  14. public int PostUser { get; set; }
  15. }
  16. }

数据访问层接口(Blog.IDAL)

  • IUserDAL
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AbstractFactorySamlpe.Blog.Models;
  7. namespace AbstractFactorySamlpe.Blog.IDAL
  8. {
  9. public interface IUserDAL
  10. {
  11. bool Insert(UserModel model);
  12. UserModel GetById(int id);
  13. bool Update(UserModel model);
  14. bool Delete(int id);
  15. }
  16. }
  • IPostDAL
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AbstractFactorySamlpe.Blog.Models;
  7. namespace AbstractFactorySamlpe.Blog.IDAL
  8. {
  9. public interface IPostDAL
  10. {
  11. bool Insert(PostModel model);
  12. PostModel GetById(int id);
  13. bool Update(PostModel model);
  14. bool Delete(int id);
  15. }
  16. }

数据访问层分类具体实现(Blog.DAL)

  • Blog.DAL.MsSql

    • MsSqlPostDAL
  1. using AbstractFactorySamlpe.Blog.IDAL;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using AbstractFactorySamlpe.Blog.Models;
  8. namespace AbstractFactorySamlpe.Blog.DAL.MsSql
  9. {
  10. public class MsSqlPostDAL : IPostDAL
  11. {
  12. public bool Delete(int id)
  13. {
  14. return true;
  15. }
  16. public PostModel GetById(int id)
  17. {
  18. return new PostModel();
  19. }
  20. public bool Insert(PostModel model)
  21. {
  22. return true;
  23. }
  24. public bool Update(PostModel model)
  25. {
  26. return true;
  27. }
  28. }
  29. }
  • MsSqlUserDAL
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AbstractFactorySamlpe.Blog.IDAL;
  7. using AbstractFactorySamlpe.Blog.Models;
  8. namespace AbstractFactorySamlpe.Blog.DAL.MsSql
  9. {
  10. public class MsSqlUserDAL : IUserDAL
  11. {
  12. public bool Delete(int id)
  13. {
  14. return true;
  15. }
  16. public UserModel GetById(int id)
  17. {
  18. return new UserModel();
  19. }
  20. public bool Insert(UserModel model)
  21. {
  22. return true;
  23. }
  24. public bool Update(UserModel model)
  25. {
  26. return true;
  27. }
  28. }
  29. }
  • Blog.DAL.MySql

    • MySqlPostDAL
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AbstractFactorySamlpe.Blog.IDAL;
  7. using AbstractFactorySamlpe.Blog.Models;
  8. namespace AbstractFactorySamlpe.Blog.DAL.MySql
  9. {
  10. public class MySqlPostDAL : IPostDAL
  11. {
  12. public bool Delete(int id)
  13. {
  14. return true;
  15. }
  16. public PostModel GetById(int id)
  17. {
  18. return new PostModel();
  19. }
  20. public bool Insert(PostModel model)
  21. {
  22. return true;
  23. }
  24. public bool Update(PostModel model)
  25. {
  26. return true;
  27. }
  28. }
  29. }
  • MySqlUserDAL
  1. using AbstractFactorySamlpe.Blog.IDAL;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using AbstractFactorySamlpe.Blog.Models;
  8. namespace AbstractFactorySamlpe.Blog.DAL.MySql
  9. {
  10. public class MySqlUserDAL : IUserDAL
  11. {
  12. public bool Delete(int id)
  13. {
  14. return true;
  15. }
  16. public UserModel GetById(int id)
  17. {
  18. return new UserModel();
  19. }
  20. public bool Insert(UserModel model)
  21. {
  22. return true;
  23. }
  24. public bool Update(UserModel model)
  25. {
  26. return true;
  27. }
  28. }
  29. }

抽象工厂(Blog.Factory)

  • IFactory
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AbstractFactorySamlpe.Blog.IDAL;
  7. namespace AbstractFactorySamlpe.Blog.Factory
  8. {
  9. public interface IFactory
  10. {
  11. IUserDAL CreateUser();
  12. IPostDAL CreatePost();
  13. }
  14. }
  • MsSqlFactory
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AbstractFactorySamlpe.Blog.DAL.MsSql;
  7. using AbstractFactorySamlpe.Blog.IDAL;
  8. namespace AbstractFactorySamlpe.Blog.Factory
  9. {
  10. public class MsSqlFactory : IFactory
  11. {
  12. public IPostDAL CreatePost()
  13. {
  14. return new MsSqlPostDAL();
  15. }
  16. public IUserDAL CreateUser()
  17. {
  18. return new MsSqlUserDAL();
  19. }
  20. }
  21. }
  • MySqlFactory
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AbstractFactorySamlpe.Blog.DAL.MySql;
  7. using AbstractFactorySamlpe.Blog.IDAL;
  8. namespace AbstractFactorySamlpe.Blog.Factory
  9. {
  10. public class MySqlFactory : IFactory
  11. {
  12. public IPostDAL CreatePost()
  13. {
  14. return new MySqlPostDAL();
  15. }
  16. public IUserDAL CreateUser()
  17. {
  18. return new MySqlUserDAL();
  19. }
  20. }
  21. }

调用(Client)

Program

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AbstractFactorySamlpe.Blog.Factory;
  7. using AbstractFactorySamlpe.Blog.IDAL;
  8. namespace AbstractFactorySamlpe
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //MSSQL
  15. //IFactory factory = new MsSqlFactory();
  16. //IUserDAL UserDAL = factory.CreateUser();
  17. //IPostDAL PostDAL = factory.CreatePost();
  18. //MySQL
  19. IFactory factory = new MySqlFactory();
  20. IUserDAL UserDAL = factory.CreateUser();
  21. IPostDAL PostDAL = factory.CreatePost();
  22. }
  23. }
  24. }

优点:

1、封装性,每个产品的实现类不是高层模块要关心的。

2、产品族内的约束为非公开状态。

缺点:

产品族扩展非常困难。

使用场景:

一个对象族或是一组没有任何关系的对象都有相同的约束,则可以使用抽象工厂模式。

注意事项:

产品族扩展困难,产品等级非常容易扩展,也就是横向扩展容易,纵向扩展困难。

.NET设计模式(2):1.2 抽象工厂模式(Abstract Factory)的更多相关文章

  1. 设计模式之十一:抽象工厂模式(Abstract Factory)

    抽象工厂模式: 提供了一个创建一系列相关的或相互依赖的对象的接口而不须要详细指定它们的类型. Provide an interface for creating families of related ...

  2. 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

    原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...

  3. 【设计模式】抽象工厂模式 Abstract Factory Pattern

    简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...

  4. 设计模式 - 抽象工厂模式(abstract factory pattern) 具体解释

    抽象工厂模式(abstract factory pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2709 ...

  5. 二十四种设计模式:抽象工厂模式(Abstract Factory Pattern)

    抽象工厂模式(Abstract Factory Pattern) 介绍提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 示例有Message和MessageModel,Messag ...

  6. 【UE4 设计模式】抽象工厂模式 Abstract Factory Pattern

    概述 描述 提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类:具体的工厂负责实现具体的产品实例 抽象工厂中每个工厂可以创建多种产品(如苹果公司生产iPhone.iPad): 工厂方法 ...

  7. 抽象工厂模式(Abstract Factory)C#实例

    抽象工厂模式(Abstract Factory)C#实例 本文出处http://www.dofactory.com/net/abstract-factory-design-pattern 一.场景描述 ...

  8. 设计模式08---设计模式之抽象工厂模式(Abstract Factory)

    1.场景模拟 举个生活中常见的例子:组装电脑,我们在组装电脑的时候,通常要选择一系列的配件,比如选择CPU时候,需要注意品牌,型号,针脚数目,主频,只有这些都确定下来,才能确定一个CPU.同样,主板也 ...

  9. 设计模式的征途—3.抽象工厂(Abstract Factory)模式

    上一篇的工厂方法模式引入了工厂等级结构,解决了在原来简单工厂模式中工厂类职责太重的原则,但是由于工厂方法模式的每个工厂只生产一类产品,可能会导致系统中存在大量的工厂类,从而增加系统开销.那么,我们应该 ...

随机推荐

  1. LoadingView

    // //  LoadingView.h //  蓝桥播报 // //  Created by 小小建 on 15/7/10. //  Copyright (c) 2015年 蓝桥. All righ ...

  2. WebApiThrottle限流框架使用手册

    阅读目录: 介绍 基于IP全局限流 基于IP的端点限流 基于IP和客户端key的端点限流 IP和客户端key的白名单 IP和客户端key自定义限制频率 端点自定义限制频率 关于被拒请求的计数器 在we ...

  3. 求两点之间最短路径-Dijkstra算法

     Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.D ...

  4. SQL Azure (16) 创建PaaS SQL Azure V12数据库

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China 最新的Azure PaaS SQL Database(SQL Azu ...

  5. PHP fsockopen 异步调用接口在nginx上偶尔实效的情况

    private function fsock_asy_do($get){ $fp = fsockopen("ssl://www.xxx.com", 443, $errno, $er ...

  6. 如果用css的border属性画一个三角形

    假设页面中有个div,如何通过css做一个三角形.这是我们项目中用到的今天就稍微总结下.顺便说一句偷偷写博客的感觉还挺爽 div { width: 0; height: 0; border-top: ...

  7. WPF入门教程系列二十二——DataGrid示例(二)

    DataGrid示例的后台代码 1)  通过Entity Framework 6.1 从数据库(本地数据库(local)/Test中的S_City表中读取城市信息数据,从S_ Province表中读取 ...

  8. windows server 2008 r2 企业版 hyper v做虚拟化的相关问题处理

    windows server 2008 r2 企业版 hyper v做虚拟化的相关问题处理 今天在dell r710 上用windows server 2008 r2企业版hyper v 做虚拟化,添 ...

  9. VS创建MVC出错解决方法

    搞定

  10. VS2015签名丢失解决办法

    废话少说,直接上问题和解决办法, 问题:VS2015安装后运行程序会发现这个问题 解决办法 1.下载SignTool.exe签名文件 2.将签名文件放入 C:\Program Files (x86)\ ...