• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#抽象工厂模式

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

工厂抽象以及实体工厂代码如下:

抽象工厂模式:

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
}

总结:抽象工厂模式采用抽象工厂和实体工厂,以及抽象产品和实体产品进行模式的划分。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap