标准结构
Ado.NET的抽象工厂结构
抽象工厂基类DbProviderFactory,实现类SqlClientFactory、OracleClientFactory等。产品基类DbConnection、DbCommand、DbParameter等,实现类SqlConnection、OracleConnection等。伪码:
public abstract class DbProviderFactory
{
public virtual DbConnection CreateConnection()
{
return null;
}
public abstract class DbConnection()
{
}
public abstract class DbCommand()
{
}
public class SqlConnection : DbConnection
{
}
public class SqlCommand : DbCommand
{
}
public class SqlClientFactory : DbProviderFactory
{
public override DbConnection CreateConnection()
{
return new SqlConnection();
}
public virtual DbConnection CreateDbCommand()
{
return new SqlCommand ();
}
}
public class OracleClientFactory : DbProviderFactory
{
public override SqlConnection CreateConnection()
{
return new SqlConnection();
}
public virtual DbCommand CreateDbCommand()
{
return new SqlCommand();
}
}
public class Client
{
DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
DbConnection = factory.CreateDbConnection();
DbCommand = factory.CreateDbCommand();
}