标签:style blog http ar color os 使用 sp on
抽象工厂模式(Abstract Factory)的定义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类。
可见,抽象工厂模式是指当有多个抽象角色时,使用的一种工厂方法模式。工厂方法模式是针对一个产品等级结构,而抽象工厂是针对多个产品等级结构。
class User { private int _id; public int ID { get { return _id; } set { _id = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } } class Department { private int _id; public int ID { get { return _id; } set { _id = value; } } private string _deptName; public string DeptName { get { return _deptName; } set { _deptName = value; } } } /// <summary> /// 抽象产品A /// </summary> interface IUser { void Insert(User user); User GetUser(int id); } /// <summary> /// 抽象产品B /// </summary> interface IDepartment { void Insert(Department department); Department GetDepartment(int id); }
/// <summary> /// 具体产品A1 抽象产品A的具体分类的实现 /// </summary> class SqlserverUser : IUser { public void Insert(User user) { Console.WriteLine("在Sqlserver中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Sqlserver中根据ID得到User表一条记录"); return null; } } /// <summary> /// 具体产品A2 抽象产品A的具体分类的实现 /// </summary> class AccessUser : IUser { public void Insert(User user) { Console.WriteLine("在Access中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Access中根据ID得到User表一条记录"); return null; } } /// <summary> /// 具体产品B1 抽象产品B的具体分类的实现 /// </summary> class SqlserverDepartment : IDepartment { public void Insert(Department department) { Console.WriteLine("在Sqlserver中给Department表增加一条记录"); } public Department GetDepartment(int id) { Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录"); return null; } } /// <summary> /// 具体产品B2 抽象产品B的具体分类的实现 /// </summary> class AccessDepartment : IDepartment { public void Insert(Department department) { Console.WriteLine("在Access中给Department表增加一条记录"); } public Department GetDepartment(int id) { Console.WriteLine("在Access中根据ID得到Department表一条记录"); return null; } }
/// <summary> /// 抽象工厂 /// </summary> interface IFactory { IUser CreateUser(); IDepartment CreateDepartment(); }
class SqlServerFactory : IFactory { public IUser CreateUser() { return new SqlserverUser(); } public IDepartment CreateDepartment() { return new SqlserverDepartment(); } } class AccessFactory : IFactory { public IUser CreateUser() { return new AccessUser(); } public IDepartment CreateDepartment() { return new AccessDepartment(); } }
static void Main(string[] args) { User user = new User(); Department dept = new Department(); //AbstractFactory factory = new SqlServerFactory(); IFactory factory = new AccessFactory(); IUser iu = factory.CreateUser(); iu.Insert(user); iu.GetUser(1); IDepartment id = factory.CreateDepartment(); id.Insert(dept); id.GetDepartment(1); }
标签:style blog http ar color os 使用 sp on
原文地址:http://www.cnblogs.com/YuanSong/p/4162263.html