Factory Method)

Source: Internet
Author: User

The simple factory mode, also known as the staticfactory method, is the simplest of the 23 common Gof induction modes in addition to the single-piece mode. To put it simply, a static method of the factory class is used to create objects. In this way, we can use static methods of the factory class to manage the creation of objects in a unified manner.


The static method determines which specific object to create based on the input string parameter. In this way, the client only needs to know what object the factory needs, the factory will create the corresponding object instance according to the instructions of the client, realizing the separation between the client and the object creation, and unlocking the coupling between the two.

The static factory delays Object Instantiation. Because the static factory determines the Object Instantiation based on the input string, we can determine the specific object created by the factory through the configuration file, in this way, we can modify the code through the configuration file without re-compiling the code.

Let's look at a practical example. For example, we want to create a batch of music boxes, including music boxes that can play violin music and music boxes that play piano music. Using the simple factory mode, we can implement this creation process as follows:


# Include "stdafx. h "# include <string> # include <iostream> # include <memory> using namespace std; // abstract product class IMusicBox {public: virtual void playMusic () = 0 ;}; // product class vilinbox: public IMusicBox {public: void playMusic () {cout <"play violin music" ;}}; class PianoBox: public IMusicBox {public: void playMusic () {cout <"play piano music" ;}}; // factory class MusicBoxFactory {public: // static method of the factory class // This method returns the created object static IMusicBox * createMusicBox (string strName) {IMusicBox * pBox = NULL based on the input string parameter; if ("Violin" = strName) {return new vilinbox ();} else if ("Piano" = strName) {return new PianoBox ();} else {return nullptr ;}}; int _ tmain (int argc, _ TCHAR * argv []) {// create the object shared_ptr pBox (MusicBoxFactory :: createMusicBox ("Violin"); pBox-> playMusic (); // create a new object pBox using the factory. reset (MusicBoxFactory: createMusicBox ("Piano"); pBox-> playMusic (); return 0 ;}# include "stdafx. h "# include <string> # include <iostream> # include <memory> using namespace std; // abstract product class IMusicBox {public: virtual void playMusic () = 0 ;}; // product class vilinbox: public IMusicBox {public: void playMusic () {cout <"play violin music" ;}}; class PianoBox: public IMusicBox {public: void playMusic () {cout <"play piano music" ;}}; // factory class MusicBoxFactory {public: // static method of the factory class // This method returns the created object static IMusicBox * createMusicBox (string strName) {IMusicBox * pBox = NULL based on the input string parameter; if ("Violin" = strName) {return new vilinbox ();} else if ("Piano" = strName) {return new PianoBox ();} else {return nullptr ;}}; int _ tmain (int argc, _ TCHAR * argv []) {// create the object shared_ptr pBox (MusicBoxFactory :: createMusicBox ("Violin"); pBox-> playMusic (); // create a new object pBox using the factory. reset (MusicBoxFactory: createMusicBox ("Piano"); pBox-> playMusic (); return 0 ;}

Here we can also see that the implementation of the simple factory mode is also very simple. First, create an abstract product class, Here IMusicBox, the factory method needs to use this abstract class to represent the product it has created, rather than the product. Then decibels are used to implement specific products, such as ViolinBox and PianoBox, which are the specific objects to be created. Finally, create a factory class MusicBoxFactory and implement a static function. This function accepts a string as the parameter. This string is the client's instruction. This function will create a specific product based on this string.

In this way, the client only needs to tell the simple factory what object it needs to obtain this object instance from the factory. The creation of objects is completed by a simple factory.

Of course, there are still some limitations in a simple factory. For example, it uses a simple comparison of entity strings to determine the objects to be created. When the number of objects created by the factory increases, it will be a very troublesome task. The abstract factory and factory methods summarized later by Gof are upgraded to some form of the simple factory model, which makes up for some shortcomings of the simple factory. Comparing these three modes will help us better understand the three creation modes.

 

 

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.