Simple factory model of big talk Design Model

Source: Internet
Author: User

From the type of design patterns, the simple factory model is a creation model, also called the static factory method model, but not one of the GOF Design Patterns in section 23, A simple factory model is determined by a factory object to create a product instance. A simple factory model is the simplest and Practical Model in the factory model family, it can be understood as a special implementation of different factory models.
So what is the simple factory model? At noon, we eat in the zhongkuaicanteen. The zhongkuaicanteen meets our needs. The zhongkuaicanteen here is a simple factory. In real life, the factory is responsible for producing products ,, similarly, in the design mode, we can also understand the simple factory mode as a class responsible for producing objects. In our normal programming, when an object is created using the new keyword, this class depends on this object, that is, the coupling between them is high. When the demand changes, we have to modify the source code of this class, at this point, we can use the very important oo principle to solve this problem. This principle is to encapsulate changes. Naturally, we need to find the changed code and then encapsulate the changed code with classes, this idea is the implementation method of our simple factory model. Here is an example of our daily life.
During the cold and summer vacations, the canteen did not open the door. Some of the students who were upgrading to work classes could eat out. Of course, Handy children could cook at home, but it was quite troublesome to cook, because they had to go to the Yuan Chen supermarket to buy food, oil, salt, soy sauce, and vinegar are indispensable. However, you can also choose to go to the nest of nine, burn, fill in the pie, and select the pancake as you like. Here, each small stall serves as a simple factory, so how do we implement the things we experience every day in our programming world.

Self-cooking:

////// When you cook your own meals /// before there is no simple factory, you can only cook your own dishes ///Public class Customer {////// Cooking method /////////
 Public static Food Cook (string type) {Food food Food = null; // Shanshan said: What should I do if I want to eat sour and spicy potato shreds? // LAN Lan said: you can burn it yourself! // Shanshan said: Well, do it yourself! If (type. equals ("sweet and sour potato") {food = new ChiliSourPotato ();} // I want to eat ground ginger egg. I have to cook it myself. // I think I am in trouble, can someone help me? Else if (type. equals ("Ginger egg") {food = new PreservedEggSinGingerSauce ();} return food;} static void Main (string [] args) {Food food1 = Cook ("Spicy potato"); food1.Print (); Food food2 = Cook ("Ginger egg"); food1.Print (); Console. read ();}}////// Abstract course class ///Public abstract class Food {// The output menu. public abstract void Print ();}////// Sweet and sour potato shreds ///Public class ChiliSourPotato: Food {public override void Print () {Console. WriteLine ("a pair of hot and sour potato shreds! ");}}////// Preserved eggs with Ginger Juice ///Public class PreservedEggSinGingerSauce: Food {public override void Print () {Console. WriteLine ("One ginger egg ");}}
When Grandpa Mao hit Jiangshan, he said that he had a good windbreaker, and after intense learning, he had to do things on his own. But sometimes he did things again, I always want to buy some food. I will get bored with what I do at noon. I will treat salt as sugar when I am not sure how to cook. Then for lunch, I will ......, so occasionally, we can go to a small restaurant and call for the restaurant. The restaurant here is equivalent to a simple factory. We can hand over these operations to the restaurant. At this time, the dependency between consumers on food has changed from direct to indirect, in this way, another object-oriented principle is implemented to reduce the coupling between objects. Do you think of it every time you go to a restaurant? This can also be implemented in our programming world. The Code is as follows:

////// The customer acts as the client and calls a simple factory to produce objects. // that is, the customer ordering, and the cook (equivalent to a simple factory) is responsible for cooking (production objects )///Class Customer {static void Main (string [] args) {// the Customer wants to order spicy potato shredded Food food1 = FoodSimpleFactory. chiliSourPotatod (""); food1.Print (); // the customer wants to order a ginger Egg Food food2 = FoodSimpleFactory. preservedEggSinGingerSauce ("Ginger egg"); food2.Print (); Console. read ();}}////// Abstract course class ///Public abstract class Food {// The output menu. public abstract void Print ();}////// Sweet and sour potato shreds ///Public class ChiliSourPotatod: Food {public override void Print () {Console. WriteLine ("a piece of sour potato silk! ");}}////// Preserved eggs with Ginger Juice ///Public class PreservedEggSinGingerSauce: Food {public override void Print () {Console. WriteLine ("One ginger egg ");}}////// Simple factory, responsible for cooking ///Public class FoodSimpleFactory {public static Food CreateFood (string type) {Food food Food = null; if (type. equals ("sweet and sour potato") {food = new ChiliSourPotatod ();} else if (type. equals ("Ginger egg") {food = new PreservedEggSinGingerSauce () ;}return food ;}}
Next, let's introduce the UML diagram in the simple factory model. We know that the simple factory mode is also called the static method mode (because the factory class defines a static method ), A factory class determines which product class instance is created based on the input parameters (popular expression: the food is burned by the order placed by the customer ). The UML diagram of the simple factory mode is as follows:
C # With a certain understanding, I have entered the world of big birds and small dishes, short and refined little stories, and a dance of object-oriented programming thinking, become the most beautiful part of the book "big talk design model", like the studious, naive, persistent little dish, worship that good at inspiration, experienced big bird, just like this, slowly approaching .........

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.