Design Mode: factory mode (continued: Virtual constructor and abstract factory)

Source: Internet
Author: User

Design Mode: factory mode (continued: Virtual constructor and abstract factory)

In the previous design mode: factory mode, two factory modes are recorded for creating a derived class object. The first mode directly uses the static member function of the base class to create the object of the derived class, this static member function directly calls the constructor of the derived class. The second mode is to use the static member function of the base class factory, create a derived class object through the factory of the derived classes stored in the base class factory. The factory of the derived class is the nested class of the derived class, which is equivalent to the exclusive factory customized for the derived class, the existence of these exclusive factories makes it unnecessary for the base class factory to understand the details of creating a derived class object. Today, we mainly record two other factory models: Virtual constructor and abstract factory. The virtual constructor mode is different from the first two factory modes. In the first two factory modes, the base class is the base class, and the derived class is the derived class. In the virtual constructor mode, although the base class is a base class, the behavior of the base class is the behavior of the derived class. Therefore, the base class seems to be a derived class in terms of its effect. Here is 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 the base class. The base class has a base class pointer: p, which will eventually point to a derived class object. Note that in the base class member function func, p calls the func () of the derived class, so the behavior of the Base class is the behavior of the derived class. Let's take a look at the definition of Base (const string & str:

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, and point the base class member pointer p to a derived class object, next, all the behaviors of this base class object are derived class behaviors, so the base class object looks like a derived class object, which is called a "virtual constructor ". The complete code is as follows:

#include
 
  #include
  
   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 is from A"<
   
    func();delete s;return 0; }
   
  
 
Note that there is also a default constructor without parameters in the Base. This constructor will be called when the object of the derived class is created to initialize the Base class sub-objects in the object of the derived class, initialize p, but this p is useless for us, so it is initialized to 0, but p = 0 has another benefit, when a base class object is destroyed, the base class destructor is called, and delete p is included in the destructor, which triggers the destructor of the derived class, the destructor of the derived class then calls the destructor of the base class sub-object, that is, the second time the base class's destructor is called. The only difference is that the p is not the p, if this p is not 0, unknown consequences may occur.

The abstract factory mode is also very simple. It is intended to have a lot of factory classes for derived classes. These factory classes create suitable objects based on their own needs, and the code may be more intuitive:

class BaseA {};class A1 : public BaseA{};class A2 : public BaseA{};class BaseB {};class B1 : public BaseB {};class B2 : public 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 public interfaces. OneFactory re-Defines createA and createB as needed to generate A1 and B1 objects. TwoFactory also redefined createA and createB as needed, to generate A2 and B2 objects respectively. The usage is as follows:

int main() { Factory *p1 = new OneFactory();p1->createA();Factory *p2 = new TwoFactory();p2->createA();return 0; }






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.