就不解释抽象工厂是什么了,直接上代码:
两个具体类: public Department() { Console.WriteLine("创建了一个Department"); } public User() { Console.WriteLine("创建了一个User"); }
负责Department的 interface IDepartment { void Insert(Department department); Department GetDepartment(int id); } class AccessDepartment : IDepartment { public AccessDepartment() { Console.WriteLine("调用了AccessDepartment的构造函数"); }
#region IDepartment 成员
public void Insert(Department department) { Console.WriteLine("在Access里给部门表增加记录"); }
public Department GetDepartment(int id) { Console.WriteLine("在Access里根据ID得到部门表一条记录"); return null; }
#endregion } class SqlserverDepartment:IDepartment { public SqlserverDepartment() { Console.WriteLine("调用了SqlserverDepartment的构造函数"); }
#region IDepartment 成员
public void Insert(Department department) { Console.WriteLine("在SQL里给部门表增加记录"); }
public Department GetDepartment(int id) { Console.WriteLine("在SQL里根据ID得到部门表一条记录"); return null; }
#endregion }
负责User的 interface IUser { void Insert(User user); User GetUser(int id); } class AccessUser : IUser { #region IUser 成员
public void Insert(User user) { Console.WriteLine("通过AccessUser类里的IUser成员函数Insert创建了一个新的User"); }
public User GetUser(int id) { Console.WriteLine("通过AccessUser类里的IUser成员函数GetUser取得ID为{0}的user编号", id); return null; }
#endregion } class SqlserverUser:IUser { #region IUser 成员
public void Insert(User user) { Console.WriteLine("通过SqlserverUser类里的IUser成员函数Insert创建了一个新的User"); }
public User GetUser(int id) { Console.WriteLine("通过SqlserverUser类里的IUser成员函数GetUser取得ID为{0}的user编号", id); return null; }
#endregion }
工厂: //所有工厂的抽象——抽象工厂 interface IFactory { //两个接口类型,由客户程序通过向上转换原则new出具体类的对象 IUser CreateUser(); IDepartment CreateDepartment(); } class AccessFactory : IFactory { public AccessFactory() { Console.WriteLine("调用了SqlServerFactory的构造函数"); }
#region IFactory 成员
public IUser CreateUser() { Console.WriteLine("通过AccessFactory类里的IFactory成员函数CreateUser创建了一个新的AccessUser"); return new AccessUser(); }
public IDepartment CreateDepartment() { Console.WriteLine("通过AccessFactory类里的IFactory成员函数IDepartment创建了一个新的AccessDepartment"); return new AccessDepartment(); }
#endregion } class SqlServerFactory:IFactory { public SqlServerFactory() { Console.WriteLine("调用了SqlServerFactory的构造函数"); }
#region IFactory 成员
public IUser CreateUser() { Console.WriteLine("通过SqlServerFactory类里的IFactory成员函数CreateUser创建了一个新的SqlserverUser"); return new SqlserverUser(); }
public IDepartment CreateDepartment() { Console.WriteLine("通过SqlServerFactory类里的IFactory成员函数CreateDepartment创建了一个新的SqlserverDepartment"); return new SqlserverDepartment(); }
#endregion }
客户端: static void Main(string[] args) { //创建两个数据表对象 Console.WriteLine("\n调用User user = new User();"); User user = new User(); Console.WriteLine("\n调用Department dept = new Department();"); Department dept = new Department();
//变化点:创建一个抽象工厂,new具体的工厂 Console.WriteLine("\n调用IFactory ifactory = new SqlServerFactory();"); IFactory ifactory = new SqlServerFactory(); //Console.WriteLine("\n调用IFactory ifactory = new AccessFactory();"); //IFactory ifactory = new AccessFactory();
//通过接口创建user执行具体操作 Console.WriteLine("\n调用IUser iuser = ifactory.CreateUser();"); IUser iuser = ifactory.CreateUser(); Console.WriteLine("\n调用iuser.Insert(user);"); iuser.Insert(user); Console.WriteLine("\n调用iuser.GetUser(1);"); iuser.GetUser(1);
//通过接口创建department执行具体操作 Console.WriteLine("\n调用IDepartment idepartment = ifactory.CreateDepartment();"); IDepartment idepartment = ifactory.CreateDepartment(); Console.WriteLine("\n调用idepartment.Insert(dept);"); idepartment.Insert(dept); Console.WriteLine("\n调用idepartment.GetDepartment(1);"); idepartment.GetDepartment(1);
Console.ReadKey(); }