.Net Framework中使用的模式-Abstract Factory模式

标准结构

abstract

Ado.NET的抽象工厂结构

ADO.NETAbstractFactory

抽象工厂基类DbProviderFactory,实现类SqlClientFactory、OracleClientFactory等。产品基类DbConnection、DbCommand、DbParameter等,实现类SqlConnection、OracleConnection等。伪码:

public abstract class DbProviderFactory
{
    public virtual DbConnection CreateConnection()
    {
        return null;
    }

    public virtual DbCommand CreateDbCommand()
    {
        return null;
    }
}

public abstract class DbConnection()
{
}

public abstract class DbCommand()
{
}

public class SqlConnection : DbConnection
{
}

public class OracleConnection : DbConnection
{
}

public class SqlCommand : DbCommand
{
}

public class OracleCommand : 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();
}

你可能感兴趣的:(framework)