What does a novice understand of the factory model (Factory pattern) look like?

Source: Internet
Author: User

Directly began to say, do not waste garden friends precious time!

What is a factory model?

Before you study, ask: "What is it?" ”。

Factory mode, which is one of the most common design patterns in the project.

It belongs to the creation mode , and the simple understanding of the creation pattern is to give the instantiation work to another object to complete.

Factory mode (also known as static Factory mode static Factory Method), it is the simplest mode in software design mode.

In order to solve what problem?

The factory pattern makes the code clear, reduces coupling , and the caller obtains the desired instantiation through an interface/abstract class without worrying about how the details are implemented.

The factory model is a typical decoupling mode, and the Dimitri law is particularly evident in the factory model.

(said relatively simple, read a number of sites and blogs, I personally think that this is the factory model to solve the most fundamental problem, if interested, you can go to look at other articles.) )

When do I use it?

The Factory mode belongs to the Create class pattern, and the factory method pattern can be used when creating different object instances under the same conditions.

Simple objects, especially those that need to be created with new, do not need to use Factory mode. (It is complicated to use the factory, simply do not need to use, but increase the complexity of the program.) )

 

Practice deepens Understanding:

I asked you to go to the restaurant to eat, there are Northeast food (I am Northeast yin), Zhejiang cuisine, Beijing cuisine and so on, I took you into the restaurant, and then ordered the Northeast food. This process, in fact, is an implementation of the factory model.

First of all, no matter what kind of restaurant it is, first it must be a "hotel", must be cooked (briefly described), the following is the code practice process.

  

First, we create a restaurant interface class, there is only one action, to stipulate that all restaurants must be cooking!!!

 /// <SUMMARY>  ///  hotel interface class  /// </SUMMARY>  public  interface   irestaurants { /// <SUMMARY>  ///  Every restaurant must cook  /// </summary>  /// <returns></returns>
      string   Cook (); }
View Code

Second, the next creation of a few cuisines of the specific restaurant.

    /// <summary>    /// Beijing Hotel     /// </summary>    class beijing:irestaurants    {        publicstring  Cook ()        {            return" Here's your Beijing food . " ;        }    }
View Code
    /// <summary>    /// Northeast Hotel     /// </summary>    class dongbei:irestaurants    {        publicstring  Cook ()        {            return" Here you are, northeast food . " ;        }    }
View Code
    /// <summary>    /// Zhejiang Hotel     /// </summary>    class zhejiang:irestaurants    {        publicstring  Cook ()        {            return" here you are. Zhejiang Cuisine " ;        }    }
View Code

Third, the restaurant has, and then this is the simple factory model of the focus. How do I get these restaurants to serve me correctly? (if the reader said to the Northeast Hotel must not give Beijing food, then please go out right, kneeling cry!!!) )

Then there needs to be a "factory" to help me tell what restaurant to Cook (the example of which restaurant to create).

    /// <summary>    ///Hotel Factory class, according to the name of the hotel to create an example. /// </summary>     Public classRestaurantsimplefactory { PublicIrestaurants Giveme (stringname) {            Switch(name) { Case "Dongbei":                    return NewDongbei ();  Case "Beijing":                    return NewBeijing ();  Case "Zhejiang":                    return NewZhejiang (); default:                    return NewDongbei (); }        }    }
View Code

Four, the preparation work is finished, can serve the dishes.

     Public classProgram {Static voidMain (string[] args) {            //Create an example of a hotel factory            varRestaurantsimplefactory =Newrestaurantsimplefactory (); //use the hotel interface to recruit a hotel instance that implements itIrestaurants restaurant = Restaurantsimplefactory.giveme ("Dongbei"); stringCookwhat =Restaurant.            Cook (); Console.WriteLine ("Hotel:"+cookwhat);        Console.ReadLine (); }    }
View Code

Illustration: This is based on my personal understanding of the map, may not be the same as the network of Daniel.

Tag: The Simple factory also contains another implementation, "factory method", followed by a deep study.

If my understanding is wrong, want to spray on the spray is OK, don't scold me! May learn in your anger that I do not understand. Thanks for watching!

What does a novice understand of the factory model (Factory pattern) look like?

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.