[Zen Reading Notes for design patterns] 008_23 design patterns 2: Factory method patterns

Source: Internet
Author: User

Preface

"Zen of design patterns" saw 78th pages. Continue to stick to it! As a creation-type design model, the factory method model is frequently used. Therefore, the learning of this model is quite valuable. So what is the definition of this mode? What is the implementation of this mode? What is the role of this model? I will do everything possible for readers to explain clearly. Of course, if not, I can discuss it together. This kind of learning is interactive and can make continuous progress! Yuan Fang, what do you think?

Body

1. How did you create an object? (The most stupid method)

1 // 2 myobject Mo; 3 4 // 5 myobject * mo = new myobject ();

Lie down and get shot! Yes, that's how I created the object! What can be used to create and solve the problem? Too stupid. Yes, from the perspective of a software designer, the ugliness is the same as that of the shanzhai tablet and iPad. In fact, for us, what is the most painful way of writing such a statement? No smart notifications! Remember the names of so many classes! So how can we get smart prompts?

2. Create objects with smart prompts (simple factory Mode)

1 class iobject {// base class 2}; 3 class myobject: Public iobject {// implementation Class 4}; 5 class myfactory {// factory class 6 public: 7 static iobject * Createobject (const string & name) {8 If (name = "myobject") {9 return New myobject (); 10} else if (name = "yourobject") {11... 12} else if (name = "hisobject") {13... 14} 15} 16 };

Simply put, we can use a myfactory to encapsulate our creation work, So that I only need to remember the name of this factory-myfactory, so that I can use myfactory :: to obtain the smart prompt creation method. If you are smart, you must have thought of another method. Yes, there is another method. We can split the Createobject method to get better smart prompts. The Code is as follows:

1 class myfactory {2 public: 3 static iobject * createmyobject () {// create myobject. Do not remember that 4 return New myobject (); 5} 6 static iobject * createyourobject () {// create yourobject. Do not remember the 7 return New yourobject (); 8} 9 string... 10 };

That's okay. It looks amazing! If I tell you that you have mastered a design model, do you believe it? Haha, congratulations, you have really learned a design model --Simple factory Mode. How is it really simple! But there is a problem with our simple factory model!

What if we have a new herobject class? Simply put, append an else if in myfactory to determine whether to add a string. For the subsequent implementation, isn't it about adding a method. Big fart! However, we violate the six principles of the design model.Open/closed principle (OCP)Remember? When We append a class, we modify the existing class. What should we do? Next we will improve this simple factory.

3. Improved the simple factory model (Factory method model)

The purpose of modifying the simple factory mode is to prevent the original code from being changed when we add a new factory to produce a new product. See the following code:

1 class iobject {2}; 3 4 class myobject: Public iobject {5}; 6 7... 8 9 class iobjectfactory {10 public: 11 virtual iobject * Createobject () {12} 13}; 14 class myobjectfactory: Public iobjectfactory {// myobject factory 15 public: 16 iobject * Createobject () {17 return New myobject (); 18} 19}; 20 21 class yourobjectfactory: Public iobjectfactory {// yourobject factory 22 public: 23 iobject * Createobject () {24 return New yourobject (); 25} 26}; 27 28 int main () {29 iobjectfactory * factory = new myobjectfactory (); // create a factory class 30 iobject * myobject = factory-> Createobject (); // create a myobject object 31}

Is there any benefit? If I want to append a hisobject class and add a factory for producing hisobject, I only need to append a hisobjectfactory class without damaging the original code. This is ourFactory method mode.

4. Let's look back at the Haha (theoretical UML diagram)

Finally, we can summarize that the above Code implements two design modes: simple factory mode and factory method mode. Of course, I did not use the advanced language features of C ++ in both modes, such as template. However, our goal has been achieved and we know the factory method mode and simple factory mode. The following is a UML diagram of the two modes:

Simple factory Mode:

Factory method mode:

Summary

As a creation-type design model, the factory method mode and simple factory mode are frequently used. Mastering these two design models is of great benefit to designing our own software systems. However, these two design patterns also have disadvantages. Of course, they appear under some extreme conditions. For our software system, we need to select the most suitable mode, that is, there is no best design mode, only the most suitable design mode.

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.