惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - 津渡卧龙

shapefile 和 MapGIS 文件格式之间的转换 Orbit Downloader 1.5.4多国语言版 迅雷工作原理的推测及慎用它的理由 GIS开发平台的未来 — .NET还是J2EE? eDonkey协议 BitTorrent 协议规范 C#中调用API [导入]Google(谷歌)全球 “开发者日”活动详情 [导入] 什么是数据挖掘? 什么是数据挖掘? C#中的“装箱”与“拆箱” [导入]C#中的“装箱”与“拆箱” C#委托的具体实现方法 [导入]SEO本能发挥效力 [导入]传Google向马桶浏览器投资100万美元 [导入]我们对谷歌失望? [导入]谷歌就谷歌输入法对用户及搜狐等各方致歉 [导入]三层式开发中的层次划分讨论 [导入]Google AdSense开放API 将允许AdWords和AdSense互转资金
[导入]C#设计模式之简单工厂篇
津渡卧龙 · 2007-04-12 · via 博客园 - 津渡卧龙

首先定义一个接口,具体名为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();

        }

    }

    好了,工程峻工了,你们明白了没有?
文章来源:http://www.ad-words.com.cn/default.asp?id=546