程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++ 類工廠實現動態對象創建

C++ 類工廠實現動態對象創建

編輯:C++入門知識

C++ 類工廠實現動態對象創建


 看了MFC中的動態創建對象後,感覺動態創建對象算是一種技術吧,理論上說算是一種設計模式。其實創建的原理很明了,把對象的類別名稱和其構造函數用容器記錄下來,在需要的時候給出創建類別查找容器即可創建對象。當然這裡我們需要自定義一個全局的函數指針用來指向類別的構造函數。在這裡我們使用類工廠模式,在淚工廠中我們定義一個通用構造函數,將函數指針指向它。下面是參考一位網友思路來整理和摸索的代碼:   //通用的函數指針     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_;
};

//頭文件被包含多次,也只定義一次mapCls_;
__declspec(selectany) map<string, CREATE_FUNC> DynObjectFactory::mapCls_;


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   //下面是測試類 *.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(); }; //*.cpp 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);   //main.cpp 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; }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved