工厂模式

前面说了写的一个数据库管理软件
写配置文件,包括增删改查,不同数据库都是不一样的,如果写到一个函数里,函数太长想改一个东西得找半天,
使用工厂来解耦.参考了大话设计模式.
private void xmlsave()
{
string server = this.textBoxServer.Text;
string user = this.textBoxUid.Text;
string password = this.textBoxPass.Text;
string type = this.textBox_type.Text;

Hashtable hashtable = new Hashtable();
hashtable["server"] = server;
hashtable["user"] = user;
hashtable["password"] = password;
hashtable["type"] = type;

dbo dboone;
dboone = dbofactory.getdbo(type);
dboone.xmlsave(hashtable);

xmlload();
}
public class dbofactory
{
public static dbo getdbo(string type)
{
//throw new System.NotImplementedException();
if (type == "mysql")
{
return new dbomysql();
}
else if (type == "sqlserver")
{
return new dbosqlserver();
}
else
{
return new dbosqlserver();
}
}
}
这样代码就容易改了,学名叫可扩展性.

posted on 2019-10-01 23:34  xinshenghu  阅读(126)  评论(0编辑  收藏  举报