Design Model Learning (1) --- Three factory models

Source: Internet
Author: User

Tell a story. Once upon a time, a boss was very rich and liked digital products and mobile phones. He often liked to buy smart phones. How can he buy them? Of course, you can buy at various mobile phone stores, such as Apple stores and Samsung stores. But the boss is very busy. What should I do? The boss was born as a programmer. Object-oriented. Add a level to ask the Secretary to buy the model and write a piece of paper to the secretary. It is easy for the boss to do so. Just steal the result of the secretary's work (because the salary is paid ). As instructed by the boss, the secretary can buy any brand of mobile phones at the corresponding store. Okay, so we have to deal with it for a while, because the rise of smartphones in this period can also be done by several companies.

Time flies, and time flies. With the advancement of science and technology, the smartphone market is too large. As for the first year of a smartphone, the saying goes, "on the air, pigs can fly ". Therefore, a large number of smartphone manufacturers emerged, and a large number of smartphone brands emerged, but this boss has gone from buying smartphones to collecting and researching smartphones, because they are not cheap. So if you know where to purchase a new phone, ask the Secretary to buy it. Oh, my God. The secretary is busy now. You know, as there are so many smart phone brands in the world, the workload of the secretary is getting heavier now, not only responsible for various meetings of the company, but also going out to buy a mobile phone every day, I am very upset. Of course, in the eyes of the boss, he added the Secretary's salary and assigned the Secretary several colleagues to help the Secretary buy a smart phone. The secretary raised his salary and some colleagues shared it with him. The secretary was so cool that he wanted to be promoted and given a salary increase to act as the CEO to marry Gao fushuai. Then he was very happy. The Secretary is also the manager. When the boss wants to buy a new brand mobile phone, he must first talk to the secretary, and then the Secretary is allocating other colleagues to buy the phone separately. Of course, the boss needs to pay more labor fees for this, which is just a few dollars. OK. It takes a while.

Time flies and time flies. With the product iterations of major mobile phone manufacturers, Nima iPhone has reached 6. It's even more crazy for the boss to collect all models of all brands. The secretary is busy again, because the secretary wants to figure out which brands have been made to several generations. Of course, this is not too difficult. Baidu, OK, after learning about the series products of each brand, I told other colleagues to buy the series products separately. Of course, if there are too many product categories, add another colleague. Just pay a few more HR fees.

In the end, tuhao's boss tried to experience all his smartphones and thought it would not work. He did not grasp the consumer's fever, so he planned to create a product on his own, so he quietly created a company, we are bound to build a global heavyweight smartphone... A wonderful thing is about to happen .... so the question is, which is the best design mode?

1. Simple Factory)

OK, the Primary School Chinese teacher taught us how to divide the central idea according to the natural section when analyzing Chinese lessons. In the first paragraph, the programmer's boss made use of the expertise he had learned to turn the actual mobile phone purchase problem into an object-oriented problem (for Secretary wow! Good Happiness ). The secretary can buy a mobile phone as required by the boss. The Secretary is the factory (is the secretary of Shenma a factory ?) Abstract: factory! We specialize in buying various brands of mobile phones. But for mobile phones, do you need to abstract them? Major mobile phone manufacturers are also unlikely to make chips, screens, and other accessories. Therefore, they are also a technical component assembly factory, so they can be abstracted for travel (with text messages, phone calls, devices with functions such as Internet access), and various actual mobile phone manufacturers are designed according to their respective designs. The Java code is as follows:

// Abstract class phone implementation abstract class phone {// abstract class at least one abstract method. This is abstract void phoneinf ();} // a specific product class iPhone extends phone {public void phoneinf () {system. out. println ("buy iPhone") ;}} class Xiaomi extends phone {public void phoneinf () {system. out. println ("buy Xiaomi") ;}// factory class secretary {public static phone buyphone (string brand) {If (brand. equals ("iPhone") return new iPhone (); else {If (brand. equals ("Xiaomi") return New Xiaomi (); else return NULL ;}} class main {public static void main (string [] ARGs) {// The boss only needs to tell the secretary what brand of mobile phone he wants to buy. Phone p1 = secretary. buyphone ("Xiaomi"); p1.phoneinf (); phone P2 = secretary. buyphone ("iPhone"); p2.phoneinf ();}}

/*************************************** * ******************************* // C ++ implementation simple factory mode *//********************************** **************************************/# include <iostream> # include <string> using namespace STD; class phone {public: Virtual void phoneinf () = 0; // pure virtual function}; Class iPhone: public phone {public: void phoneinf () {cout <"buy iPhone" <Endl ;}}; class Xiaomi: public phone {public: void phoneinf () {cout <"buy Xiaomi" <Endl ;}}; class secretary {public: static phone * buyphone (string brand) {If (brand = "iPhone ") return new iPhone (); else {If (brand = "Xiaomi") return New Xiaomi (); else return NULL ;}}; int main () {Phone * P1 = SECRETARY: buyphone ("iPhone"); P1-> phoneinf (); phone * P2 = SECRETARY: buyphone ("Xiaomi "); p2-> phoneinf (); Return 0 ;}

The simple factory model consists of the following:

A. The factory class (secretary here) is the core part. Implemented by a specific class.

B. Abstract products. (Here is phone), which is implemented by an interface or abstract class in Java. C ++ can be implemented by abstract classes (classes containing pure virtual functions.

C. Specific products. This refers to products produced by mobile phone manufacturers, such as iPhone and Xiaomi. Java inherits or implements abstract classes and interfaces to create an actual product instance. Subclass instantiation object in C ++.

In this way, the boss (actual customer) only requests, and the execution will be done by the Secretary (factory. Factories must have functions such as logical judgment. For example, secretaries must be able to distinguish various mobile phone brands. In this way, you can do this when you are a child. Once the boss needs some new mobile phones, but when the data volume is small, the secretary needs to buy some brands (for the factory class, it is necessary to modify the class ).

2. Factory method)

The second paragraph tells us that when the Secretary is too busy to frequently modify the factory class, it is equivalent to causing a huge workload for the secretary. Someone must help. Now the Secretary is responsible for arranging the task. The specific mobile phone number is called by his staff and then the secretary. At this time, you need to add an abstract factory class to distribute tasks only. The specific work is done by the actual factory. The Code is as follows:

// Java factory method mode // abstract product abstract class phone {public abstract void phoneinf () ;}// specific product class iPhone extends phone {public void phoneinf () {system. out. println ("buy iPhone") ;}} class Xiaomi extends phone {public void phoneinf () {system. out. println ("buy Xiaomi") ;}// abstract factory class abstract class secretary {public abstract phone buyphone ();} // actual factory class persona_iphone extends Secretary {public phone buyphone () {return new iPhone () ;}} class personb_xiaomi extends Secretary {public phone buyphone () {return New Xiaomi () ;}} class main {public static void main (string [] ARGs) {// The Secretary assigns person1 to buy iphonepersona_iphone person1 = new persona_iphone (); phone p1 = person1.buyphone (); p1.phoneinf (); // The Secretary assigns person2 to buy xiaomipersonb_xiaomi person2 = new personb_xiaomi (); phone P2 = person2.buyphone (); p2.phoneinf ();}}
/*************************************** * ********************************* C ++ implements the factory method mode **************************************** * *******************************/# include <iostream> # include <string> using namespace STD; class phone {public: Virtual void phoneinf () = 0; // pure virtual function}; Class iPhone: public phone {public: void phoneinf () {cout <"buy iPhone" <Endl ;}}; class Xiaomi: public phone {public: void phoneinf () {cout <"buy Xiaomi" <Endl ;}; // abstract factory class secretary {public: virtual phone * buyphone () = 0 ;}; // actual factory class persona_iphone: Public Secretary {public: Phone * buyphone () {return new iPhone () ;}}; class personb_xiaomi: Public Secretary {public: Phone * buyphone () {return New Xiaomi () ;}}; int main () {// The secretary calls person1 to buy iPhone person1; phone * phone1 = person1.buyphone (); phone1-> phoneinf (); // The secretary calls person2 to buy xiaomipersonb_xiaomi person2; phone * phone2 = person2.buyphone (); phone2-> phoneinf (); Return 0 ;}

The factory method format is as follows:

A. Abstract product. (Here is phone), which is implemented by an interface or abstract class in Java. C ++ can be implemented by abstract classes (classes containing pure virtual functions.

B. Specific products. This refers to products produced by mobile phone manufacturers, such as iPhone and Xiaomi. Java inherits or implements abstract classes and interfaces to create an actual product instance. Subclass instantiation object in C ++.

C. abstract factory class (here is the secretary). Compared with the simple factory model, the added class frees the secretary from the actual labor and is only responsible for arranging tasks, implemented by an abstract class.

D. Actual factory. It refers to the person who actually buys a mobile phone. Actual class instantiation.

This will make it easier for the secretary to buy a mobile phone. As long as there are enough people to buy a new mobile phone, you only need the Secretary to send an individual, in this way, the secretary is still easy and scalable. However, the only downside is that you need to hire people to pay for it. The actual code should be more than a lot of practical classes. Whatever, things can meet the requirements.

3. Abstract Factory Model

The third natural section tells us that mobile phone manufacturers are all out of the series of mobile phones, and neither of the first two models is applicable. It is impossible for the secretary to instruct a to buy iPhone 1 and ask B to buy iPhone 2 .. yes, but it is not necessary. You can definitely buy iPhone 2 for iPhone 1. Therefore, the secretary needs to re-plan the task list to reschedule the task.

// Java Abstract Factory mode // abstract product class abstract class phone {public abstract void phoneinf () ;}// specific product class iphone1 extends phone {public void phoneinf () {system. out. println ("buy iphone1");} class iphone1s extends phone {public void phoneinf () {system. out. println ("buy iphone1s") ;}} class xiaomi1 extends phone {public void phoneinf () {system. out. println ("buy xiaomi1");} class xiaomi1s extends phone {public void phoneinf () {system. out. println ("buy xiaomi1s") ;}// abstract factory class abstract class secretary {public abstract phone buyphone_1 (); // buy a generation of mobile phone public abstract phone buyphone_1s (); // buy an upgraded version of the first generation // public abstract phone buyphone_2 (); // buy a second generation mobile phone // continue to expand ...} // actual factory class persona_iphone extends Secretary {public phone buyphone_1 () // buy an iPhone generation phone {return New iphone1 ();} public phone buyphone_1s () // buy iphone1s {return New iphone1s () ;}} class personb_xiaomi extends Secretary {public phone buyphone_1 () {return New xiaomi1 ();} public phone buyphone_1s () {return New xiaomi1s () ;}} class main {public static void main (string [] ARGs) {// The Secretary assigns person1 to buy iphone1, 1 s product persona_iphone person1 = new persona_iphone (); phone p1 = person1.buyphone _ 1 (); // iPhone 1phone P2 bought by persona = person1.buyphone _ 1 S (); // iphone1sp1 bought by persona. phoneinf (); p2.phoneinf (); // The Secretary assigns person2 to buy xiaomi1, 1 s product personb_xiaomi person2 = new personb_xiaomi (); phone P3 = person2.buyphone _ 1 (); p3.phoneinf (); phone P4 = person2.buyphone _ 1 S (); p4.phoneinf ();}}

/*************************************** * ******************************* C ++ implements the abstract factory mode * **************************************** * **************************/# include <iostream> # include <string> using namespace STD; // abstract product class phone {public: Virtual void phoneinf () = 0; // pure virtual function}; // actual product class iphone1: public phone {public: void phoneinf () {cout <"buy iphone1" <Endl ;}}; class iphone1s: public phone {public: void phoneinf () {cout <"buy iphone1s" <Endl ;}}; class xiaomi1: public phone {public: void phoneinf () {cout <"buy xiaomi1" <Endl ;}}; class xiaomi1s: public phone {public: void phoneinf () {cout <"buy xiaomi1s" <Endl ;}; // abstract factory class secretary {public: virtual phone * buyphone1 () = 0; // buy a generation of mobile phone virtual phone * buyphone1s () = 0; // buy 1 s mobile phone //... continue extension}; // actual factory class persona_iphone: Public Secretary {public: Phone * buyphone1 () {return New iphone1 ();} phone * buyphone1s () {return New iphone1s () ;}}; class personb_xiaomi: Public Secretary {public: Phone * buyphone1 () {return New xiaomi1 ();} phone * buyphone1s () {return New xiaomi1s () ;}}; int main () {// The secretary calls person1 to buy iPhone 1, 1 spersona_iphone person1; phone * phone1 = person1.buyphone1 (); phone * phone2 = person1.buyphone1s (); phone1-> phoneinf (); phone2-> phoneinf (); // The secretary calls person2 to buy xiaomi1 and 1spersonb_xiaomi person2; phone * phone3 = person2.buyphone1 (); phone * phone4 = person2.buyphone1s (); phone3-> phoneinf (); phone4-> phoneinf (); Return 0 ;}
Analysis composition: Same as the factory method mode:

A. Abstract product. (Here is phone), which is implemented by an interface or abstract class in Java. C ++ can be implemented by abstract classes (classes containing pure virtual functions.

B. Specific products. This refers to products produced by mobile phone manufacturers, such as iPhone 1, 1 s, and xiaomi1. Java inherits or implements abstract classes and interfaces to create an actual product instance. Subclass instantiation object in C ++.

C. abstract factory class (here is the secretary). Compared with the simple factory model, the added class frees the secretary from the actual labor and is only responsible for arranging tasks, implemented by an abstract class. But for such a product family, you need to redesign the category class. In this example.

D. Actual factory. It refers to the person who actually buys a mobile phone. Actual class instantiation.


Design Model Learning (1) --- Three factory models

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.