asp.net simple factory model and factory method model--practical skills

Source: Internet
Author: User
Tags reflection
the difference between a simple factory model and a factory method pattern
The biggest advantage of a simple factory model is that the factory class contains the necessary logical judgments, dynamically instantiating the related classes according to the client's selection criteria, and to the client, the dependency on the specific product. The factory method pattern defines an excuse for creating objects, allowing subclasses to decide which class to instantiate, and the factory method is to delay the instantiation of a class to its subclasses. In fact, do some more contact is not difficult to find: when the Factory method mode is implemented, the client needs to decide to instantiate that factory to implement the Operation class, choose the problem of judgment or exist, that is to say, factory method The internal logic judgment of simple factory moved to the client code, I want to add some functions, it is necessary to modify the factory class , but now we just need to modify the client. The following is our teacher through a project to simple analysis of the difference between the factory model, I roughly sorted out, write a bad, only as a reference oh.

Now when we are developing some Web projects or WinForm projects, we all want the database to manage all the information, now on a system I developed "Longnan Original mall" as an example, if my system put into use, if I use the database is Access database, but, after a while, Since the original mall goods bought very good, Access database is nearly unable to meet the needs of customers, this time customers want to change to SQL Server database, so we have to rewrite the code, to replace the SQL Server database to achieve, if again after a period of time, SQL The server database does not meet the needs of the user, at this time the user wants to change to oracel database to realize it, here is not to say, we may be the development of people with vomiting blood ah, which caused us to ponder, how can we do a system so that we are not so troublesome to modify the code? This involves the design pattern, so there is a factory method model, the following is a small experiment with the factory method model to achieve such a process.

Create a new console application named Factorymethodpattern, add a Iproductdal interface to the console, define a method inside, and implement the following:
Copy Code code as follows:

Namespace Factorymethodpattern
{
public interface Iproductdal
{
void Insert ();
}
}

Then the new interface implements the Factory mode Iproductdalfactory, which is implemented as follows:
Copy Code code as follows:

Namespace Factorymethodpattern
{
public interface Iproductdalfactory
{
Iproductdal Createproductdal ();
}
}

Next, add Class Accessproductdal to the project, inherit from the interface Iproductdal, and implement it by outputting a piece of information to the console, which is implemented as follows:
Copy Code code as follows:

Namespace Factorymethodpattern
{
public class Accessproductdal:iproductdal
{
#region Iproductdal Members
public void Insert ()
{
Console.WriteLine ("Accessproductdal.insert");
}
#endregion
}
}

Then create a Accessproductdal factory class that allows him to inherit from the Iproductdalfactory interface and create a method to make the other return value Iproductdal. Finally, the implementation of the method returns the instantiated Accessproductdal, the implementation code is as follows:
Copy Code code as follows:

Namespace Factorymethodpattern
{
public class Accessproductdalfactory:iproductdalfactory
{
#region Iproductdalfactory Members
Public Iproductdal Createproductdal ()
{
return new Accessproductdal ();
}
#endregion
}
}

Next, write: Implement SQL Server database method, add a class sqlproductdal, make its method output a word
Copy Code code as follows:

Namespace Factorymethodpattern
{
public class Sqlproductdal:iproductdal
{
#region Iproductdal Members
public void Insert ()
{
Console.WriteLine ("Sqlproductdal.insert");
}
#endregion
}
}

Add the Sqlproductdalfactory class and implement the following code:
Copy Code code as follows:

Namespace Factorymethodpattern
{
public class Sqlproductdalfactory:iproductdalfactory
{
#region Iproductdalfactory Members
Public Iproductdal Createproductdal ()
{
return new Sqlproductdal ();
}
#endregion
}
}

Next, add the app.config file to implement what database the database is selected by the system, as follows:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<appSettings>
<add key= "Dalfactory" value= "Factorymethodpattern.sqlproductdalfactory"/>
</appSettings>
</configuration>

In the Write business Logic layer BLL, use reflection to get the path selected in app.config. Read it Out
The code is as follows:
Copy Code code as follows:

Namespace Factorymethodpattern
{
public class BLL
{
public void Insert ()
{
Using reflection to implement functionality
Iproductdalfactory factory = (iproductdalfactory) assembly.getexecutingassembly (). CreateInstance (configurationmanager.appsettings["dalfactory"]);
Iproductdal Pro = Factory. Createproductdal ();
Pro. Insert ();
}
}
}

Finally read the BLL layer information in the program, output information
Copy Code code as follows:

Namespace Factorymethodpattern
{
Class Program
{
Static Voidmain (string[] args)
{
BLL Product = new BLL ();
Product. Insert ();
Console.readkey ();
}
}
}

The last click to run the displayed output information is:

Now this small system is complete, now I want to join the Oracel database? I just write two Oracel database classes add to the inside, and then modify the path in the app.config OK.

    1. Summary: The factory approach overcomes the drawbacks of simple factories violating open-closed principles, having retained the advantage of the encapsulation object creation process, the factory method pattern is further abstracted and promoted by the Simple factory model, which, due to the use of polymorphism, maintains the advantages of a simple factory model and overcomes its drawbacks.
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.