c++ - Question about abstract factories and injection -
This is similar to one of my other questions, but I think enough is enough to ask a new question.
Actually I am writing a user interface, and there are nodes in my user interface that can be selected. When a node is selected, the user interface ends with an intangible node base class "INode". From this, I get a factory by making node-> fortifier (), and by this I can make an approot dialog or view for that node because the right factory is able to create concrete node (eg factory-> adidial dylog)> view (node ), e.t.c.).
My question is that the factory must first try to find the best way to enter the node.
So far, I have thought of 3 ways:
1) When I create a node, then inject the right factory:
AreaNode * Node = New Arianode (New AreaAntactor ()); So the definition of field node is: fieldNode: public INOD {Areaode (INodeAbstractFactory * Injectature) {m_injectedFactory = injectedFactory; } INodeAbstractFactory * getFactory () {return m_injectedFactory; } INodeAbstractFactory * m_injectedFactory; }; 2) Inject a more normal factory and allow the factory to factory node from that factory: fieldNode: public INOD {zoneNode (IFT & E * injectfacturing) ) {M_injectedFactory = InjectFactor; } INodeAbstractFactory * getFactory () {return m_injectedFactory-> GetAreaNodeFactory (); } IFactory * M_InjectFactor; ) 3) Make concrete factory (although it probably uses different factories for the same node or removes the scope for later change): Aryanode: Public INOX {INodeAbstractFactory * getFactory () {New Area Nodfector (Return); }}
Current Thoughts on These Options: Option 1: May be a little random - Or maybe I can use another factory just to inject the right factory for me. Option 2: Forces the node to know about intangible factory implementation so that he can call getAreaNodeFactory, such a bad thing
Option 3: It seems very restrictive because I swap in class I will not be able to and I am not keen on the node to know about the concrete implementation of the factory - although in this case it is not very much an issue (the famous last word!).
Any thoughts on this?
Thank you.
Edit: Sorry, variables declarations in original post have not been corrected.
Edit: Another problem with Option 2 I have to implement "getFactory" in each node type. With at least option 1, the base class can return the exit factory class every time.
How to About
Category FactoryBase {/ * Interface Template} & lt; Typename node type & gt; Category Factory: Public FactoryBase {// Default Implementation} // If necessary, add the appropriate specializations for all relevant node texts. Template & lt; Typename node type & gt; Inline Factory & lt; Node type & gt; GetFactory (nodetype *) {Return Factory & lt; Node type & gt; (); } Class AreaNode: Public Node {Factbiz getFactory () {getFactory (get)); }};
Template logging will ensure that the type of factory has been taken from this
manual errors should be prevented, but in general, I would not allow any factory to publicly But it does not bother with coming out. Simply add the following bit to the base class:
class node {public: std :: auto_ptr & lt; View & gt; CreateView () {this-> gt; GetFactory () - & gt; CreateView (this); } // e.t.c. Private: Virtual Factory Base * getFactory () = 0; };
Comments
Post a Comment