简单工厂模式
前言
抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,而无需制定他们具体的类。
优点一:易于交换产品系列,由于具体工厂类,例如 IFactory factory = new MySQLFactory(),在一个应用中只需要在初始化的时候出现一次,这就使得改变一个应用的具体工厂变得非常容易,它只需要改变具体工厂即可使用不同的产品配置。
优点二:具体的创建实例过程与客户端分离,客户端通过他们的抽象接口操纵实例,产品的具体类名也被具体工厂的实现分离,不会出现在客户端代码中。
一、基础类
1、User类
1 public class User
2 {
3 public int ID { get; set; }
4 public string Name { get; set; }
5 }
2、操作User类
1 public class SqlServerUser
2 {
3 public User GetUser(int id)
4 {
5 Console.WriteLine("在Sql server中根据ID得到User表一条记录");
6 return null;
7 }
8
9 public void insert(User user)
10 {
11 Console.WriteLine("在Sql server中给User表增加一条记录");
12 }
13 }
1 public class MysqlUser : IUser
2 {
3 public User GetUser(int id)
4 {
5 Console.WriteLine("在Mysql中根据ID得到User表一条记录");
6 return null;
7 }
8
9 public void insert(User user)
10 {
11 Console.WriteLine("在Mysql中给User表增加一条记录");
12 }
13 }
3、Department类
1 public class Department
2 {
3 public int ID { get; set; }
4 public string Name { get; set; }
5 }
4、操作department类
1 public class SqlServerDepartment : IDepartment
2 {
3 public Department GetDepartment(int id)
4 {
5 Console.WriteLine("在Sql server中根据ID得到User表一条记录");
6 return null;
7 }
8
9 public void Insert(Department department)
10 {
11 Console.WriteLine("在Sql server中给User表增加一条记录");
12 }
13 }
1 public class MysqlDepartment : IDepartment
2 {
3 public Department GetDepartment(int id)
4 {
5 Console.WriteLine("在Mysql中根据ID得到Department表一条记录");
6 return null;
7 }
8
9 public void Insert(Department user)
10 {
11 Console.WriteLine("在Mysql中给Department表增加一条记录");
12 }
13 }
二、抽象工厂
IUser接口,用于客户端访问,解除与具体数据库访问的耦合
1 public interface IUser
2 {
3 void insert(User user);
4 User GetUser(int id);
5 }
6 public class SqlServerUser : IUser
7 {
8 public User GetUser(int id)
9 {
10 Console.WriteLine("在Sql server中根据ID得到User表一条记录");
11 return null;
12 }
13
14 public void insert(User user)
15 {
16 Console.WriteLine("在Sql server中给User表增加一条记录");
17 }
18 }
19 public class MysqlUser : IUser
20 {
21 public User GetUser(int id)
22 {
23 Console.WriteLine("在Mysql中根据ID得到User表一条记录");
24 return null;
25 }
26
27 public void insert(User user)
28 {
29 Console.WriteLine("在Mysql中给User表增加一条记录");
30 }
31 }
1 public interface IDepartment
2 {
3 void Insert(Department department);
4 Department GetDepartment(int id);
5 }
6 public class SqlServerDepartment : IDepartment
7 {
8 public Department GetDepartment(int id)
9 {
10 Console.WriteLine("在Sql server中根据ID得到User表一条记录");
11 return null;
12 }
13
14 public void Insert(Department department)
15 {
16 Console.WriteLine("在Sql server中给User表增加一条记录");
17 }
18 }
19 public class MysqlDepartment : IDepartment
20 {
21 public Department GetDepartment(int id)
22 {
23 Console.WriteLine("在Mysql中根据ID得到Department表一条记录");
24 return null;
25 }
26
27 public void Insert(Department user)
28 {
29 Console.WriteLine("在Mysql中给Department表增加一条记录");
30 }
31 }
IFactory接口,定义一个创建访问User表对象的抽象的工厂接口
1 public interface IFactory
2 {
3 IUser CreateUser();
4 IDepartment CreateDepartment();
5 }
实例化IFactory接口
1 public class SqlServerFactory : IFactory
2 {
3 public IDepartment CreateDepartment()
4 {
5 return new SqlServerDepartment();
6 }
7
8 public IUser CreateUser()
9 {
10 return new SqlServerUser();
11 }
12 }
13 public class MysqlFactory : IFactory
14 {
15 public IDepartment CreateDepartment()
16 {
17 return new MysqlDepartment();
18 }
19
20 public IUser CreateUser()
21 {
22 return new MysqlUser();
23 }
24 }
客户端:
1 User user = new User();
2 Department dep = new Department();
3 //只需要确定实例化哪一个数据库访问对象给factory
4 抽象工厂模式.IFactory factory = new 抽象工厂模式.MysqlFactory();
5
6 IUser iu = factory.CreateUser();
7 iu.insert(user);
8 iu.GetUser(1);
9
10 IDepartment idep = factory.CreateDepartment();
11 idep.Insert(dep);
12 idep.GetDepartment(1);
三、简单工厂来改进抽象工厂
1 public class Data4
2 {
3 private static readonly string db = "Sqlserver";
4 public static IUser CreateUser()
5 {
6 IUser result = null;
7 switch (db)
8 {
9 case "Sqlserver":
10 result = new SqlServerUser();
11 break;
12 case "Mysql":
13 result = new MysqlUser();
14 break;
15 default:
16 break;
17 }
18 return result;
19 }
20 public static IDepartment CreateDepartment()
21 {
22 IDepartment result = null;
23 switch (db)
24 {
25 case "Sqlserver":
26 result = new SqlServerDepartment();
27 break;
28 case "Mysql":
29 result = new MysqlDepartment();
30 break;
31 default:
32 break;
33 }
34 return result;
35 }
36 }
客户端
1 User user = new User();
2 Department dep = new Department();
3 IUser iu = 简单工厂.Data4.CreateUser();
4 iu.insert(user);
5 iu.GetUser(1);
6
7 IDepartment idep = 简单工厂.Data4.CreateDepartment();
8 idep.Insert(dep);
9 idep.GetDepartment(1);
四、反射+抽象工厂
Assembly.Load("程序集名称").CreateInstance("命名空间.类全名")
1 public class Data3
2 {
3 //程序集名称
4 private static readonly string AssemblyName = "抽象工厂模式";
5 //数据库名称,mysql,oracle等
6 //private static readonly string db = "Sqlserver";
7
8 //配置文件中获取
9 private static readonly string db = ConfigurationManager.AppSettings.Get("_db");
10 public static IUser CreateUser()
11 {
12 string classname = AssemblyName + "." + db + "User";
13 return (IUser)Assembly.Load(AssemblyName).CreateInstance(classname);
14 }
15 public static IDepartment CreateDepartment()
16 {
17 string classname = AssemblyName + "." + db + "Department";
18 return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(classname);
19 }
20 }
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <startup>
4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5 </startup>
6 <appSettings>
7 <add key="_db" value="Sqlserver"/>
8 </appSettings>
9 </configuration>