Skip to content
-
Visitor Method Design Patterns in C++
Last Updated :
08 Jan, 2024
A visitor design patterns or visitor method is basically defined as a behavioral design pattern that allows us to define a new operation without changing the classes of the elements on which it operates.

It is particularly useful when we have a set of related classes, and we want to perform different operations on each class without modifying their code.
Important Topics for Visitor Design Patterns in C++
Use Cases of the Visitor Design Patterns in C++ Design Patterns
- Traversal of Composite Structures: When working with composite structures (like trees or graphs), the Visitor Pattern can be used to traverse the structure and perform operations on the elements. This is especially useful when the elements have different types.
- Separation of Concerns: Visitor Pattern helps in separating the concerns of the data structure and the algorithms that operate on it. This separation makes it easier to add new operations without modifying the existing code.
- Double Dispatch: In languages like C++, method overloading is determined at compile-time based on the static type of the object. The Visitor Pattern helps achieve dynamic dispatch or double dispatch, where the appropriate method to be called is determined at runtime based on the actual type of both the visitor and the visited objects.
- Serialization and Deserialization: Visitor Pattern can be used for serialization and deserialization processes, where we define visitors to traverse and convert objects into a format suitable for storage or transmission.
- Implementing Operations Across Classes: When we have a set of classes that are not related through inheritance but need to perform similar operations, the Visitor Pattern can be used to implement those operations in separate visitor classes.
Example for Visitor Design Patterns in C++:
Problem Statement:
Let's consider a simple example of the Visitor Pattern in C++ involving a set of geometric shapes (elements) and a set of operations (visitors) that can be performed on these shapes.
Step wise implementation of Visitor Design Patterns in C++:
Visitor Interface
- Declares a visit method for each concrete element type.
C++
class Visitor {
public:
virtual void visit(ElementA& element) = 0;
virtual void visit(ElementB& element) = 0;
// ... other visit methods for different elements
};
Concrete Visitor
- Implements the visit methods declared in the Visitor interface.
C++
class ConcreteVisitor : public Visitor {
public:
void visit(ElementA& element) override {
// Perform operation on ElementA
}
void visit(ElementB& element) override {
// Perform operation on ElementB
}
// ... implementations for other visit methods
};
Element Interface
- Declares an accept method that takes a visitor as an argument.
C++
class Element {
public:
virtual void accept(Visitor& visitor) = 0;
};
Concrete Element
- Implements the accept method, calling the appropriate visit method on the visitor.
C++
class ElementA : public Element {
public:
void accept(Visitor& visitor) override {
visitor.visit(*this);
}
};
class ElementB : public Element {
public:
void accept(Visitor& visitor) override {
visitor.visit(*this);
}
};
Object Structure
- Represents a collection or structure of elements.
overall
C++
#include <iostream>
#include <vector>
// Forward declarations
class Circle;
class Square;
// Visitor interface
class ShapeVisitor {
public:
virtual void visit(Circle& circle) = 0;
virtual void visit(Square& square) = 0;
};
// Element interface
class Shape {
public:
virtual void accept(ShapeVisitor& visitor) = 0;
};
// Concrete Element: Circle
class Circle : public Shape {
public:
void accept(ShapeVisitor& visitor) override {
visitor.visit(*this);
}
void draw() {
std::cout << "Drawing Circle\n";
}
};
// Concrete Element: Square
class Square : public Shape {
public:
void accept(ShapeVisitor& visitor) override {
visitor.visit(*this);
}
void draw() {
std::cout << "Drawing Square\n";
}
};
// Concrete Visitor: DrawingVisitor
class DrawingVisitor : public ShapeVisitor {
public:
void visit(Circle& circle) override {
std::cout << "Drawing a Circle\n";
circle.draw();
}
void visit(Square& square) override {
std::cout << "Drawing a Square\n";
square.draw();
}
};
// Concrete Visitor: AreaVisitor
class AreaVisitor : public ShapeVisitor {
public:
void visit(Circle& circle) override {
std::cout << "Calculating area of Circle\n";
// Calculate and print area logic for Circle
}
void visit(Square& square) override {
std::cout << "Calculating area of Square\n";
// Calculate and print area logic for Square
}
};
// Object Structure
class ShapeContainer {
public:
void addShape(Shape* shape) {
shapes.push_back(shape);
}
void performOperations(ShapeVisitor& visitor) {
for (Shape* shape : shapes) {
shape->accept(visitor);
}
}
private:
std::vector<Shape*> shapes;
};
int main() {
// Create instances of shapes
Circle circle;
Square square;
// Create a container and add shapes to it
ShapeContainer container;
container.addShape(&circle);
container.addShape(&square);
// Create visitors
DrawingVisitor drawingVisitor;
AreaVisitor areaVisitor;
// Perform drawing operations
container.performOperations(drawingVisitor);
// Perform area calculation operations
container.performOperations(areaVisitor);
return 0;
}
OutputDrawing a Circle
Drawing Circle
Drawing a Square
Drawing Square
Calculating area of Circle
Calculating area of Square
Explanation of example:
- 'Shape' is the element interface, and Circle and Square are concrete elements implementing this interface. Each shape has an 'accept' method that takes a 'ShapeVisitor' as a parameter.
- ShapeVisitor is the visitor interface with visit methods for each type of shape. Concrete visitor classes (DrawingVisitor and AreaVisitor) implement this interface and provide specific implementations for each visit method.
- ShapeContainer represents the object structure, which is a collection of shapes. It has a method performOperations that accepts a ShapeVisitor and iterates through each shape, calling its accept method to perform the corresponding operation.
- In the main function, instances of shapes are created, added to the ShapeContainer, and then drawing and area calculation operations are performed using the DrawingVisitor and AreaVisitor, respectively.
Diagrammatic Representation of Visitor Pattern in C++

- Visitor Interface/Abstract Class: Defines the interface or abstract class with a set of visit methods, each corresponding to a specific type of element in the object structure. These methods typically take the element as a parameter.
- Concrete Visitor: Implements the Visitor interface or extends the abstract class. It provides concrete implementations for each of the visit methods, specifying what actions should be taken when visiting each type of element.
- Element Interface/Abstract Class: Declares the accept method, which accepts a visitor as an argument. This method is implemented by concrete elements and is used to invoke the appropriate visit method on the visitor.
- Concrete Element: Implements the Element interface or extends the abstract class. It defines the accept method by calling the corresponding visit method on the visitor, effectively allowing the visitor to perform operations on the element.
- Object Structure: Represents a collection or structure of elements that can be traversed by the visitor. It provides an interface to traverse its elements, often through an iterator or similar mechanism.
The Visitor Pattern is particularly useful when the object structure is complex, and the algorithm to be applied to its elements is likely to change or be extended. It allows for adding new operations without modifying the existing code for the elements or the object structure.
Advantages of Visitor Design Patterns in C++
- Separation of Concerns: The Visitor pattern helps in separating the concerns of the algorithm from the structure of the objects on which it operates. This promotes a clean and modular design by keeping related operations grouped in the visitor class.
- Open/Closed Principle: The pattern follows the Open/Closed Principle, which states that a class should be open for extension but closed for modification. we can introduce new operations (visitors) without modifying the existing code of the elements being visited.
- Extensibility: It allows us to add new operations to existing classes without modifying those classes. This makes it easy to extend the functionality of a set of classes by adding new visitors.
- Double Dispatch: The Visitor pattern implements a form of double dispatch, where the method to be called is determined at runtime based on the type of both the visitor and the element being visited. This allows for more dynamic and flexible behavior.
- Maintainability: The Visitor pattern makes it easier to maintain and manage code because each operation is encapsulated in a separate visitor class. If we need to make changes or add new functionality, we can do so in the visitor class without affecting the elements being visited.
- Improved Readability: The pattern can lead to improved code readability by centralizing the operations in separate visitor classes. This can make the code more understandable and maintainable, especially when dealing with complex algorithms.
- Visitor Reusability: Visitors can be reused across different element structures. Once we have defined a visitor, we can use it with different sets of elements that implement the same interface, providing a high degree of reusability.
Disadvantages of Visitor Design Patterns in C++
- Increased Complexity: The Visitor pattern can make the code more complex, especially for simple object structures or operations. The need to define separate visitor classes for each operation may lead to an increase in the number of classes and interactions.
- Coupling Between Visitor and Element Classes: Introducing new elements or changing the structure of existing elements can impact all visitor classes, potentially leading to a high degree of coupling between the element and visitor classes.
- Difficulty in Adding New Elements: Adding a new element to the object structure requires modifying all existing visitor classes to accommodate the new element. This violates the Open/Closed Principle, as the system is not closed for modification when new elements are added.
- Violation of Encapsulation: In order to apply the Visitor pattern, the elements being visited must expose their internal structure to the visitor. This can lead to a violation of encapsulation, as the visitor needs access to the internal details of the elements.
- Acceptance Interface Modification: The element classes need to define an "accept" method to allow visitors to visit them. If the set of possible operations (visitors) changes frequently, it may result in modifications to the acceptance interface, affecting all element classes.
- Difficulty in Debugging: The pattern can make the code harder to debug and understand, especially for developers unfamiliar with the Visitor pattern. The logic of an operation is distributed across multiple visitor classes, which may be scattered throughout the codebase.
- Potential Overhead: For simple systems or operations, the Visitor pattern may introduce unnecessary overhead in terms of additional classes and method calls. In such cases, a more straightforward approach might be preferable.
- Learning Curve: Developers who are not familiar with the Visitor pattern may find it challenging to understand and apply. The pattern introduces a specific way of structuring code, and its benefits might not be immediately apparent.
Conclusion
Visitor pattern provides a powerful mechanism for separating algorithms from the objects on which they operate, it should be applied judiciously. It is most beneficial in situations where the benefits of extensibility and separation of concerns outweigh the added complexity and potential drawbacks.
Similar Reads
Modern C++ Design Patterns Tutorial Design patterns in C++ help developers create maintainable, flexible, and understandable code. They encapsulate the expertise and experience of seasoned software architects and developers, making it easier for newer programmers to follow established best practices. What are C++ Design Patterns?A des
7 min read
Creational Software Design Patterns in C++
Factory Method Pattern | C++ Design Patterns Factory Method Pattern provides an interface for creating objects but leaves the actual object instantiation to derived classes. This allows for flexibility in object creation and promotes loose coupling between the client code and the concrete products.Factory Method Pattern | C++ Design PatternsTa
8 min read
Abstract Factory Pattern | C++ Design Patterns Abstract Factory Pattern is a creational design pattern used in object-oriented programming. It provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is a way to encapsulate the creation of objects and ensure that they are
6 min read
Builder Pattern | C++ Design Patterns The builder pattern is defined as a creational design pattern that separates the construction of a complex object from its representation, allowing us to create different representations of an object using the same construction process. It's beneficial when an object has many optional properties or
6 min read
Prototype Pattern | C++ Design Patterns When designing software, it's crucial to make it efficient, easy to reuse, and simple to maintain. One way to achieve these goals is by using design patterns, and one such pattern is the Prototype Pattern. In this article, we'll explore the Prototype Design Pattern in the context of C++. Important T
5 min read
Singleton Pattern | C++ Design Patterns A singleton pattern is a design pattern that ensures that only one instance of a class can exist in the entire program. This means that if you try to create another instance of the class, it will return the same instance that was created earlier. The Singleton pattern is useful when we need to have
11 min read
Structural Software Design Patterns in C++
Adapter Pattern | C++ Design Patterns Adapter Pattern is a structural design pattern used to make two incompatible interfaces work together. It acts as a bridge between two incompatible interfaces, allowing them to collaborate without modifying their source code. This pattern is particularly useful when integrating legacy code or third-
6 min read
Bridge Method | C++ Design Patterns Bridge Pattern is basically a structural design pattern in software engineering or in C++ programming that is used to separate an object's abstraction from its implementation. It is part of the Gang of Four (GoF) design patterns and is particularly useful when we need to avoid a permanent binding be
9 min read
`;
$(commentSectionTemplate).insertBefore(".article--recommended");
}
loadComments();
});
});
function loadComments() {
if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) {
if (!isfollowingApiCall) {
isfollowingApiCall = true;
setTimeout(function(){
if (loginData && loginData.isLoggedIn) {
if (loginData.userName !== $('#followAuthor').val()) {
is_following();
} else {
$('.profileCard-profile-picture').css('background-color', '#E7E7E7');
}
} else {
$('.follow-btn').removeClass('hideIt');
}
}, 3000);
}
}
});
}
$(".accordion-header").click(function() {
var arrowIcon = $(this).find('.bottom-arrow-icon');
arrowIcon.toggleClass('rotate180');
});
});
window.isReportArticle = false;
function report_article(){
if (!loginData || !loginData.isLoggedIn) {
const loginModalButton = $('.login-modal-btn')
if (loginModalButton.length) {
loginModalButton.click();
}
return;
}
if(!window.isReportArticle){
//to add loader
$('.report-loader').addClass('spinner');
jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', {
PRACTICE_API_URL: practiceAPIURL,
PRACTICE_URL:practiceURL
},function(responseTxt, statusTxt, xhr){
if(statusTxt == "error"){
alert("Error: " + xhr.status + ": " + xhr.statusText);
}
});
}else{
window.scrollTo({ top: 0, behavior: 'smooth' });
$("#report_modal_content").show();
}
}
function closeShareModal() {
const shareOption = document.querySelector('[data-gfg-action="share-article"]');
shareOption.classList.remove("hover_share_menu");
let shareModal = document.querySelector(".hover__share-modal-container");
shareModal && shareModal.remove();
}
function openShareModal() {
closeShareModal(); // Remove existing modal if any
let shareModal = document.querySelector(".three_dot_dropdown_share");
shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" }));
document.querySelector(".hover__share-modal-container").append(
Object.assign(document.createElement('div'), { className: "share__modal" }),
);
document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" }));
const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"];
socialOptions.forEach((socialOption) => {
const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" });
const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` });
const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` });
const shareLink = (socialOption === "Copy Link") ?
Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) :
Object.assign(document.createElement('a'), { className: "link-container" });
if (socialOption === "LinkedIn") {
shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`);
shareLink.setAttribute('target', '_blank');
}
if (socialOption === "WhatsApp") {
shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
if (socialOption === "Twitter") {
shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
shareLink.append(icon, socialText);
socialContainer.append(shareLink);
document.querySelector(".share__modal").appendChild(socialContainer);
//adding copy url functionality
if(socialOption === "Copy Link") {
shareLink.addEventListener("click", function() {
var tempInput = document.createElement("input");
tempInput.value = window.location.href;
document.body.appendChild(tempInput);
tempInput.select();
tempInput.setSelectionRange(0, 99999); // For mobile devices
document.execCommand('copy');
document.body.removeChild(tempInput);
this.querySelector(".share__option-text").textContent = "Copied"
})
}
});
// document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu"));
}
function toggleLikeElementVisibility(selector, show) {
document.querySelector(`.${selector}`).style.display = show ? "block" : "none";
}
function closeKebabMenu(){
document.getElementById("myDropdown").classList.toggle("show");
}