Flattening the hierarchy to navigate through them
The visitor implementation has to have some idea of the structure of the composite. In some instances of composite implementation, there will be scores of visitors to be implemented. Moreover, applying transformations and filters on nodes is a bit difficult in the case of visitor interfaces. The GOF pattern catalog has an iterator pattern that can be used to navigate a sequence of items. The problem is: How can we linearize a hierarchy for processing using the iterator pattern? Most hierarchies can be flattened to a list, sequence, or stream by writing a visitor implementation for that purpose. Let us write a flattening visitor for the said task.
Take a look at the following code:
// Flatten the File/Folders into a linear list class FlattenVisitor : public IFileFolderVisitor{ list <FileInformation> files; string CurrDir; public: FlattenVisitor() { CurrDir = "";} ~FlattenVisitor() { files.clear();} list<FileInformation...