Design mode: Factory mode (cont.: Virtual constructors and abstract factories)

Source: Internet
Author: User

In the previous design mode: Factory mode, two factory patterns were recorded for creating derived class objects, the first using a static member function of the base class to create an object of the derived class, invoking the constructor of the derived class directly in the static member function, and the second pattern using the base class factory Static member function to create a derived class object from each derived class factory saved in the base class factory, which is a nested class of derived classes, equivalent to a proprietary factory tailored for a derived class, where the existence of these exclusive factories makes it unnecessary for the base class factory to know the details of creating derived class objects. Today, two other factory modes are recorded: virtual constructors and abstract factories. in the first two factory modes, the base class is the base class and the derived class is a derived class, while the base class is the base class in the virtual constructor pattern, but the behavior of the base class is the behavior of the derived class, so from the effect, the base class appears to be the derived class. Let's look at a simple example:

Class Base {base *p;protected:base () {p = NULL;} Public:base (const string &str); ~base (); virtual void func () {P->func ();}};
This is the definition of a base class that has a pointer to a base class: P, this pointer will eventually point to a derived class object, notice that the Func () of the derived class is called by P in the member function func () of the base class, so the behavior of the base class is the behavior of the derived class, let's look at base (const String &str) is defined as:

Base::base (const string &str) {if (str== "a") p = new A (), if (str== "B") p = new B ();}
So when we call the above constructor to create a base class object, we point the base class's member pointer p to a derived class object, and then all the behavior of the base class object is the behavior of the derived class, so the base class object looks like a derived class object, which is called a "fictional function". The following is the complete code:

#include <iostream> #include <string>using namespace Std;class Base {base *p;protected:base () {p = NULL;} Public:base (const string &str); ~base (); virtual void func () {P->func ();}}; Class A:public Base {A () {}a (const a&) {}friend class base;public:void func () {cout<< "This was from A" << Endl }};class b:public Base {B () {}b (const b&) {}friend class base;public:void func () {cout<< "This was from B" <& Lt;endl; }}; Base::base (const string &str) {if (str== "a") p = new A (), if (str== "B") p = new B ();} Base::~base () {delete p;} int main () {base *s = new Base ("A"); S->func ();d elete S;return 0;}
It is important to note that there is also a default constructor in base that will be called at the time of creating the derived class object to initialize the base class sub-object in the derived class object, that is, to initialize p, but this p is useless to us, so it is initialized to 0, but there is a benefit to p=0. Because the destructor of the base class is called when the base class object is destroyed, there is a delete p in the destructor, which throws the destructor of the derived class, and then the destructor of the derived class calls the destructor of the base class sub-object, that is, the destructor of the base class is called the second time, the only difference is this p non-P, if this p is not 0 , may cause unknown consequences.

Abstract Factory mode is also very simple, it is intended to have a large number of derived class factories, these derived class factories to create the appropriate object according to their own needs, directly see the code is perhaps more intuitive:

Class Basea {};class a1:public basea{};class a2:public basea{};class baseb {};class b1:public BaseB {};class B2:pu Blic Baseb {};class Factory {public:virtual basea* createa () = 0;virtual baseb* createb () = 0;}; Class Onefactory:public Factory {basea* Createa () {return new A1 (); baseb* Createb () {return new B1 ();}}; Class Twofactory:public Factory {basea* Createa () {return new A2 (); baseb* Createb () {return new B2 ();}};
Factory provides a common interface, onefactory to redefine Createa and createb according to their needs, to generate A1 and B1 objects, and twofactory to redefine Createa and createb according to their needs, To generate the A2 and B2 objects separately. Use the following methods:

int main () {Factory *p1 = new Onefactory ();p 1->createa (); Factory *p2 = new Twofactory ();p 2->createa (); return 0; }






Design mode: Factory mode (cont.: Virtual constructors and abstract factories)

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.