Factory method Mode

Source: Internet
Author: User
Tags ole

In the first article: http://www.cnblogs.com/lystory/p/5083046.html

We introduced the simple factory model, which is an upgrade approach to a simple factory.

1, in a simple factory, we have to add a new product must be modified Dbfactory, recompile the program, let it support the new product instances.

2, in the factory method, we add a new product, only need to add a corresponding assembly of products, such as the addition of Oracle, only need to copy the assembly to the software directory. Then change the configuration file, the original code does not have to change. Isn't that cool?

This has all the advantages of a simple factory, but also solves an important disadvantage of the simple factory, the problem of coupling.

Program: This is the consumer, is stable.

Product Agreement (Idbaccess): Defines the standard service "GB" of the product.

Factory Agreement (Dbfactory): Defines the standard service "GB" of the factory.

Products (SQL Server, OLE DB): specific actual products.

Factory (Sqlfactory, oledbfactory): This is the escape product service, which wraps the service to the program.

1   class Program2     {3         Static voidMain (string[] args)4         {5 6Dbfactory sqlfactory =Newsqlfactory ();7 8Idbaccess sqlaccess =sqlfactory.create ();9 TenSqlaccess.insert ("specifying the use of SQL instance"); One  A console.readline (); -  -             //using reflection optimization, modifying a variable will give you the desired result without having to modify the code the             stringStrfactoryname ="sqlfactory"; -  -             ///through the configuration file, we can specify the factory to use, compared to the simple factory, when we create a new instance, we do not need to modify any of the original code, only need to copy the good assembly to the software directory. Change the config file again and it's OK. -             ///string strfactoryname = root. selectSingleNode ("DatabaseType"); +  - dbfactory dbfactory; +Dbfactory = (dbfactory) assembly.load ("Design Patterns"). CreateInstance ("design mode. Factory method mode."+strfactoryname); A  atIdbaccess idbaccess =dbfactory.create (); -  -Sqlaccess.insert ("at this point, it is up to the program runtime to dynamically specify which assembly code to use, based on the parameter Strfactoryname"); -  - console.readline (); -         } in     } -  to     /// <summary>  +     ///all data access interfaces -     /// </summary>  the      Public Abstract classidbaccess *     { $          Public Abstract stringInsertstringName);Panax Notoginseng     } -  the     /// <summary> +     ///SQL Products A     /// </summary> the      Public classsqlserver:idbaccess +     { -         //Related Methods $          PublicSystem.Data.SqlClient.SqlConnection Connection $         { -             Get -             { the                 return NewSystem.Data.SqlClient.SqlConnection (); -             }Wuyi         } the  -          Public Override stringInsertstringName) Wu         { -             return "SQL Server"; About         } $  -     } -  -     /// <summary> A     ///OLE DB Products +     /// </summary> the      Public classoledb:idbaccess -     { $         //Related Methods the          PublicSystem.Data.OleDb.OleDbConnection Connection the         { the             Get the             { -                 return NewSystem.Data.OleDb.OleDbConnection (); in             } the         } the  About          Public Override stringInsertstringName) the         { the             return "OLE DB"; the         } +  -     } the Bayi     /// <summary> the     ///implement plant and product Sqlfactory the     /// </summary> -      Public classsqlfactory:dbfactory -     { the          Public Overrideidbaccess Create () the         { the             return NewSQL Server (); the         } -     } the  the     /// <summary> the     ///implement plant and product Oledbfactory94     /// </summary> the      Public classoledbfactory:dbfactory the     { the          Public Overrideidbaccess Create ()98         { About             return NewOLE DB (); -         }101     }102 103     /// <summary>104     ///Defining factory base classes the     /// </summary>106      Public Abstract classdbfactory107     {108          Public Abstractidbaccess Create ();109}

If you add new Oracle

Just the following code: (Make it a single assembly, the namespace is the same, change it directly in the configuration file is ok.) It's perfect, isn't it? )

1     /// <summary>2     ///Oracle Products3     /// </summary>4      Public classoracle:idbaccess5     {6          Public Override stringInsertstringName)7         {8             return "Oracle";9         }Ten  One     } A  -     /// <summary> -     ///implement plant and product Oraclefactory the     /// </summary> -      Public classoraclefactory:dbfactory -     { -          Public Overrideidbaccess Create () +         { -             return NewOracle (); +         } A}

Factory method Mode

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.