Design Pattern series-simple factory pattern

Source: Internet
Author: User

Design Pattern series-simple factory pattern

The simple factory model is the most basic design model we come into contact with the design model. When writing a program, some classes may be involved. Which class should be instantiated during use? This is a problem solved by a simple factory.

To solve this problem, we need to write a logical judgment to implement it. For example, we design a restaurant program where food is available to customers, such as bean curd.

There is a cook in the hotel. After receiving the information, the cook will cook. Each cook can cook only one dish.(As shown in)

The bean curd brain did not actually exist at the beginning, but it was written on the menu. After the customer ordered the food, the cook would cook the food and then the cook would cook it again.

Some hotels have a lot of dishes, all of which are placed in the menu (class). When the user finishes the meal, the waiter needs to know that the cook corresponding to each dish is that person, then the waiter determines which cook to cook.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + PHN0cm9uZz4gICAgICAgICA8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20150117/20150117093018289.jpg" alt = "\">


At this time, if the waiter asks for leave, the entire hotel may encounter problems. This is not what we want. So we added a job like this to the hotel-the chef selects a person and has a form for him. Our waiter gave him the food he ordered, he used the form to determine which cook the dish should be prepared and then sent the food to the cook and the cook for operations.


In this way, after the number of persons notified by the kitchen staff is increased, the normal operation of our hotel will no longer be affected if the waiters cannot come, which increases the system stability, in addition, when we add new dishes, we only need to add new dishes to the staff and menu of the kitchen. The waiters will not be affected, and our scalability will be enhanced.

This is the simple factory model-What should we eat? You can ask me. The following code is similar:

Class Program {static void Main (string [] args) {// hand over the menu to the user Console. WriteLine ("enter what you want to eat? We have tofu brain, scrambled eggs, and fried beans for your choice !! "); // Select the required menu string greens =" "; greens = Console. readLine (); chef Chef; // after calling, select Chef = greensoptionfactory. getchef (greens); // get the food string result = Chef. getfood; Console. writeLine ("hello, sir, this is yours: {0 }! ", Result) ;}} public class greensoptionfactory {// select the chef public static chef getchef (string greens) {Chef chef = null; switch (greens) {case" Tofu brain ": chef = new cheftofu (); break; case "scrambled eggs": Chef = new chefegg (); break; case "fried beans": Chef = new chefbean (); break ;} // output return Chef ;}/// defines the cook class public class chef {private string food = ""; // The Cook's cooking method public virtual string getfood {get {return food ;}}// the cook's public class cheftofu: chef {public override string getfood {get {return "bean curd brain" ;}}/// chef public class chefbean for fried beans: chef {public override string getfood {get {return "" ;}}// public class chefegg for scrambled eggs: chef {public override string getfood {get {return "scrambled eggs ";}}}}

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.