I have an object called Binary that contain array of references which are just strings, those references after a process of resolving will result another binary objects and of course those binaries could contain also their references and so on.
As it appears, I am dealing with composite pattern (a composite binary that contains another composite binary or leaf binary ).
However, i am confused how to build this composite in a cleaner way, i have the below method that takes a binary which as entry point and it performs a deep first traversal as following :
[NotNull] public Binary ResolveReferences([NotNull] Binary entryPointBinary)
{
var compositeBuilder = new CompositeBuilder();
compositeBuilder.AddBinary(entryPointBinary);
stack.Push(entryPointBinary);
while (stack.Any())
{
stack.TryPop(out var currentBinary);
if (!visited.Contains(currentBinary)) visited.Add(currentBinary);
if (currentBinary.References != null)
{
foreach (var reference in currentBinary.References)
{
var resolvedBinary = GetResolvedReference(reference);
if (!visited.Contains(resolved))
stack.Push(resolved);
compositeBuilder.AddBinary(currentBinary, resolved);
}
}
}
return compositeBuilder.GetResult();
As you can see, this method is too far from respecting single responsibility principal, it performs resolving of references and building the composite and iterating with DFS on binaries.
What I tried to do :
In order to clean this method, I thought, I would use iterator to perform DFS over binaries and visitor to resolve references of each dependency. Hence, I could decouple resolving logic and use another visitor to build composite of each binary which consists of a binary (the root) and a composition which is binaries result of resolving references.
You can use Visitor along with Iterator to traverse a complex data structure and execute some operation over its elements, even if they all have different classes.
However, I found that this is not corresponding to visitor pattern as the below quote states
If you are applying the same operation to each element, or all the elements are of the same type, you don't need a visitor. You need a visitor when you want to select different operations depending on the type of element.
My questions are :
Is the resolving process of each reference of a given binary applicable to visitor pattern which will be responsible of resolving a references and call builder to build corresponding binary ?
Should I adopt iterator to decouple code performing DFS from the business logic code?
Aucun commentaire:
Enregistrer un commentaire