C#设计模式之简单工厂篇(转载)

C#设计模式之简单工厂篇(转载),第1张

概述 首先定义一个接口,具体名为Idatabase,在这个接口中,定义好数据库 *** 作的方法名和参数,以及返回值,本案例中我定义如下方法:public interface IDatabase{   bool Connect(string ConnectString);    bool Open();    bool Command(string SQL);    void Close();}    重要提醒:“接口一生唯谨慎,定义大事不糊涂”,编写接口时一定要考虑周全,并对参数、返回值进行反复推敲,为什么?因为所有的实现类都是要根据该接口的规范进行代码具体编写,也即接口的定义是公用的,一旦改动了接口,后果就是所有的实现类也都必须相应调整。    然后就是编写具体的实现类了,客户要求多少不同类型的数据库,你就定义多少个Idatabase的实现类,虽然工作量大了点,可当你看到客户满意的笑容时,你心里也就会有一种由衷的幸福感,好了,SqlServer实现类代码如下:public class SqlServer : IDatabase    {        SqlConnection conn;        SqlCommand command;        public bool Connect(string ConnectString)        {            try            {                conn = new SqlConnection(ConnectString);                return true;            }            catch(SqlException)            {                return false;            }         }        public bool Open()        {            try            {                conn.Open();                return true;            }            catch(SqlException)            {                return false;            }        }        public bool Command(string SQL)        {            try            {                command = new SqlCommand(SQL,conn);                command.ExecuteNonQuery();                return true;            }            catch(SqlException)            {                return false;            }        }        public void Close()        {            conn.Close();            conn.Dispose();        }    } 呵呵,有点长,咬着牙读完,心里明白了就会很舒服的,如果你现在有这种感觉了,好,再接再厉,再为Oracle实现类编写具体代码吧,依葫芦画瓢,大家有空就画一下吧,我就画个雏形了:public class Oracle : IDatabase    {        public Oracle()        {        }        public bool Connect(string ConnectString)        {            return true;        }        public bool Open()        {            return true;        }        public bool Command(string SQL)        {            return true;        }        public void Close()        {        }    }    嗯,不错,你有多少种数据库就编写不同的实现类代码吧,这里就不赘述了,接下来呢?聪明的读者一定会想到这个问题:这个接口和这么多的实现类怎么用啊?我们再定义一个称之为工厂的类,由它来决定选用哪种数据库为进行 *** 作,这个类比较简单:public class Factory    {        public static IDatabase SelectDatabase(string DatabaseType)        {            switch(DatabaseType)            {                case "SqlServer":                    return new SqlServer();                case "Oracle":                    return new Oracle();                default:                    return new SqlServer();            }        }    }看明白了吗?好了,我们该让尊敬的、永远高贵的客户出场了,只有他,唯有他才有决定用哪种数据库的最高权限,你看,他这样用:public class Client    {        public static void Main()        {            //Get the database information from Web.Config.            string DBType = ConfigurationSettings.AppSettings["DBType"];            string DBConnectString = ConfigurationSettings.AppSettings["DBConn"];            IDatabase DB = Factory.SelectDatabase(DBType);            //Connect the selected database.            if(DB.Connect(DBConnectString)==false)            {                Console.WriteLine("The database {0} can@#t be connected.",DBType);                return;            }            //Open database.            if(DB.Open()==false)            {                Console.WriteLine("The database {0} can@#t be opened, the connect string is {1}.",DBType,DBConnectString);                return;            }            //Execute SQL Command.            string SQL = "update Order set price = price * 0.07 where productID = @#002@#";            if(DB.Command(SQL))            {                //Do something...            }            else            {                Console.WriteLine("The Operator is not success. SQL statament is {0}",SQL);                DB.Close();                return;            }            DB.Close();        }    }    好了,工程峻工了,你们明白了没有?    思考题:简单工厂的应用场合和局限性?    作业题:假如要开发一个多媒体播放器,既能用Window MediaPlayer播放,又能用RealPlayer播放,还能用QuickTime播放,具体用什么播放器,由客户选择,请你画出UML图并写出代码。

 首先定义一个接口,具体名为IDatabase,在这个接口中,定义好数据库 *** 作的方法名和参数,以及返回值,本案例中我定义如下方法:
<p >public interface IDatabase


<p >{


<p >   bool Connect(string ConnectString);


<p >    bool open();


<p >    bool Command(string sql);


<p >    voID Close();


<p >}


<p >    重要提醒:“接口一生唯谨慎,定义大事不糊涂”,编写接口时一定要考虑周全,并对参数、返回值进行反复推敲,为什么?因为所有的实现类都是要根据该接口的规范进行代码具体编写,也即接口的定义是公用的,一旦改动了接口,后果就是所有的实现类也都必须相应调整。


<p >    然后就是编写具体的实现类了,客户要求多少不同类型的数据库,你就定义多少个IDatabase的实现类,虽然工作量大了点,可当你看到客户满意的笑容时,你心里也就会有一种由衷的幸福感,好了,sqlServer实现类代码如下:


<p >public class sqlServer : IDatabase


<p >    {


<p >        sqlConnection conn;


<p >        sqlCommand command;


<p >        public bool Connect(string ConnectString)


<p >        {


<p >            try


<p >            {


<p >                conn = new sqlConnection(ConnectString);


<p >                return true;


<p >            }


<p >            catch(sqlException)


<p >            {


<p >                return false;


<p >            }


<p > 


<p >        }


<p >        public bool open()


<p >        {


<p >            try


<p >            {


<p >                conn.open();


<p >                return true;


<p >            }


<p >            catch(sqlException)


<p >            {


<p >                return false;


<p >            }


<p >        }


<p >        public bool Command(string sql)


<p >        {


<p >            try


<p >            {


<p >                command = new sqlCommand(sql,conn);


<p >                command.ExecuteNonquery();


<p >                return true;


<p >            }


<p >            catch(sqlException)


<p >            {


<p >                return false;


<p >            }


<p >        }


<p >        public voID Close()


<p >        {


<p >            conn.Close();


<p >            conn.dispose();


<p >        }


<p >    } 呵呵,有点长,咬着牙读完,心里明白了就会很舒服的,如果你现在有这种感觉了,好,再接再厉,再为Oracle实现类编写具体代码吧,依葫芦画瓢,大家有空就画一下吧,我就画个雏形了:


<p >public class Oracle : IDatabase


<p >    {


<p >        public Oracle()


<p >        {


<p >        }


<p >        public bool Connect(string ConnectString)


<p >        {


<p >            return true;


<p >        }


<p >        public bool open()


<p >        {


<p >            return true;


<p >        }


<p >        public bool Command(string sql)


<p >        {


<p >            return true;


<p >        }


<p >        public voID Close()


<p >        {


<p >        }


<p >    }


<p >    嗯,不错,你有多少种数据库就编写不同的实现类代码吧,这里就不赘述了,接下来呢?聪明的读者一定会想到这个问题:这个接口和这么多的实现类怎么用啊?我们再定义一个称之为工厂的类,由它来决定选用哪种数据库为进行 *** 作,这个类比较简单:


<p >public class Factory


<p >    {


<p >        public static IDatabase SelectDatabase(string DatabaseType)


<p >        {


<p >            switch(DatabaseType)


<p >            {


<p >                case "sqlServer":


<p >                    return new sqlServer();


<p >                case "Oracle":


<p >                    return new Oracle();


<p >                default:


<p >                    return new sqlServer();


<p >            }


<p >        }


<p >    }看明白了吗?好了,我们该让尊敬的、永远高贵的客户出场了,只有他,唯有他才有决定用哪种数据库的最高权限,你看,他这样用:


<p >public class ClIEnt


<p >    {


<p >        public static voID Main()


<p >        {


<p >            //Get the database information from Web.Config.


<p >            string DBType = ConfigurationSettings.AppSettings["DBType"];


<p >            string DBConnectString = ConfigurationSettings.AppSettings["DBConn"];


<p >            IDatabase DB = Factory.SelectDatabase(DBType);


<p >            //Connect the selected database.


<p >            if(DB.Connect(DBConnectString)==false)


<p >            {


<p >                Console.Writeline("The database {0} <a href="mailto:can@#t"><font color="#000000" size="2">can@#t be connected.",DBType);


<p >                return;


<p >            }


<p >            //Open database.


<p >            if(DB.open()==false)


<p >            {


<p >                Console.Writeline("The database {0} <a href="mailto:can@#t"><font color="#000000" size="2">can@#t be opened,the connect string is {1}.",DBType,DBConnectString);


<p >                return;


<p >            }


<p >            //Execute sql Command.


<p >            string sql = "update Order set price = price * 0.07 where productID = @#002@#";


<p >            if(DB.Command(sql))


<p >            {


<p >                //Do something...


<p >            }


<p >            else


<p >            {


<p >                Console.Writeline("The Operator is not success. sql statament is {0}",sql);


<p >                DB.Close();


<p >                return;


<p >            }


<p >            DB.Close();


<p >        }


<p >    }


<p >    好了,工程峻工了,你们明白了没有?


<p >    思考题:简单工厂的应用场合和局限性?


<p >    作业题:假如要开发一个多媒体播放器,既能用Window MediaPlayer播放,又能用RealPlayer播放,还能用QuickTime播放,具体用什么播放器,由客户选择,请你画出UML图并写出代码。

总结

以上是内存溢出为你收集整理的C#设计模式之简单工厂篇(转载)全部内容,希望文章能够帮你解决C#设计模式之简单工厂篇(转载)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264033.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存