This seems like one of my other questions, but in a different way I think to ask a new question.
I mainly write the user interface, and my user interface has nodes that can be selected. When node is selected, the user interface ends with the abstract base class node "INode". From this, I get the factory by executing node โ getFactory (), and from this I can create the appropriate dialogs or views for this node, because the correct factory is returned by the concrete node (e.g. factory โ createAddDialog (), factory โ createView (node) and so on .d.).
My question is trying to find the best way for this factory to break into node first.
So far I have been thinking of three ways:
1) Add the correct factory when I create the node:
AreaNode *node = new AreaNode(new AreaNodeFactory());
Thus, the definition of AreaNode:
AreaNode : public INode { AreaNode(INodeAbstractFactory *injectedFactory) { m_injectedFactory = injectedFactory; } INodeAbstractFactory* getFactory() { return m_injectedFactory; } INodeAbstractFactory* m_injectedFactory; };
2) Introduce a more general factory and let node get a factory from this factory:
AreaNode : public INode { AreaNode(IFactory *injectedFactory) { m_injectedFactory = injectedFactory; } INodeAbstractFactory* getFactory() { return m_injectedFactory->getAreaNodeFactory(); } IFactory* m_injectedFactory; }
3) Just create a specific factory (although this removes the scope for using different factories for the same node, perhaps for testing or for subsequent changes):
AreaNode : public INode { INodeAbstractFactory* getFactory() { return new AreaNodeFactory(); } }
Current thoughts on these parameters:
Option 1: maybe a little random - I would need to make sure that I always give it the correct factory for this type, or maybe I can just use another factory to enter the correct factory for me.
Option 2: Makes node aware of the abstract factory implementation enough to be able to call getAreaNodeFactory, which might not be so bad. This at least helps to ensure that the correct / same factory will always be checked out (provided that the more general factory is implemented correctly).
Option 3: This limits me a bit since I cannot change the class, and I'm not interested in node to know about a specific factory implementation - although this is the case, this may not be a big problem (famous words!).
Any thoughts on this?
Thanks.
EDIT: Sorry, missed variable declarations in the origin column, fixed.
EDIT: Another issue with option 2 - I need to implement "getFactory" in each node type. At least with option 1, the base class can simply return a nested abstract factory class each time.