Asp.net simple factory mode and factory method mode

Source: Internet
Author: User

Differences between simple factory mode and factory method mode
The biggest advantage of the simple factory mode is that the factory class contains the necessary logic judgment, and the related class is dynamically instantiated according to the client selection conditions. For the client, the dependency of specific products is exclusive. The factory method mode defines an excuse for creating an object, so that the subclass decides which class to instantiate. The factory method delays the instantiation of a class to its subclass. In fact, it is not difficult to find out more links: when implementing the factory method mode, the client needs to decide to instantiate the factory to implement the computing class and choose whether the problem still exists. That is to say, the factory method is simple. The internal logic judgment of the factory is moved to the client code. I want to add some functions. I originally needed to modify the factory class, but now we only need to modify the client. The following is a simple analysis of the differences between factory models by our teacher through a project. I have roughly sorted it out, but it is not easy to write, just for reference.

Now, when we develop some web or WInform projects, we need databases to manage all the information. Now we take the developed system LongYuan Mall as an example, if my system is put into use, if my database is an Access database, but after a while, because LongYuan mall has bought a good product, the Access database cannot meet the customer's needs. In this case, the customer wants to replace it with the SQL Server database. In this case, we must re-compile the code and change it to the SQL Server database, after a while, the SQL Server database cannot meet users' needs. At this time, the user wants to switch to the replicel database for implementation, maybe our developers are vomiting blood, which will cause us to think deeply. How can we make a system that will make us not so troublesome to modify the code? This involves the design model, so there is a factory method model, the following uses the factory method model to do a small experiment to achieve such a process.

Create a new console application named FactoryMethodPattern. Add an IProductDAL interface to the console and define a method as follows:
Copy codeThe Code is as follows:
Namespace FactoryMethodPattern
{
Public interface IProductDAL
{
Void Insert ();
}
}

Create an interface to implement the factory mode IProductDALFactory. The implementation is as follows:
Copy codeThe Code is as follows:
Namespace FactoryMethodPattern
{
Public interface IProductDALFactory
{
IProductDAL CreateProductDAL ();
}
}

Next, add the AccessProductDAL class to the project, inherit from the IProductDAL interface, and output a message to the console, as follows:
Copy codeThe Code is as follows:
Namespace FactoryMethodPattern
{
Public class AccessProductDAL: IProductDAL
{
# Region IProductDAL Member
Public void Insert ()
{
Console. WriteLine ("AccessProductDAL. Insert ");
}
# Endregion
}
}

Create an AccessProductDAL factory class to inherit from the IProductDALFactory interface, create a method to make other return values IProductDAL, and finally return the instantiated AccessProductDAL in the implementation of the method. The implementation code is as follows:
Copy codeThe Code is as follows:
Namespace FactoryMethodPattern
{
Public class AccessProductDALFactory: IProductDALFactory
{
# Region IProductDALFactory Member
Public IProductDAL CreateProductDAL ()
{
Return new AccessProductDAL ();
}
# Endregion
}
}

Next, let's write: implement the SQL Server database method, add a class SqlProductDAL, and make the method output a sentence
Copy codeThe Code is as follows:
Namespace FactoryMethodPattern
{
Public class SqlProductDAL: IProductDAL
{
# Region IProductDAL Member
Public void Insert ()
{
Console. WriteLine ("SqlProductDAL. Insert ");
}
# Endregion
}
}

Add the SqlProductDALFactory class. The implementation code is as follows:
Copy codeThe Code is as follows:
Namespace FactoryMethodPattern
{
Public class SqlProductDALFactory: IProductDALFactory
{
# Region IProductDALFactory Member
Public IProductDAL CreateProductDAL ()
{
Return new SqlProductDAL ();
}
# Endregion
}
}

Next, add the App. config file to implement the database selected by the system. The Code is as follows:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "DALFactory" value = "FactoryMethodPattern. SqlProductDALFactory"/>
</AppSettings>
</Configuration>

In the business logic layer, use BLL to obtain the path selected in app. config through reflection. Read
The Code is as follows:
Copy codeThe Code is as follows:
Namespace FactoryMethodPattern
{
Public class BLL
{
Public void Insert ()
{
// Use reflection to implement the function
IProductDALFactory factory = (IProductDALFactory) Assembly. GetExecutingAssembly (). CreateInstance (ConfigurationManager. deleetworkflow ["DALFactory"]);
IProductDAL pro = factory. CreateProductDAL ();
Pro. Insert ();
}
}
}

Finally, read BLL layer information and output information in program.
Copy codeThe Code is as follows:
Namespace FactoryMethodPattern
{
Class Program
{
Static voidMain (string [] args)
{
BLL product = new BLL ();
Product. Insert ();
Console. ReadKey ();
}
}
}

Finally, click "run" to display the following output information:

Now this small system is complete. Now I want to join the replicel database? I just need to add the classes in the two mongoel databases and modify the path in app. config.

  1. Conclusion: The Factory method overcomes the disadvantages of a simple factory that violates the open-closed principle and maintains the advantages of the process of creating encapsulated objects. The factory method mode is further abstracted and promoted by the simple factory mode, due to the use of polymorphism, the factory method pattern maintains the advantages of the simple factory pattern and overcomes its shortcomings.

Related Article

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.