无废话C#设计模式之三:Abstract Factory
2009-04-02 08:21:34 来源:WEB开发网 public abstract class DbProviderFactory
{
// Methods
protected DbProviderFactory()
{
}
public virtual DbCommand CreateCommand()
{
return null;
}
public virtual DbCommandBuilder CreateCommandBuilder()
{
return null;
}
public virtual DbConnection CreateConnection()
{
return null;
}
public virtual DbConnectionStringBuilder CreateConnectionStringBuilder()
{
return null;
}
public virtual DbDataAdapter CreateDataAdapter()
{
return null;
}
public virtual DbDataSourceEnumerator CreateDataSourceEnumerator()
{
return null;
}
public virtual DbParameter CreateParameter()
{
return null;
}
public virtual CodeAccessPermission CreatePermission(PermissionState state)
{
return null;
}
// Properties
public virtual bool CanCreateDataSourceEnumerator
{
get
{
return false;
}
}
}
public sealed class OleDbFactory : DbProviderFactory
{
// Fields
public static readonly OleDbFactory Instance = new OleDbFactory();
// Methods
private OleDbFactory()
{
}
public override DbCommand CreateCommand()
{
return new OleDbCommand();
}
public override DbCommandBuilder CreateCommandBuilder()
{
return new OleDbCommandBuilder();
}
public override DbConnection CreateConnection()
{
return new OleDbConnection();
}
public override DbConnectionStringBuilder CreateConnectionStringBuilder()
{
return new OleDbConnectionStringBuilder();
}
public override DbDataAdapter CreateDataAdapter()
{
return new OleDbDataAdapter();
}
public override DbParameter CreateParameter()
{
return new OleDbParameter();
}
public override CodeAccessPermission CreatePermission(PermissionState state)
{
return new OleDbPermission(state);
}
}
public sealed class SqlClientFactory : DbProviderFactory, IServiceProvider
{
// Fields
public static readonly SqlClientFactory Instance = new SqlClientFactory();
// Methods
private SqlClientFactory()
{
}
public override DbCommand CreateCommand()
{
return new SqlCommand();
}
public override DbCommandBuilder CreateCommandBuilder()
{
return new SqlCommandBuilder();
}
public override DbConnection CreateConnection()
{
return new SqlConnection();
}
public override DbConnectionStringBuilder CreateConnectionStringBuilder()
{
return new SqlConnectionStringBuilder();
}
public override DbDataAdapter CreateDataAdapter()
{
return new SqlDataAdapter();
}
public override DbDataSourceEnumerator CreateDataSourceEnumerator()
{
return SqlDataSourceEnumerator.Instance;
}
public override DbParameter CreateParameter()
{
return new SqlParameter();
}
public override CodeAccessPermission CreatePermission(PermissionState state)
{
return new SqlClientPermission(state);
}
object IServiceProvider.GetService(Type serviceType)
{
object obj2 = null;
if (serviceType == GreenMethods.SystemDataCommonDbProviderServices_Type)
{
obj2 = GreenMethods.SystemDataSqlClientSqlProviderServices_Instance();
}
return obj2;
}
// Properties
public override bool CanCreateDataSourceEnumerator
{
get
{
return true;
}
}
}
更多精彩
赞助商链接