不一样的抽象工厂模式(设计模式七)
前言
工厂模式解决了对象创建导致的紧耦合的关系,从而支持对象创建的稳定。那么抽象工厂是解决什么问题呢?
我们知道要松耦合高内聚,那么什么时候需要高内聚呢?高内聚解决什么问题呢?
在软件系统中,经常面临着"一系列相互依赖对象"的创建工作。同时由于需求的变化,往往存在更多系列的创建工作呢?
正文
在我们数据库访问中,我们常常用到SqlConnection、SqlCommand、SqlDataReader,这是mmsql,但是有一个问题,这个是我们在使用不同数据库的时候呢,我们要切换不同对象,假设MySqlConnection、MySqlCommand、MySqlDataReader。
这样我们又得去修改代码,这是因为new SqlConnection 是具体实现,所以我们使用工厂模式来解决这个问题。
DBCommandFactory dBCommand;
DBConnectionFactory dBConnection;
DBDataReaderFactory dBDataReader;
public Application(DBConnectionFactory dBConnection, DBCommandFactory dBCommand, DBDataReaderFactory dBDataReader)
{
this.dBCommand = dBCommand;
this.dBConnection = dBConnection;
this.dBDataReader = dBDataReader;
}
public void doSomeThing()
{
//做一些操作
}
放出其他代码:
public interface IDBConnection
{
}
public abstract class DBConnectionFactory
{
public abstract IDBConnection CreateDBConnection();
}
public interface IDBCommand
{
}
public abstract class DBCommandFactory
{
public abstract IDBCommand CreateDBCommand();
}
public interface IDBDataReader
{
}
public abstract class DBDataReaderFactory
{
public abstract IDBDataReader CreateDBDataReader();
}
public class SqlConnection : IDBConnection
{
}
public class SqlConnectionFactory : DBConnectionFactory
{
public override IDBConnection CreateDBConnection()
{
return new SqlConnection();
}
}
public class SqlCommand : IDBCommand
{
}
public class SqlCommandFactory : DBCommandFactory
{
public override IDBCommand CreateDBCommand()
{
return new SqlCommand();
}
}
public class SqlDataReader : IDBDataReader
{
}
public class SqlDataReaderFactory : DBDataReaderFactory
{
public override IDBDataReader CreateDBDataReader()
{
return new SqlDataReader();
}
}
public class MySqlConnection : IDBConnection
{
}
public class MySqlCommand : IDBCommand
{
}
public class MysqlDataReader : IDBCommand
{
}
//mysql 的工厂我没有写
那么这个时候有啥问题呢?
DBCommandFactory dBCommand;
DBConnectionFactory dBConnection;
DBDataReaderFactory dBDataReader;
public Application(DBConnectionFactory dBConnection, DBCommandFactory dBCommand, DBDataReaderFactory dBDataReader)
{
this.dBCommand = dBCommand;
this.dBConnection = dBConnection;
this.dBDataReader = dBDataReader;
}
看上面代码,有一个问题就DBConnectionFactory 如果是mmsql,那么DBCommandFactory 、DBDataReaderFactory 同样要是这样。
那么就可以做一个这样的变化,这时候就是要高内聚。
public class Application
{
DBFactory dBFactory;
public Application(DBFactory dBFactory)
{
this.dBFactory = dBFactory;
}
public void doSomeThing()
{
dBFactory.CreateDBCommand();
dBFactory.CreateDBConnection();
dBFactory.CreateDBDataReader();
}
}
这里只放出mmsql 的代码:
public interface IDBConnection
{
}
public interface IDBCommand
{
}
public interface IDBDataReader
{
}
public abstract class DBFactory
{
public abstract IDBConnection CreateDBConnection();
public abstract IDBCommand CreateDBCommand();
public abstract IDBDataReader CreateDBDataReader();
}
public abstract class DBDataReaderFactory
{
}
public class SqlConnection : IDBConnection
{
}
public class SqlCommand : IDBCommand
{
}
public class SqlDataReader : IDBDataReader
{
}
public class SqlConnectionFactory : DBFactory
{
public override IDBCommand CreateDBCommand()
{
return new SqlCommand();
}
public override IDBConnection CreateDBConnection()
{
return new SqlConnection();
}
public override IDBDataReader CreateDBDataReader()
{
throw new NotImplementedException();
}
}
概念图
后续补上