I have different visitors for specific types of objects. I have a problem implementing a general interface which can be used for all types. What is the best architecture to use in this situation? I've came up with 3 different solutions, but all of them look pretty ugly to me: (some things like virtual destructors are dropped for simplicity)
class IObject {
virtual void Accept(IVisitor& visior) = 0;
};
class Text: IObject {
void Accept(IVisitor& visitor) {
visitor.Visit(*this);
}
};
class Image: IObject {
void Accept(IVisitor& visitor) {
visitor.Visit(*this);
}
};
class IVisitor {
virtual void Visit(Text& text) = 0;
virtual void Visit(Image& image) = 0;
};
class TextVisitor: IVisitor {
void Visit(Text& text) {
// Do some stuff with text
}
void Visit(Image& image) {
// Image not supported, throw exception
}
};
OR
class IObject {};
class Text: IObject {};
class Image: IObject {};
class IVisitor {
virtual void Visit(IObject& object) = 0;
};
class TextVisitor: IVisitor {
void Visit(IObject& object) {
Text& text = dynamic_cast<Text&>(object);
// Do some stuff with text
}
};
OR
template <typename T>
class IVisitor {
virtual void Visit(T& object) = 0;
};
class TextVisitor: IVisitor<Text> {
void Visit(Text& text) {
// Do some stuff
}
};
class ImageVisitor: IVisitor<Image> {
void Visit(Image& image) {
// Do some stuff
}
};
class ITextImagelVisitor: IVisitor<Text>, IVisitor<Image> {};
class VisitorDispatcher: ITextImageVisitor {
void Visit(Text& text) {
text_visitor_->Visit(text);
}
void Visit(Image& image) {
image_visitor_->Visit(image);
}
std::shared_ptr<IVisitor<Text>> text_visitor_;
std::shared_ptr<IVisitor<Image>> image_visitor_;
};
class IObject {
virtual void Accept(ITextImageVisitor& visior) = 0;
};
class Text: IObject {
void Accept(ITextImageVisitor& visitor) {
visitor.Visit(*this);
}
};
class Image: IObject {
void Accept(ITextImageVisitor& visitor) {
visitor.Visit(*this);
}
};