.NET設計模式(2):1.2 抽象工廠模式(Abstract Factory)

来源:http://www.cnblogs.com/seayxu/archive/2016/04/17/design-pattern-for-dotnet-abstract-factory.html
-Advertisement-
Play Games

![大話設計模式 抽象工廠模式 結構圖][1] 概述 抽象工廠模式(Abstract Factory)是所有形態的工廠模式中最為抽象和最具一般性的一種形態。抽象工廠模式是指當有多個抽象角色時,使用的一種工廠模式。抽象工廠模式可以向客戶端提供一個介面,使客戶端在不必指定產品的具體的情況下,創建多個產品 ...


大話設計模式-抽象工廠模式-結構圖

概述

抽象工廠模式(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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactorySamlpe.Blog.Models
{
    public class UserModel
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
    }
}
  • PostModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactorySamlpe.Blog.Models
{
    public class PostModel
    {
        public int PostId { get; set; }
        public string PostTitle { get; set; }
        public string PostContent { get; set; }
        public DateTime PostTime{ get; set; }
        public int PostUser { get; set; }
    }
}

數據訪問層介面(Blog.IDAL)

  • IUserDAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.IDAL
{
    public interface IUserDAL
    {
        bool Insert(UserModel model);

        UserModel GetById(int id);

        bool Update(UserModel model);

        bool Delete(int id);
    }
}
  • IPostDAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.IDAL
{
    public interface IPostDAL
    {
        bool Insert(PostModel model);

        PostModel GetById(int id);

        bool Update(PostModel model);

        bool Delete(int id);
    }
}

數據訪問層分類具體實現(Blog.DAL)

  • Blog.DAL.MsSql
  • MsSqlPostDAL
using AbstractFactorySamlpe.Blog.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.DAL.MsSql
{
    public class MsSqlPostDAL : IPostDAL
    {
        public bool Delete(int id)
        {
            return true;
        }

        public PostModel GetById(int id)
        {
            return new PostModel();
        }

        public bool Insert(PostModel model)
        {
            return true;
        }

        public bool Update(PostModel model)
        {
            return true;
        }
    }
}
  • MsSqlUserDAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.IDAL;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.DAL.MsSql
{
    public class MsSqlUserDAL : IUserDAL
    {
        public bool Delete(int id)
        {
            return true;
        }

        public UserModel GetById(int id)
        {
            return new UserModel();
        }

        public bool Insert(UserModel model)
        {
            return true;
        }

        public bool Update(UserModel model)
        {
            return true;
        }
    }
}
  • Blog.DAL.MySql
  • MySqlPostDAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.IDAL;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.DAL.MySql
{
    public class MySqlPostDAL : IPostDAL
    {
        public bool Delete(int id)
        {
            return true;
        }

        public PostModel GetById(int id)
        {
            return new PostModel();
        }

        public bool Insert(PostModel model)
        {
            return true;
        }

        public bool Update(PostModel model)
        {
            return true;
        }
    }
}
  • MySqlUserDAL
using AbstractFactorySamlpe.Blog.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.DAL.MySql
{
    public class MySqlUserDAL : IUserDAL
    {
        public bool Delete(int id)
        {
            return true;
        }

        public UserModel GetById(int id)
        {
            return new UserModel();
        }

        public bool Insert(UserModel model)
        {
            return true;
        }

        public bool Update(UserModel model)
        {
            return true;
        }
    }
}

抽象工廠(Blog.Factory)

  • IFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.IDAL;

namespace AbstractFactorySamlpe.Blog.Factory
{
    public interface IFactory
    {

        IUserDAL CreateUser();

        IPostDAL CreatePost();
    }
}
  • MsSqlFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.DAL.MsSql;
using AbstractFactorySamlpe.Blog.IDAL;

namespace AbstractFactorySamlpe.Blog.Factory
{
    public class MsSqlFactory : IFactory
    {
        public IPostDAL CreatePost()
        {
            return new MsSqlPostDAL();
        }

        public IUserDAL CreateUser()
        {
            return new MsSqlUserDAL();
        }
    }
}
  • MySqlFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.DAL.MySql;
using AbstractFactorySamlpe.Blog.IDAL;

namespace AbstractFactorySamlpe.Blog.Factory
{
    public class MySqlFactory : IFactory
    {
        public IPostDAL CreatePost()
        {
            return new MySqlPostDAL();
        }

        public IUserDAL CreateUser()
        {
            return new MySqlUserDAL();
        }
    }
}

調用(Client)

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Factory;
using AbstractFactorySamlpe.Blog.IDAL;

namespace AbstractFactorySamlpe
{
    class Program
    {
        static void Main(string[] args)
        {
            //MSSQL
            //IFactory factory = new MsSqlFactory();
            //IUserDAL UserDAL = factory.CreateUser();
            //IPostDAL PostDAL = factory.CreatePost();

            //MySQL
            IFactory factory = new MySqlFactory();
            IUserDAL UserDAL = factory.CreateUser();
            IPostDAL PostDAL = factory.CreatePost();
        }
    }
}

Client

優點:

1、封裝性,每個產品的實現類不是高層模塊要關心的。
2、產品族內的約束為非公開狀態。

缺點:

產品族擴展非常困難。

使用場景:

一個對象族或是一組沒有任何關係的對象都有相同的約束,則可以使用抽象工廠模式。

註意事項:
產品族擴展困難,產品等級非常容易擴展,也就是橫向擴展容易,縱向擴展困難。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1.什麼時候可以選擇final修飾符 如果想讓一個類不被其他類繼承,不允許在有子類,這時候就要考慮用到final來修飾。 2.用final修飾的類 首先大家要明白,用final修飾的類是不能被繼承的,下麵來看一個錯誤案例。 eg: 此時這段代碼的class SubPenguin extends Pe ...
  • 引入命名空間後 為什麼不用指定文件路徑? ...
  • 一、繼承 1、作用:繼承是復用程式代碼的有力手段。 2、理解繼承:當有多個類之間存在相同的屬性時,我們可以在這些類中抽取一個父類,當子類繼承父類時,子類就能使用父類所有的方法和屬性。(子類就無需定義與父類相同的屬性) 二、繼承的基本語法 1、在java語言中,用關鍵字Extends來表示一個類繼承另 ...
  • 思路: Python的實現 要點: ...
  • 一、選擇伺服器放在哪個區域 對於Google而言,不同的區域,搜索結果的就不同.一些英文關鍵詞, 你在中國用Google搜索,發現你網站的排名非產靠前,但是如果你在國外用Google, 你可能翻十頁都找不到你的網站. 我們做Google排名的排名效果不是讓我們自己看的, 而是讓我們的潛在客戶看的. ...
  • 外觀模式(Facade) 定義 外觀模式(Facade),為子系統中的一組介面提供一個一致的界面,此模式定義了一個高層介面,這個介面使得這一子系統更加容易使用。 類圖 描述 Facade:外觀類,外觀類知道哪些子系統類負責處理請求,將用戶的請求代理給恰當的子系統對象。 Classes:子系統類,實現 ...
  • 數組概念 數組是存儲同一種數據類型多個元素的集合。也可以看成是一個容器。數組既可以存儲基本數據類型,也可以存儲引用數據類型。 數組的初始化 動態初始化:初始化時只指定數組長度,由系統為數組分配初始值。格式:數據類型[] 數組名 = new 數據類型[數組長度]; 數組長度其實就是數組中元素的個數。舉 ...
  • 一、什麼是nginx 是一個C語言開發的HTTP反向代理伺服器,性能非常高 一個俄羅斯的哥們開發的,官方提供的測試性能能夠達到5W的併發,我的天吶~,實際測試差不多是2W,而淘寶的牛人可以優化到200W 運行效率非常好,占用的資源也非常低,運行穩定 二、Nginx的應用場景 有哪些 1、http服務 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...