C # simple factory of design patterns

Source: Internet
Author: User

First, define an interface named idatabase. In this interface, define the method name, parameters, and return values of database operations. In this case, I define the following methods:

Public interface idatabase

{

Bool connect (string connectstring );

Bool open ();

Bool command (string SQL );

Void close ();

}

Important Note: "The interface should be cautious throughout its life, and its definition should not be confused." You must carefully compile the interface and repeat parameters and return values. Why? Because all the implementation classes must be written according to the interface specification, that is, the interface definition is public. Once the interface is changed, the consequence is that all implementation classes must be adjusted accordingly.

 

Then, you need to write the specific implementation class. You need to define the implementation classes of the idatabase as to the number of different types of databases required by the customer. Although the workload is a little heavy, but when you see a smile satisfied with the customer, you will have a kind of heartfelt happiness. Well, the sqlserver implementation class code is as follows:

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 ();

}

}

 

Haha, it's a little long. After reading it with a bit of teeth, you will feel very comfortable. If you feel this way now, please make persistent efforts to write specific code for the Oracle implementation class, if you are free, just draw a prototype:

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 ()

{

 

}

}

 

Well, it's good. If you have many types of databases, write different implementation-class code. I won't go into details here. What about next? Smart readers will surely think of this question: how can this interface be used with so many implementation classes? Let's define another class called factory, which decides which database to choose for operation. This class is relatively simple:

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 ();

}

}

}

Do you understand? Well, we should make a distinguished, always-noble customer appear. Only him can decide which database to use, as you can see:

Public class client

{

Public static void main ()

{

// Get the database information from web. config.

String dbtype = configurationsettings. receivettings ["dbtype"];

String dbconnectstring = configurationsettings. deleettings ["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 ();

}

}

 

Okay, the project is complete. Do you understand?

Question: What are the application scenarios and limitations of simple factories?

Assignment question: If you want to develop a multimedia player that can be played by both window mediaplayer and RealPlayer, and QuickTime, Which player should be selected by the customer, draw a UML diagram and write the code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.