设计模式学习笔记--抽象工厂模式

Posted bzyzhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式学习笔记--抽象工厂模式相关的知识,希望对你有一定的参考价值。

 1 using System;
 2 
 3 namespace AbstractFactory
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/28 10:23:20 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// User说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class User
12     {
13         private int id;
14 
15         public int Id
16         {
17             get { return id; }
18             set { id = value; }
19         }
20 
21         private string name;
22 
23         public string Name
24         {
25             get { return name; }
26             set { name = value; }
27         }
28     }
29 }
View Code
 1 using System;
 2 
 3 namespace AbstractFactory
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/28 10:38:21 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Department说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class Department
12     {
13         private int id;
14 
15         public int Id
16         {
17             get { return id; }
18             set { id = value; }
19         }
20 
21         private string departmentName;
22 
23         public string DepartmentName
24         {
25             get { return departmentName; }
26             set { departmentName = value; }
27         }
28     }
29 }
View Code
1 namespace AbstractFactory
2 {
3     public interface IUser
4     {
5         void Insert(User user);
6         User GetUser(int id);
7     }
8 }
View Code
1 namespace AbstractFactory
2 {
3     public interface IDepartment
4     {
5         void Insert(Department department);
6         Department GetDepartment(int id);
7     }
8 }
View Code
 1 using System;
 2 
 3 namespace AbstractFactory
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/28 11:23:53 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// SqlServerUser说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class SqlServerUser:IUser
12     {
13         public void Insert(User user)
14         {
15             Console.WriteLine("在SQL Server中给User表增加了一条记录");
16         }
17 
18         public User GetUser(int id)
19         {
20             Console.WriteLine("在SQL Server中根据ID得到User表一条记录");
21             return null;
22         }
23     }
24 }
View Code
 1 using System;
 2 
 3 namespace AbstractFactory
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/28 10:40:10 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// SqlServerDepartment说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class SqlServerDepartment:IDepartment
12     {
13         public void Insert(Department department)
14         {
15             Console.WriteLine("在SQL Server中给Department表增加了一条记录");
16         }
17 
18         public Department GetDepartment(int id)
19         {
20             Console.WriteLine("在SQL Server中根据ID得到Department表一条记录");
21             return null;
22         }
23     }
24 }
View Code
 1 using System;
 2 
 3 namespace AbstractFactory
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/28 10:30:42 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// AccessUser说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class AccessUser:IUser
12     {
13         public void Insert(User user)
14         {
15             Console.WriteLine("在Access中给User表增加了一条记录");
16         }
17 
18         public User GetUser(int id)
19         {
20             Console.WriteLine("在Access中根据ID得到User表一条记录");
21             return null;
22         }
23     }
24 }
View Code
 1 using System;
 2 
 3 namespace AbstractFactory
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/28 10:41:39 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// AccessDepartment说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class AccessDepartment:IDepartment
12     {
13         public void Insert(Department department)
14         {
15             Console.WriteLine("在Access中给Department表增加了一条记录");
16         }
17 
18         public Department GetDepartment(int id)
19         {
20             Console.WriteLine("在Access中根据ID得到Department表一条记录");
21             return null;
22         }
23     }
24 }
View Code
 1 using System;
 2 using System.Reflection;
 3 using System.Configuration;
 4 
 5 namespace AbstractFactory
 6 {
 7     /// <summary> 
 8     /// 作者:bzyzhang
 9     /// 时间:2016/5/28 10:52:18 
10     /// 博客地址:http://www.cnblogs.com/bzyzhang/
11     /// DataAccess说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
12     /// </summary> 
13     public class DataAccess
14     {
15         private static readonly string AssemblyName = "AbstractFactory";
16         //private static readonly string db = "SqlServer";
17         private static readonly string db = ConfigurationManager.AppSettings["DB"];
18 
19         public static IUser CreateUser()
20         {
21             string className = AssemblyName + "." + db + "User";
22             return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);
23         }
24 
25         public static IDepartment CreateDepartment()
26         {
27             string className = AssemblyName + "." + db + "Department";
28             return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className);
29         }
30     }
31 }
View Code
 1 namespace AbstractFactory
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             User user = new User();
 8             Department dept = new Department();
 9 
10             IUser iu = DataAccess.CreateUser();
11             iu.Insert(user);
12             iu.GetUser(1);
13 
14             IDepartment id = DataAccess.CreateDepartment();
15             id.Insert(dept);
16             id.GetDepartment(1);
17         }
18     }
19 }
View Code
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3   <startup>
4     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5   </startup>
6   <appSettings>
7     <add key="DB" value="SqlServer"/>
8   </appSettings>
9 </configuration>
View Code

 

以上是关于设计模式学习笔记--抽象工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式:学习笔记——抽象工厂模式

设计模式学习笔记(十一:抽象工厂模式)

java/android 设计模式学习笔记---抽象工厂模式

工厂模式学习笔记

《Head First 设计模式》学习笔记——工厂模式 + 抽象工厂模式

设计模式学习笔记 之 抽象工厂模式