抽象工厂模式处理的是多表业务。工厂方法模式处理的单表的多项业务。
本例以选择操作数据库(SQL、 ACCESS)为例,解除与数据库的耦合。方便更改数据库的选取
本例结构图如下:
工厂接口类(IFactory)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { interface IFactory { IUser CreateUser(); //IDepartment CreateDept(); } }
SQL操作User表的工厂(SqlServerFactory):继承于工厂接口类(IFactory)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { class SqlServerFactory:IFactory { public IUser CreateUser() { return new SqlServerUser(); } } }
ACCESS操作User表的工厂(AccessFactory):继承于工厂接口类(IFactory)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { class AccessFactory:IFactory { public IUser CreateUser() { return new AccessUser(); } } }
用户表(User)接口类(IUser)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { interface IUser { void Insert(User user); void GetUser(string id); } }
SQL处理User表的业务(SqlServerUser):继承于用户表接口(IUser)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { class SqlServerUser:IUser { public void Insert(User user) { Console.WriteLine("在User表中插入一条数据{0}!",user); } public void GetUser(string id) { Console.WriteLine("通过{0}来查找数据!",id); } } }
ACCESS处理User表的业务(AccserverUser):继承于用户表接口(IUser)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { class AccessUser:IUser { public void Insert(User user) { Console.WriteLine("在{0}表中插入数据!Access数据库",user); } public void GetUser(string id) { Console.WriteLine("通过{0}查找数据库表!Access数据库",id); } } }
封装User表属性:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { class User { private string id; private User user; /// <summary> /// 用户表对象 /// </summary> public User User1 { get { return user; } set { user = value; } } /// <summary> /// ID号 /// </summary> public string Id { get { return id; } set { id = value; } } } }
测试类(TestMain)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB { class TestMain { static void Main(string[] args) { //User user = new User(); //IFactory sqlFactory = new SqlServerFactory(); //IUser iUser = sqlFactory.CreateUser(); //iUser.Insert(user); //user.Id = "name"; //iUser.GetUser(user.Id); User user = new User(); IFactory accFactory = new AccessFactory(); IUser iUser = accFactory.CreateUser(); iUser.Insert(user); iUser.GetUser("ID"); Console.ReadLine(); } } }
上述为工厂方法模式的实现:抽象方法模式即在此基础上再加入其他表的业务逻辑操作
在上述的 工厂接口类(IFactory)里添加部门表的(Department)方法即
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { interface IFactory { IUser CreateUser(); IDepartment CreateDept(); } }
更改SQL操作Department表的工厂、ACCESS操作Dapartment表的工厂
SQL操作工厂(SqlServerFactory):继承于工厂接口类(IFactory)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { class SqlServerFactory:IFactory { public IUser CreateUser() { return new SqlServerUser(); } public IDepartment CreateDepart() { return new SqlServerDepartment(); } } }
ACCESS操作工厂(AccessFactory):继承于工厂接口类(IFactory)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace FactoryMethodPatternDB.CLASS { class AccessFactory:IFactory { public IUser CreateUser() { return new AccessUser(); } public IDepartment CreateDepart() { return new AccessDepart(); } } }
添加部门表(Department)的接口,
添加SQL处理Department表业务的方法、ACCESS处理Department表业务的方法
测试类中: IFactory sqlFactory = new Sql