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

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

概述

抽象工廠模式(Abstract Factory)是全部形態的工廠模式中最爲抽象和最具通常性的一種形態。抽象工廠模式是指當有多個抽象角色時,使用的一種工廠模式。抽象工廠模式能夠向客戶端提供一個接口,使客戶端在沒必要指定產品的具體的狀況下,建立多個產品族中的產品對象。設計模式


定義

抽象工廠模式(Abstract Factory),提供一個建立一系列相關或相互依賴對象的接口,而無需指定它們具體的類。架構


UML圖解

  • AbstractFactory:聲明一個建立抽象產品對象的操做接口spa

  • ConcreteFactory:實現建立具體產品對象的操做設計

  • AbstractProduct:聲明一類產品對象接口code

  • Product對象

    • 定義一個被相應具體工廠建立的產品對象接口

    • 實現AbstractProduct接口get

  • Client:使用AbstractFactory和AbstractProduct類聲明的接口博客

  在抽象工廠模式中,產品的建立由ConcreteFactory來完成,從結構圖中能夠看出,抽象工廠模式的ConcreteFactory不是負責一種具體Product的建立,而是負責一個Product族的建立。string


實現

說明: 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

優勢:

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

缺點:

產品族擴展很是困難。

使用場景:

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

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

相關文章
相關標籤/搜索