开发者

C++ pattern to prohibit instantiation of a class outside a certain scope?

I have a System class that can return a pointer to an Editor class. The Editor class is instantiated within the System class and passed pointers to System's private variables. The Editor class essentially acts as an alternative interface to System's internal data structures.

My question: Does a design pattern exist that allows me to prohibit the direct instantiation of the Editor class but somehow still instant开发者_开发问答iate it inside System?

Thanks.


You could make Editor's constructor private which would keep others from instantiating it and then making System a friend will allow it to access the constructor.

class System {
public:
    System() : editor_(new Editor()) { ... }

private:
    Editor* editor_;
}

class Editor {
    friend class System;
    Editor() { ... }
}


You can create an abstract interface for your Editor, and nest the implementation inside the definition of System. It can even be protected or private.

class IEditor
{
public:
   virtual int Whatever() = 0;  
};

class System
{
public:
  int foo;
  IEditor *GetEditor() { return &m_Editor; }

protected:
  class Editor 
  {
     virtual int Whatever() { return 1; }
     // etc...
  }

  Editor m_Editor;
}


If you don't want to make nested classes, you could make class Editor a friend of System, and make the constructors and destructors of Editor private. That way only System is allowed to instantiate and destroy an instance of class Editor.


Consider making the copy constructor and the copy assignment private along with the constructor. The example below shows that editor1 and editor2 can be created if these two methods are not made private.

class Editor {  
private:  
    Editor();  
    friend class System;  
};  

class System {  
public:  
    Editor editor;  
};  

int main() {  
    System system;  
    Editor editor1(system.editor);  
    Editor editor2 = system.editor;  
    return 0;  
}  


Why not split the System's state into another class? For example:

class SystemState {
};

class Editor {
    public:
        Editor(SystemState &state) :
            state(state) {
        }

    private:
        SystemState &state;
};

class System {
    public:
        System() :
            editor(new Editor(state)) {
        }

    private:
        SystemState state;

        Editor *editor;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜