工厂接口:提供创建对象的接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AbstractFactory.InterfaceProvide { public interface IFactory { IUserInfo CreateUser(); IDepartment CreateDepartment(); } } |
实例接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AbstractFactory.InterfaceProvide { public interface IDepartment { void GetEntity( int id); void Insert(Model.Department model); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AbstractFactory.InterfaceProvide { public interface IUserInfo { void GetEntity( int id); void Insert(Model.UserInfo model); } } |
抽象工厂类:通过反射创建对应的不同的工厂。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using AbstractFactory.InterfaceProvide; using System.Xml; namespace AbstractFactory.Fac { public class DataAccess : IFactory { //public string db = "SqlServer"; public static string db = GetDB(); public static string GetDB() { XmlDocument doc = new XmlDocument(); string dir = AppDomain.CurrentDomain.BaseDirectory.Replace( "\\bin\\Debug" , "" ); doc.Load(dir + "App.xml" ); XmlElement xe = doc.DocumentElement; string val = xe.ChildNodes[0].InnerText; return val; } public static IFactory CreateFac() { return (IFactory)Assembly.Load( "AbstractFactory" ).CreateInstance( "AbstractFactory.Fac." + db + "Factory" ); } //public IUserInfo CreateUser() //{ // return (IUserInfo)Assembly.Load("AbstractFactory").CreateInstance("AbstractFactory.DAL." + db + "." + db + "UserInfo"); //} //public IDepartment CreateDepartment() //{ // return (IDepartment)Assembly.Load("AbstractFactory").CreateInstance("AbstractFactory.DAL." + db + "." + db + "Department"); //} } } |
Access工厂:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using AbstractFactory.InterfaceProvide; using AbstractFactory.DAL; using AbstractFactory.DAL.Access; namespace AbstractFactory.Fac { public class AccessFactory : IFactory { public IUserInfo CreateUser() { return new AccessUserInfo(); } public IDepartment CreateDepartment() { return new AccessDepartment(); } } } |
SqlServer工厂:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using AbstractFactory.InterfaceProvide; namespace AbstractFactory.Fac { public class SqlServerFactory : IFactory { public IUserInfo CreateUser() { return new DAL.SqlServer.SqlServerUserInfo(); } public IDepartment CreateDepartment() { return new DAL.SqlServer.SqlServerDepartment(); } } } |
通过加载配置文件,选择初始化哪个工厂。
1 2 3 4 | <?xml version= "1.0" encoding= "utf-8" ?> <DataBase> <Type>SqlServer</Type> </DataBase> |
要实例的类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using AbstractFactory.InterfaceProvide; namespace AbstractFactory.DAL.Access { public class AccessDepartment : IDepartment { public void GetEntity( int id) { Console.WriteLine( "在Access中的Department表获得一个实体!!!" ); } public void Insert(Model.Department model) { Console.WriteLine( "在Access中的Department表添加一个实体!!!" ); } } public class AccessUserInfo : IUserInfo { public void GetEntity( int id) { Console.WriteLine( "在Access中的UserInfo表获得一个实体!!!" ); } public void Insert(Model.UserInfo model) { Console.WriteLine( "在Access中的UserInfo表添加一个实体!!!" ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using AbstractFactory.InterfaceProvide; using AbstractFactory.Model; namespace AbstractFactory.DAL.SqlServer { public class SqlServerDepartment : IDepartment { public void Insert(Department user) { Console.WriteLine( "在Sqlserver中的Department表添加一个实体!!!" ); } public void GetEntity( int id) { Console.WriteLine( "在Sqlserver中的Department表获得一个实体!!!" ); } } public class SqlServerUserInfo : IUserInfo { public void Insert(UserInfo user) { Console.WriteLine( "在Sqlserver中的UserInfo表添加一个实体!!!" ); } public void GetEntity( int id) { Console.WriteLine( "在Sqlserver中的UserInfo表获得一个实体!!!" ); } } } |
Model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AbstractFactory.Model { public class UserInfo { private string _UserName; public string UserName { get { return _UserName; } set { _UserName = value; } } private int _Age; public int Age { get { return _Age; } set { _Age = value; } } } public class Department { public string Name { get ; set ; } } } |
使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using AbstractFactory.DAL.Access; using AbstractFactory.DAL.SqlServer; using AbstractFactory.Fac; using AbstractFactory.InterfaceProvide; namespace AbstractFactory { class Program { static void Main( string [] args) { //DataAccess da = new DataAccess(); //IUserInfo sqlUser = da.CreateUser(); //sqlUser.Insert(null); //sqlUser.GetEntity(1); IFactory fac = DataAccess.CreateFac(); IUserInfo userInfo = fac.CreateUser(); userInfo.GetEntity(1); Console.ReadLine(); //AccessUserInfo AccessFac = new AccessFactory().CreateUser() as AccessUserInfo; //AccessFac.Insert(null); //AccessFac.GetEntity(1); //Console.ReadLine(); } } } |
参考:https://www.cnblogs.com/Terrylee/archive/2006/01/04/310716.html
分类:
DesignMode(设计模式)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】