昨天看了一个有关的Web Cast,对其中的Factory模式很是感兴趣。[小陈讲课是不是太搞笑了!^o^ ]
恰好项目中有一个小东西需要做,简单分析过后发觉可以用Factory模式来实现,于是就有了下面的东西!
项目描述: 一个简单的文章管理 b/s
开发语言: c#
数据库:access
类 Factory
public enum DocType{BuildingInfo=0,Affiche=1,Files=2,Law=3};
public class Factory
{
public Factory(){}
public static DocBase FactoryCreater(DocType docType,HttpServerUtility server)
{
DocBase doc = null;
switch (docType)
{
case DocType.BuildingInfo:
doc = new BuildingInfo(server);
break;
case DocType.Affiche:
doc = new Affiche(server);
break;
case DocType.Files:
doc = new Files(server);
break;
case DocType.Law:
doc = new Law(server);
break;
}
return doc;
}
}
BuilingInfo 类
public class BuildingInfo : DocBase
{
public BuildingInfo(){}
public BuildingInfo(HttpServerUtility server)
{
db = new Db(server);
strSelectAll = @"select * from tb_buildinginfo order by indate desc";
strDeleteDoc = @"delete * from tb_buildinginfo where id = ";
strAddDoc = @"insert into tb_buildinginfo(title,body,indate) values('$1','$2','$3')";
}
}
DocBase 类 基类
public class DocBase
{
protected string strSelectAll = String.Empty;
protected string strDeleteDoc = String.Empty;
protected string strAddDoc = String.Empty;
protected Db db;
public DocBase(){}
public DocBase(HttpServerUtility server)
{
db = new Db(server);
}
public DataTable GetDocs()
{
return db.RunSql(strSelectAll);
}
public DataTable GetTopDocs(int num)
{
string strSelectTop = strSelectAll.Replace("*","Top " + num.ToString() +" *");
return db.RunSql(strSelectTop);
}
public DataTable GetDoc(int id)
{
string strSelectDoc = strSelectAll.Replace("order by indate desc"," where id = " + id.ToString());
return db.RunSql(strSelectDoc);
}
public bool DeleteDoc(int id)
{
strDeleteDoc = strDeleteDoc + id.ToString();
if (db.RunCommandSql(strDeleteDoc) == -1)
{
return false;
}
else
{
return true;
}
}
public bool AddDoc(string title,string body,DateTime dt)
{
//略
}
}
BuildingInfo Affiche Files Law 只要分别给
protected string strSelectAll = String.Empty;
protected string strDeleteDoc = String.Empty;
protected string strAddDoc = String.Empty; 赋值就可以了! 呵呵!
博主看Web Cast后对Factory模式感兴趣,在简单文章管理B/S项目中用其实现功能。开发语言为C#,数据库用Access,文中给出Factory、BuildingInfo、DocBase等类的代码,展示了文章的查询、删除、添加等操作的实现。
465

被折叠的 条评论
为什么被折叠?



