工厂抽象以及实体工厂代码如下:
抽象工厂模式:
public abstract class AbstractDALFactory { public abstract IAdminService CreateAdminService(); public static AbstractDALFactory ChooseFactory() {
string dbType = ConfigurationManager.AppSettings["DBType"].ToString();
AbstractDALFactory factory = null; switch(dbType) { case "Sql": factory = new SqlDALFactory(); break; case "Access": factory = new AccessDALFactory(); break; case "Oracle": factory = new OracleDALFactory(); break; } return factory; } }
Access实体工厂:
public class AccessDALFactory : AbstractDALFactory { public override IAdminService CreateAdminService() { return new AdminService(); } }
Oracle实体工厂:
public class OracleDALFactory:AbstractDALFactory { public override IAdminService CreateAdminService() { return new AdminService(); } }
Sql实体工厂:
public class SqlDALFactory : AbstractDALFactory { public override IAdminService CreateAdminService() { return new AdminService(); } }
抽象产品和实体产品的代码如下:
抽象产品:
public interface IAdminService { //检测登录用户是否存在 bool CheckAdmin(int id,string pwd); }
Access实体产品:
public class AdminService:IAdminService { #region IAdminService 成员 public AdminService() { connstr ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Environment.CurrentDirectory +@"\mydb.mdb"; } private string connstr; private OleDbConnection conn = new OleDbConnection(); private void open() { if (conn.State == ConnectionState.Closed) { conn.ConnectionString = connstr; conn.Open(); return; } if (conn.State == ConnectionState.Open) { return; } } public bool CheckAdmin(int id, string pwd) { open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; //特别注意,sql 语句后面不要添加“;”,会出现异常,0091,无效符号。害的调试半天 cmd.CommandText = "select * from guanli t where t.id ='" + id + "' and t.password = '" + pwd + "'"; object result = null; try { result = cmd.ExecuteScalar(); } catch (OleDbException ex) { Console.WriteLine("wws:" + ex.Message); return false; }
if (conn.State == ConnectionState.Open) conn.Close(); return result != null ? true : false; }
#endregion }
Oracle实体产品:
public class AdminService : IAdminService { #region IAdminService 成员 private string connstr = "Data Source=mydb;Persist Security Info=True;User ID=heatdb;Password=heatdb"; private OracleConnection conn = new OracleConnection(); private void open() { if (conn.State == ConnectionState.Closed) { conn.ConnectionString = connstr; conn.Open(); return; } if (conn.State == ConnectionState.Open) { return; } } public bool CheckAdmin(int id, string pwd) { open(); OracleCommand cmd = new OracleCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; //特别注意,sql 语句后面不要添加“;”,会出现异常,0091,无效符号。害的调试半天 cmd.CommandText = "select * from guanli t where t.id ='" + id + "' and t.password = '" + pwd + "'"; object result = null; try { result = cmd.ExecuteScalar(); } catch (OracleException ex ) { Console.WriteLine("wws:"+ex.Message); return false; }
if (conn.State == ConnectionState.Open) conn.Close(); return result != null ? true : false; }
#endregion }
sql实体产品:
public class AdminService : IAdminService { #region IAdminService 成员 private string connstr = @"Data Source=SUNLIKE\SQLEXPRESS;Initial Catalog=blank;Persist Security Info=True;User ID=sa;Password=sa"; private SqlConnection conn = new SqlConnection(); private void open() { if (conn.State == ConnectionState.Closed) { conn.ConnectionString = connstr; conn.Open(); return; } if (conn.State == ConnectionState.Open) { return; } } public bool CheckAdmin(int id, string pwd) { open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from guanli where username = '"+id+"' and password = '"+pwd +"';";
object result = cmd.ExecuteScalar();
if (conn.State == ConnectionState.Open) conn.Close(); return result != null ? true : false; }
#endregion }
总结:抽象工厂模式采用抽象工厂和实体工厂,以及抽象产品和实体产品进行模式的划分。
|
请发表评论