定 义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
结构图:
示例结构图:
实体类:
class User { public int Id { get; set; } public string Name { get; set; } } class Department { public int Id { get; set; } public string Name { get; set; } }
数据访问接口:
interface IUser { void Insert(User user); User GetUser(int id); } interface IDepartment { void Insert(Department department); Department GetDepartment(int id); }
User数据库访问具体类:
class SqlServerUser : IUser { public void Insert(User user) { Console.WriteLine("向SqlServer数据库中User表添加新用户"); } public User GetUser(int id) { Console.WriteLine("根据用户Id从SqlServer数据库中获取User"); return null; } } class AccessUser : IUser { public void Insert(User user) { Console.WriteLine("向Access数据库中User表添加新用户"); } public User GetUser(int id) { Console.WriteLine("根据Id从Access数据库中获取User"); return null; } }
Department数据库访问具体类:
class SqlServerDepartment : IDepartment { public void Insert(Department department) { Console.WriteLine("向SqlServer数据库Department表中添加一条新记录"); } public Department GetDepartment(int id) { Console.WriteLine("根据Id从SqlServer数据库Department表中部门信息"); return null; } } class AccessDepartment : IDepartment { public void Insert(Department department) { Console.WriteLine("向Access数据库Department表中添加一条新记录"); } public Department GetDepartment(int id) { Console.WriteLine("根据Id从Access数据库Department表中获得部门信息"); return null; } }
抽象工厂接口:
interface IFactory { IUser CreateUser(); IDepartment CreateDepartment(); }
SqlServerFactory类:
class SqlServerFactory : IFactory { public IUser CreateUser() { return new SqlServerUser(); } public IDepartment CreateDepartment() { return new SqlServerDepartment(); } }
AccessFactory类:
class AccessFactory : IFactory { public IUser CreateUser() { return new AccessUser(); } public IDepartment CreateDepartment() { return new AccessDepartment(); } }
客户端代码:
User user = new User(); Department department=new Department (); IFactory factory = new SqlServerFactory(); IUser iu = factory.CreateUser(); IDepartment idepart = factory.CreateDepartment(); iu.Insert(user); iu.GetUser(1); idepart.Insert(department); idepart.GetDepartment(1);
工厂方法模式和抽象工厂模式区别:
反射+配置文件方式修改以上示例:
结构图:
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DBType" value="SqlServer"/>
</appSettings>
</configuration>
DataAccess类:
class DataAccess { //程序集名称 private static readonly string AssemblyName = "DesignPatternControl"; //数据库类型 private static readonly string db = ConfigurationManager.AppSettings["DBType"]; public static IUser CreateUser() { string className = AssemblyName + "." + db + "User"; //具体数据库访问类名称 return (IUser)Assembly.Load(AssemblyName).CreateInstance(className); } public static IDepartment CreateDepartment() { string className = AssemblyName + "." + db + "Department"; //具体数据库访问类名称 return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className); } }
客户端调用:
//反射+抽象方法 User user = new User(); Department department = new Department(); IUser iu = DataAccess.CreateUser(); iu.Insert(user); iu.GetUser(1); IDepartment id = DataAccess.CreateDepartment(); id.Insert(department); id.GetDepartment(1);