概述 抽象工廠模式為創建一組對象提供了一種解決方案。與工廠方法模式相比,抽象工廠模式中的具體工廠不只是創建一種產品,它負責創建一組產品。抽象工廠模式定義如下: 抽象工廠模式(Abstract Factory Pattern):提供一個創建一系列相關或相互依賴對象的介面,而無須指定它們具體的類。抽象工 ...
概述
抽象工廠模式為創建一組對象提供了一種解決方案。與工廠方法模式相比,抽象工廠模式中的具體工廠不只是創建一種產品,它負責創建一組產品。抽象工廠模式定義如下:
抽象工廠模式(Abstract Factory Pattern):提供一個創建一系列相關或相互依賴對象的介面,而無須指定它們具體的類。抽象工廠模式又稱為Kit模式,它是一種對象創建型模式。
描述需求
分別往SQLserver、mysql、sqliter資料庫中添加用戶和部門數據。
抽象工廠實現代碼
/// <summary> /// 頂層抽象工廠 /// </summary> public interface IFacatory { IDatabaseUser GetDatabaseUser(); IDatabaseDepartment GetDatabaseDepartment(); } /// <summary> /// 具體工廠 /// </summary> public class SqlliteFacatory : IFacatory { public IDatabaseDepartment GetDatabaseDepartment() { return new SqlliteDepartment(); } public IDatabaseUser GetDatabaseUser() { return new SqliteUser(); } } /// <summary> /// 具體工廠 /// </summary> public class MysqlFacatory : IFacatory { public IDatabaseDepartment GetDatabaseDepartment() { return new MysqlDepartment(); } public IDatabaseUser GetDatabaseUser() { return new MysqlUser(); } } /// <summary> /// 具體工廠 /// </summary> public class SqlserverFacatory : IFacatory { public IDatabaseDepartment GetDatabaseDepartment() { return new SqlServerDepartment(); } public IDatabaseUser GetDatabaseUser() { return new SqlServerUser(); } }
/// <summary> /// 用戶類 /// </summary> public class User { public string Name { get; set; } public int Id { get; set; } } /// <summary> /// 抽象的產品 /// </summary> public interface IDatabaseUser { void InsertUser(User user); User GetUser(int id); } /// <summary> /// 具體的產品 /// </summary> public class SqlServerUser: IDatabaseUser { public void InsertUser(User user) { Console.WriteLine($"插入了SqlServerUser:{user.Name}"); } public User GetUser(int id) { Console.WriteLine($"獲取了Id:{id}的用戶"); return null; } } /// <summary> /// 具體的產品 /// </summary> public class MysqlUser : IDatabaseUser { public void InsertUser(User user) { Console.WriteLine($"插入了MysqlUser:{user.Name}"); } public User GetUser(int id) { Console.WriteLine($"獲取了Id:{id}的用戶"); return null; } } /// <summary> /// 具體的產品 /// </summary> public class SqliteUser : IDatabaseUser { public void InsertUser(User user) { Console.WriteLine($"插入了SqliteUser:{user.Name}"); } public User GetUser(int id) { Console.WriteLine($"獲取了Id:{id}的用戶"); return null; } }
/// <summary> /// 部門類 /// </summary> public class Department { public string Name { get; set; } public int Id { get; set; } } /// <summary> /// 抽象的產品 /// </summary> public interface IDatabaseDepartment { void InsertDepartment(Department department); Department GetDepartment(int id); } /// <summary> /// 具體的產品 /// </summary> public class SqlServerDepartment : IDatabaseDepartment { public void InsertDepartment(Department department) { Console.WriteLine($"插入了SqlServerDepartment部門:{department.Name}"); } public Department GetDepartment(int id) { Console.WriteLine($"獲取了Id:{id}的部門"); return null; } } /// <summary> /// 具體的產品 /// </summary> public class MysqlDepartment : IDatabaseDepartment { public void InsertDepartment(Department department) { Console.WriteLine($"插入了MysqlDepartment部門:{department.Name}"); } public Department GetDepartment(int id) { Console.WriteLine($"獲取了Id:{id}的部門"); return null; } } /// <summary> /// 具體的產品 /// </summary> public class SqlliteDepartment : IDatabaseDepartment { public void InsertDepartment(Department department) { Console.WriteLine($"插入了SqlliteDepartment部門:{department.Name}"); } public Department GetDepartment(int id) { Console.WriteLine($"獲取了Id:{id}的部門"); return null; } }
//C#控制台調用 Console.WriteLine("抽象工廠"); User user = new(); user.Name = "mhg"; user.Id = 1; //變化點 IFacatory facatory=new SqlserverFacatory(); //MysqlFacatory //SqlliteFacatory IDatabaseUser databaseUser = facatory.GetDatabaseUser(); databaseUser.InsertUser(user); databaseUser?.GetUser(user.Id); Department department = new Department(); department.Name = "研發部"; department.Id = 2; IDatabaseDepartment databaseDepartment= facatory.GetDatabaseDepartment(); databaseDepartment.InsertDepartment(department); databaseDepartment?.GetDepartment(department.Id);
以上就用抽象工廠實現的全部代碼,該代碼中IFacatory做為抽象工廠,SqlserverFacatory、MysqlFacatory、SqliteFacatory作為具體的產品工廠來生產產品;IDatabaseUser和IDatabaseDepartment為抽象的產品分別生產具體的產品SqlServerUser、MysqlUser、SqliteUser和SqlServerDepartment、MysqlDepartment、SqlliteDepartment。
總結
這個需求也可以用工廠模式實現,但每增加一個產品,就必須增加一個工廠類,而用抽象工廠可以縮減創建子類工廠的數量,不必給每一個產品增加工廠類,可以對產品進行分組,每組中的不同產品由同一個工廠類的不同方法來創建。
作者:realyrare
出處:https://www.cnblogs.com/mhg215/
聲援博主:如果您覺得文章對您有幫助,請點擊文章末尾的【關註我】吧!
別忘記點擊文章右下角的【推薦】支持一波。~~~///(^v^)\\\~~~ .
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
如果您有其他問題,也歡迎關註我下方的公眾號,可以聯繫我一起交流切磋!