I was going through the "Modern C++ Design" book, saw below code explained in "8.1 The Need for Object Factories", I have some doubts
- Are we not moving the problem of object creation from Document to DocumentManager class. I mean we have to create concert DocumentManager?
- How to keep track of all the derived classes of DocumentManager, created by many independent clients?
Should not the listOfDocs_ be static and DocumentManager be a singleton.
My main doubt is, is that a solution at all or I am missing some point. According to the book "CreateDocument()" is the GoF book's factory method. But I am not able to understand the above points.
DocumentManager from the book,
class DocumentManager
{
...
public:
Document* NewDocument();
private:
virtual Document* CreateDocument() = 0;
std::list<Document*> listOfDocs_;
};
Document* DocumentManager::NewDocument()
{
Document* pDoc = CreateDocument();
listOfDocs_.push_back(pDoc);
...
return pDoc;
}
Client code:
Document* GraphicDocumentManager::CreateDocument()
{
return new GraphicDocument;
}
Aucun commentaire:
Enregistrer un commentaire