Design Pattern learning: Factory method pattern and prototype Pattern

Source: Internet
Author: User

Raise the question-analyze the problem-solve the problem. This is the general idea of writing argumentative papers. This is exactly what we usually write in this article. So learning a theory also follows this idea, the learning design model is no exception. Before learning any mode, you must first understand the background of this mode, that is, problems encountered in the actual coding or modification and maintenance code, and then analyze the problems, whether it violates the design pattern principle or the object-oriented ideology, then leads to the design pattern used to solve this problem, and finally compares and summarizes the applicable occasions and advantages and disadvantages of this pattern.

1. Factory method mode
Definition: defines an interface used to create objects so that the subclass determines which class to instantiate. The factory method delays the instantiation of a class to its subclass.

Simple factory mode vs factory method mode: The biggest advantage of the simple factory mode is that the factory class contains the necessary logic judgment, and the related classes are dynamically instantiated Based on the client selection conditions. For the client, remove dependencies with specific products. In the previous simple factory calculator example, you only need to give '+' to the factory, and the factory automatically provides the corresponding instance. The client only needs to perform the operation, different instances implement different operations. But the problem is that if you want to add a function, you need to add the 'case' branch condition to the factory class method, so that it is not only open to the extension, but also open to the modification, it violates the open-closed principle. Since the factory class is coupled with the branch class, we can start with it and abstract the factory class into an interface based on the Dependency inversion principle. This interface has only one method, it is to create the factory method of the abstract product, and then implement this interface for the factory managers of all production specific classes. In this way, you do not need to change the original factory class after converting to the factory method mode, you only need to add the operation class and the corresponding factory class of this function. In this way, only the scaling changes and no modification changes will conform to the open-closed and closed principle.

When the factory method mode is implemented, the client needs to decide which factory to instantiate to implement the computing class, and choose whether the problem still exists, just to move the internal logic judgment of the simple factory to the client, if you want to add a function, you would have changed the factory class, but now you want to modify the client.


# Include <string> # include <iostream> using namespace STD; // instance base class Leifeng {public: Virtual void sweep () {cout <"" <Endl ;}; // a college student studying Lei Feng, equivalent to concreteproductclass Student: Public Leifeng {public: Virtual void sweep () {cout <"" <Endl ;}; // volunteers from lei, equivalent to concreteproductclass volenter: Public Leifeng {public: Virtual void sweep () {cout <"" <Endl ;}; // workshop base class creatorclass leifengfactory {public: Virtual Leifeng * createleifeng () {return New Leifeng () ;}}; // workshop class studentfactory: Public leifengfactory {public: Virtual Leifeng * createleifeng () {return new student ();}}; class volenterfactory: Public leifengfactory {public: Virtual Leifeng * createleifeng () {return New volenter () ;}}; // client int main () {leifengfactory * Sf = new leifengfactory (); Leifeng * s = SF-> createleifeng (); s-> sweep (); Delete s; Delete SF; return 0 ;}

Advantage: The Factory method mode further abstracts and promotes the simple factory mode. Due to the use of polymorphism, the factory method mode maintains the advantages of the simple factory mode to encapsulate the object creation process, and follow the principle of openness and closure. The disadvantage of the factory method mode is that each time a product is added, a product factory class needs to be added to increase the additional development workload.

2. prototype mode
Definition: create another custom object from an object without having to know the details of the creation and improve the creation performance. To put it bluntly, copy an object completely


# Include <iostream> # include <vector> # include <string> using namespace STD; Class prototype // abstract base class {PRIVATE: String m_strname; public: Prototype (string strname) {m_strname = strname;} prototype () {m_strname = "";} void show () {cout <m_strname <Endl;} Virtual Prototype * clone () = 0 ;}; // class concreteprototype1class concreteprototype1: Public prototype {public: concreteprototype1 (string strname): Prototype (strname) {} concreteprototype1 () {} Virtual Prototype * clone () {concreteprototype1 * P = new concreteprototype1 (); * P = * This; // copy the object return P ;}}; // class concreteprototype2class concreteprototype2: Public prototype {public: concreteprototype2 (string strname): Prototype (strname) {} concreteprototype2 () {} Virtual Prototype * clone () {concreteprototype2 * P = new concreteprototype2 (); * P = * this; // copy the object return P ;}}; // client int main () {concreteprototype1 * test = new concreteprototype1 (""); concreteprototype2 * Test2 = (concreteprototype2 *) test-> clone (); test-> show (); Test2-> show (); Return 0 ;}

Application Scenario: If you want to create an object repeatedly, You need to execute the constructor once every new time. If the constructor takes a long time, so it is too inefficient to execute this initialization multiple times. Generally, when the initialization information does not change, cloning is the best method. This not only hides the details of object creation, but also greatly improves the performance without re-initializing the object, instead, it dynamically obtains the running state of the object.


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: 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.