public class Factory
{
public INode CreateNode(TypeEnum type)
{
switch(type)
{
case TypeEnum.Document:
return new DocumentNode(this);
case TypeEnum.Item:
return new ItemNode();
}
}
}
public interface INode
{
void DoAction();
}
public class ItemNode : INode
{
void DoAction()
{
// Nothing to do or simple action without more childs.
}
}
public class DocumentNode : INode
{
private Factory factory;
public Node(Factory factory)
{
this.factory = factory;
}
public void DoAction()
{
var nodeTypes = this.GetNodeTypes();
foreach (var value in nodeTypes)
{
var node = this.factory.CreateObject(value);
node.DoAction(otherValues);
}
}
private IEnumerable<TypeEnum> GetNodeTypes()
{
// Retrieve values here... Could be DocumentNode or ItemNode types.
}
}
È un approccio valido e ha senso iniettare la factory utilizzata per creare un'istanza di classe nell'istanza creata stessa per usarla (come un albero)?