首页 > 其他 > 详细

Abstract Factory(抽象工厂模式)

时间:2014-05-29 03:49:55      阅读:469      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
System.Data.Common.DbProviderFactory
public abstract class DbProviderFactory
{
   public virtual DbConnection CreateConnection()
  {
     return null;
  }
   public virtual DbCommand CreateCommand()
  {
     return null;
  }
}
 
System.Data.SqlClient.SqlClientFactory
public sealed class SqlClientFactory : DbProviderFactory, IServiceProvider
{
   public override DbConnection CreateConnection()
  {
     return new SqlConnection();
  }
  public override DbCommand CreateCommand()
  {
    return new SqlCommand();
  }
}
 
System.Data.Odbc.OdbcFactory
public sealed class OdbcFactory : DbProviderFactory
{
  public override DbConnection CreateConnection()
  {
    return new OdbcConnection();
  }
  public override DbCommand CreateCommand()
  {
    return new OdbcCommand();
  }
}
 
System.Data.Common.DbConnection
public abstract class DbConnection : Component, IDbConnection
{
}
 
System.Data.SqlClient.SqlConnection
public sealed partial class SqlConnection : DbConnection, ICloneable
{
}
 
System.Data.Odbc.OdbcConnection
public sealed partial class OdbcConnection : DbConnection, ICloneable
{
}
 
System.Data.Common.DbCommand
public abstract class DbCommand : Component, IDbCommand
{
}
 
System.Data.SqlClient.SqlCommand
public sealed class SqlCommand : DbCommand, ICloneable
{
}
 
System.Data.Odbc.OdbcCommand
public sealed class OdbcCommand : DbCommand, ICloneable
{
}
bubuko.com,布布扣

 

bubuko.com,布布扣
public class Client
{
public static void Main()
{
    string connectionString =
System.Configuration.ConfigurationManager.AppSettings["DB_CONNECTION_STRING"];
    string providerName =
System.Configuration.ConfigurationManager.AppSettings["DB_PROVIDER_NAME"];
    DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
    DbConnection connection = factory.CreateConnection();
    connection.ConnectionString = connectionString;
    using (connection)
    {
      DbCommand command = factory.CreateCommand();
      command.CommandText =
"INSERT INTO t_users(user_username, user_password) VALUES(‘admin‘ ,‘admin))";
      command.Connection = connection;
      command.ExecuteNonQuery();
    }
  }
}
bubuko.com,布布扣

Abstract Factory(抽象工厂模式),布布扣,bubuko.com

Abstract Factory(抽象工厂模式)

原文:http://www.cnblogs.com/www-zhoushuai-com/p/3754000.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!