Looking back at the previous article, we implemented a simple factory mode to create different classes of objects. However, because c ++ does not have syntax similar to new "Circle", the CreateShape Function
Ifelse needs to constantly judge the number. If multiple different class objects need to be created, it is obviously very difficult. The following uses the macro-defined registration method to achieve dynamic creation.
Object.
Shape. h:
# Ifndef _ SHAPE_H _
# Define _ SHAPE_H _
Class Shape
{
Public:
Virtual void Draw () = 0;
Virtual ~ Shape (){}
};
Class Circle: public Shape
{
Public:
Void Draw ();
~ Circle ();
};
Class Square: public Shape
{
Public:
Void Draw ();
~ Square ();
};
Class Rectangle: public Shape
{
Public:
Void Draw ();
~ Rectangle ();
};
# Endif/_ SHAPE_H _
Shape. cpp:
# Include "Shape. h"
# Include "DynBase. h"
# Include <iostream>
Using namespace std;
Void Circle: Draw ()
{
Cout <"Circle: Draw ()..." <endl;
}
Circle ::~ Circle ()
{
Cout <"~ Circle... "<endl;
}
Void Square: Draw ()
{
Cout <"Square: Draw ()..." <endl;
}
Square ::~ Square ()
{
Cout <"~ Square... "<endl;
}
Void Rectangle: Draw ()
{
Cout <"Rectangle: Draw ()..." <endl;
}
Rectangle ::~ Rectangle ()
{
Cout <"~ Rectangle... "<endl;
}
REGISTER_CLASS (Circle );
REGISTER_CLASS (Square );
REGISTER_CLASS (Rectangle );
DynBase. h:
# Ifndef _ DYN_BASE_H _
# Define _ DYN_BASE_H _
# Include <map>
# Include <string>
Using namespace std;
Typedef void * (* CREATE_FUNC )();
Class DynObjectFactory
{
Public:
Static void * CreateObject (const string & name)
{
Map <string, CREATE_FUNC>: const_iterator it;
It = mapCls _. find (name );
If (it = mapCls _. end ())
Return 0;
Else
Return it-> second (); // func ();
}
Static void Register (const string & name, CREATE_FUNC func)
{
MapCls _ [name] = func;
}
Private:
Static map <string, CREATE_FUNC> mapCls _;
};
// G ++
// _ Attribute (weak ))
_ Declspec (selectany) map <string, CREATE_FUNC> DynObjectFactory: mapCls _;
// The header file is contained multiple times, and only mapCls _ is defined once _;
Class Register
{
Public:
Register (const string & name, CREATE_FUNC func)
{
DynObjectFactory: Register (name, func );
}
};
# Define REGISTER_CLASS (class_name )\
Class class_name # Register {\
Public :\
Static void * NewInstance ()\
{\
Return new class_name ;\
}\
Private :\
Static Register reg _;\
};\
Register class_name # Register: reg _ (# class_name, class_name # Register: NewInstance)
// CircleRegister
# Endif // _ DYN_BASE_H _
DynTest. cpp:
# Include "Shape. h"
# Include "DynBase. h"
# Include <iostream>
# Include <vector>
# Include <string>
Using namespace std;
Void DrawAllShapes (const vector <Shape *> & v)
{
Vector <Shape *>: const_iterator it;
For (it = v. begin (); it! = V. end (); ++ it)
{
(* It)-> Draw ();
}
}
Void DeleteAllShapes (const vector <Shape *> & v)
{
Vector <Shape *>: const_iterator it;
For (it = v. begin (); it! = V. end (); ++ it)
{
Delete (* it );
}
}
Int main (void)
{
Vector <Shape *> v;
Shape * ps;
Ps = static_cast <Shape *> (DynObjectFactory: CreateObject ("Circle "));
V. push_back (ps );
Ps = static_cast <Shape *> (DynObjectFactory: CreateObject ("Square "));
V. push_back (ps );
Ps = static_cast <Shape *> (DynObjectFactory: CreateObject ("Rectangle "));
V. push_back (ps );
DrawAllShapes (v );
DeleteAllShapes (v );
Return 0;
}
In DynBase. h # define a macro definition REGISTER_CLASS (class_name), and call the macro definition in Shape. cpp, using REGISTER_CLASS (Circle );
For example, the program compilation preprocessing phase will be replaced:
Class CircleRegister {
Public:
Static void * NewInstance ()
{
Return newCircle;
}
Private:
Static Register reg _;
};
Register CircleRegister: reg _ ("Circle", CircleRegister: NewInstance );
That is to say, a new class is defined, and because it contains static members, Initialization is performed before the main function is executed, the Register class constructor is called, and the class constructor is called in the constructor.
DynObjectFactory: Register (name, func); that is, the static member function of the DynObjectFactory class is called. In the Register function, the map container is used to complete
Register a string with a function pointer, for example, mapCls _ [name] = func;
Go to the main function and call DynObjectFactory: CreateObject ("Circle"). Find the corresponding function pointer through string in the CreateObject function.
(NewInstance), and return the created object pointer after the call. Note that return it-> second (); in it-> second is the function pointer, which is
Number indicates that this function is called. If you are unfamiliar with #, # In macro definition, refer to here.
In this way, when you need to create multiple different class objects, you no longer need to write a lot of ifelse judgments.