抽象工廠模式(Abstract Factory)是全部形態的工廠模式中最爲抽象和最具通常性的一種形態。抽象工廠模式是指當有多個抽象角色時,使用的一種工廠模式。抽象工廠模式能夠向客戶端提供一個接口,使客戶端在沒必要指定產品的具體的狀況下,建立多個產品族中的產品對象。設計模式
抽象工廠模式(Abstract Factory),提供一個建立一系列相關或相互依賴對象的接口,而無需指定它們具體的類。架構
AbstractFactory:聲明一個建立抽象產品對象的操做接口spa
ConcreteFactory:實現建立具體產品對象的操做設計
AbstractProduct:聲明一類產品對象接口code
Product對象
定義一個被相應具體工廠建立的產品對象接口
實現AbstractProduct接口get
Client:使用AbstractFactory和AbstractProduct類聲明的接口博客
在抽象工廠模式中,產品的建立由ConcreteFactory來完成,從結構圖中能夠看出,抽象工廠模式的ConcreteFactory不是負責一種具體Product的建立,而是負責一個Product族的建立。string
說明: 1.本次實現以博客系統爲例,抽取其中的用戶和文章兩個對象進行說明。 2.採用三層架構,爲了省事,去掉了業務邏輯層(BLL),但願理解。
示例項目結構圖
示例項目類圖
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; } } }
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; } } }
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); } }
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); } }
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; } } }
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; } } }
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; } } }
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; } } }
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(); } }
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(); } } }
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(); } } }
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(); } } }
一、封裝性,每一個產品的實現類不是高層模塊要關心的。 二、產品族內的約束爲非公開狀態。
產品族擴展很是困難。
一個對象族或是一組沒有任何關係的對象都有相同的約束,則能夠使用抽象工廠模式。
注意事項: 產品族擴展困難,產品等級很是容易擴展,也就是橫向擴展容易,縱向擴展困難。