控制台代码:
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Reflection; //反射命名空间
5 using System.Configuration;
6
7 namespace AbstractorFactory
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 User user = new User();
14 IUser iu = DataAccess.CreateUser();
15 iu.Insert(user);
16 iu.GetUser(1);
17 //IFactory factory = new AccessFactory();
18 //IUser iu = factory.CreateUser();
19 //iu.Insert(user);
20 //iu.GetUser(1);
21 Console.ReadLine();
22 }
23 }
24 interface IUser
25 {
26 void Insert(User user);
27 User GetUser(int id);
28 }
29 class User
30 {
31 private int _id;
32
33 public int Id
34 {
35 get { return _id; }
36 set { _id = value; }
37 }
38 private string _name;
39
40 public string Name
41 {
42 get { return _name; }
43 set { _name = value; }
44 }
45 }
46 class SqlserverUser:IUser
47 {
48 public void Insert(User user)
49 {
50 Console.WriteLine("在Sql server中给User表增加一条记录");
51 }
52 public User GetUser(int id)
53 {
54 Console.WriteLine("在Sql server中根据id得到User表中的一条记录");
55 return null;
56 }
57 }
58 class AccessUser : IUser
59 {
60 public void Insert(User user)
61 {
62 Console.WriteLine("在Access中给User表增加一条记录");
63 }
64 public User GetUser(int id)
65 {
66 Console.WriteLine("在Access中根据id得到User表中的一条记录");
67 return null;
68 }
69 }
70 #region 抽象工厂模式
71 //interface IFactory
72 //{
73 // IUser CreateUser();
74 //}
75 //class SqlServerFactory : IFactory
76 //{
77 // public IUser CreateUser()
78 // {
79 // return new SqlserverUser();
80 // }
81 //}
82 //class AccessFactory : IFactory
83 //{
84 // public IUser CreateUser()
85 // {
86 // return new AccessUser();
87 // }
88 //}
89 #endregion
90 class DataAccess
91 {
92 private static readonly string db = ConfigurationSettings.AppSettings["db"];
93 private static readonly string assemblyName = "AbstractorFactory";
94
95 public static IUser CreateUser()
96 {
97 //抽象工厂模式
98 //IUse result=null;
99 //switch (db)
100 //{
101 // case "Sqlserver":
102 // result = new SqlserverUser();
103 // break;
104 // case "AccessUser":
105 // result = new AccessUser();
106 // break;
107 //}
108 string className = assemblyName + "." + db + "User";
109 IUser result =(IUser)Assembly.Load(assemblyName).CreateInstance(className); //利用反射技术实例化user.
110 return result;
111 }
112 }
113 }
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Reflection; //反射命名空间
5 using System.Configuration;
6
7 namespace AbstractorFactory
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 User user = new User();
14 IUser iu = DataAccess.CreateUser();
15 iu.Insert(user);
16 iu.GetUser(1);
17 //IFactory factory = new AccessFactory();
18 //IUser iu = factory.CreateUser();
19 //iu.Insert(user);
20 //iu.GetUser(1);
21 Console.ReadLine();
22 }
23 }
24 interface IUser
25 {
26 void Insert(User user);
27 User GetUser(int id);
28 }
29 class User
30 {
31 private int _id;
32
33 public int Id
34 {
35 get { return _id; }
36 set { _id = value; }
37 }
38 private string _name;
39
40 public string Name
41 {
42 get { return _name; }
43 set { _name = value; }
44 }
45 }
46 class SqlserverUser:IUser
47 {
48 public void Insert(User user)
49 {
50 Console.WriteLine("在Sql server中给User表增加一条记录");
51 }
52 public User GetUser(int id)
53 {
54 Console.WriteLine("在Sql server中根据id得到User表中的一条记录");
55 return null;
56 }
57 }
58 class AccessUser : IUser
59 {
60 public void Insert(User user)
61 {
62 Console.WriteLine("在Access中给User表增加一条记录");
63 }
64 public User GetUser(int id)
65 {
66 Console.WriteLine("在Access中根据id得到User表中的一条记录");
67 return null;
68 }
69 }
70 #region 抽象工厂模式
71 //interface IFactory
72 //{
73 // IUser CreateUser();
74 //}
75 //class SqlServerFactory : IFactory
76 //{
77 // public IUser CreateUser()
78 // {
79 // return new SqlserverUser();
80 // }
81 //}
82 //class AccessFactory : IFactory
83 //{
84 // public IUser CreateUser()
85 // {
86 // return new AccessUser();
87 // }
88 //}
89 #endregion
90 class DataAccess
91 {
92 private static readonly string db = ConfigurationSettings.AppSettings["db"];
93 private static readonly string assemblyName = "AbstractorFactory";
94
95 public static IUser CreateUser()
96 {
97 //抽象工厂模式
98 //IUse result=null;
99 //switch (db)
100 //{
101 // case "Sqlserver":
102 // result = new SqlserverUser();
103 // break;
104 // case "AccessUser":
105 // result = new AccessUser();
106 // break;
107 //}
108 string className = assemblyName + "." + db + "User";
109 IUser result =(IUser)Assembly.Load(assemblyName).CreateInstance(className); //利用反射技术实例化user.
110 return result;
111 }
112 }
113 }
app.config 文件
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <appSettings>
4 <add key="db" value="Sqlserver"/>
5 </appSettings>
6 </configuration>
2 <configuration>
3 <appSettings>
4 <add key="db" value="Sqlserver"/>
5 </appSettings>
6 </configuration>