Factory mode of design mode: Simulate DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC to dynamically create class objects

Source: Internet
Author: User

The factory model in this form is a method used in my project. It is a very mature template and can be used directly by readers in their own projects. I personally feel that this method truly implements the "open and closed" principle. The biggest benefit is that the design of the user product class is completely independent of the implementation of this mode, such as providing necessary functions. If you do not understand the factory model, please refer to other articles on the Internet. This implementation is a little difficult to understand. Good things for everyone to enjoy. If you don't talk much about it, put the code first!

First, it is the product base class. It is equivalent to an interface. Let's write the actions required by the product here!

#ifndef _CPRODUCTBASE_H_#define _CPRODUCTBASE_H_enum PRODUCT_TYPE{PT_NOKIA=0, PT_APPLE, PT_GOOGLE};class CProductBase{public:CProductBase(){}~CProductBase(){}public:virtual void doSomething()=0;};#endif 

Second, the factory class. This class is not very easy to understand. First of all, we need to understand that CreateByType creates a factory unit based on the product type, and a product is created by the factory unit. The factory unit is saved in the map.

#ifndef _DYNAMICFACTORY_H_#define _DYNAMICFACTORY_H_#include #include "CProductBase.h"using namespace std;class CDynamicFactory{protected:CDynamicFactory(){ m_mapType2Factor.clear(); }public:class CFactoryUnitBase{public:virtual CProductBase* BuildProduct()=0;};typedef std::map
  
       MAP_TYPE_UNIT;typedef std::map
   
    ::iterator MAP_TYPE_UNIT_ITERATOR;static CDynamicFactory& GetInstance(){static CDynamicFactory nodeFactory;return nodeFactory;}CProductBase* CreateByType(PRODUCT_TYPE emType){CFactoryUnitBase*pFactoryUnit(NULL);CProductBase*pProduct(NULL);MAP_TYPE_UNIT_ITERATOR it = m_mapType2Factor.find(emType);if (it != m_mapType2Factor.end()){pFactoryUnit= it->second;pProduct= pFactoryUnit->BuildProduct();}return pProduct;}void RegisterFactory(PRODUCT_TYPE emType, CFactoryUnitBase* pFactoryUnit){m_mapType2Factor[emType] = pFactoryUnit;}private:MAP_TYPE_UNIT m_mapType2Factor;};#endif
   
  
The following is a dynamically created macro definition. Why does map have a factory for the corresponding type of product before the program runs, and static class_name: FactoryUnit _ factory (class_name: ProductType ); it defines a Global static variable, and the name can be retrieved at will, because the initialization is performed at compilation (right, isn't it too sure ?), Therefore, the FactoryUnit constructor is called and then the RegisterFactory In the constructor is executed.

#ifndef _PRODUCTCREATOR_H_#define _PRODUCTCREATOR_H_#include "DynamicFactory.h"#define DECLARE_DYNAMIC() \public:\static const PRODUCT_TYPE ProductType;\class FactoryUnit : public CDynamicFactory::CFactoryUnitBase\{\public:\FactoryUnit(PRODUCT_TYPE emType)\{\CDynamicFactory::GetInstance().RegisterFactory(emType,this);\}\virtual CProductBase* BuildProduct();\};#define IMPLEMENT_DYNAMIC(class_name, emType) \const PRODUCT_TYPE class_name::ProductType = emType; \static class_name::FactoryUnit __factory(class_name::ProductType);\CProductBase* class_name::FactoryUnit::BuildProduct()\{\class_name* pProduct = new class_name;\return pProduct;\}#endif

Next, we will add the test program.

#ifndef _CGOOGLE_H_#define _CGOOGLE_H_#include "DynamicFactory.h"#include "ProductCreator.h"#include "CProductBase.h"class CGoogle : public CProductBase{DECLARE_DYNAMIC()public:virtual void doSomething();};#endif
# Include "CGoogle. h" # include
 
  
Using std: cout; using std: endl; IMPLEMENT_DYNAMIC (CGoogle, PT_GOOGLE); void CGoogle: doSomething () {cout <"In Google" <
  
   
#ifndef _CNOKIA_H_#define _CNOKIA_H_#include "DynamicFactory.h"#include "ProductCreator.h"#include "CProductBase.h"class CNokia: public CProductBase{DECLARE_DYNAMIC()public:virtual void doSomething();};#endif
# Include "CNokia. h" # include
    
     
Using std: cout; using std: endl; IMPLEMENT_DYNAMIC (CNokia, PT_NOKIA); void CNokia: doSomething () {cout <"In Nokia" <
     
      
Main function.
      

# Include "DynamicFactory. h "# include" ProductCreator. h "# include" CGoogle. h "# include" CNokia. h "int main () {CNokia * p = (CNokia *) CDynamicFactory: GetInstance (). createByType (PT_NOKIA); p-> doSomething (); delete p; // note that p = NULL; return 0 ;}



Note: When the product object is added to BuildProduct () in this mode, you must remember to delete it when the program is used up. This is the only thing that I think the implementation is poor and relies on user behavior. If you have any good ideas, please let me know!


The following is a factory model written by another Web user using the dynamic creation method. For more information, see:
Http://blog.csdn.net/happyanger6/article/details/7277638


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.