DEV Community

Binoy Vijayan
Binoy Vijayan

Posted on • Edited on

GoF-Visitor Pattern

The Visitor pattern is a behavioural design pattern that allows you to define new operations on a collection of objects without changing the classes of those objects. It separates algorithms from the objects on which they operate. This pattern is particularly useful when you have a hierarchy of classes and need to perform different operations based on the type of object.

Here's a brief overview of the Visitor pattern:

Intent:

Represent an operation to be performed on the elements of an object structure. It lets you define a new operation without changing the classes of the elements on which it operates.
Key Components:

Visitor Interface:

Defines a visit method for each type of element in the object structure.

Concrete Visitor:

Implements the Visitor interface and provides concrete implementations of the visit methods for each type of element.

Element Interface:

Declares an accept method that accepts a visitor as an argument.

Concrete Element:

Implements the Element interface and provides an implementation of the accept method.

Object Structure:

Represents a collection of elements. It provides a way to traverse the elements and accepts a visitor, which then performs the operation on each element.

Example:

Consider a document structure composed of different types of elements such as paragraphs, tables, and images. You may want to perform operations like spell checking, exporting to different formats, or counting the number of words on each element. Instead of adding these methods to the base class of each element, you can use the Visitor pattern.

You can define a visitor interface with methods like visitParagraph, visitTable, and visitImage. Each concrete visitor provides implementations for these methods.

Each element class implements the accept method, which accepts a visitor as an argument and calls the appropriate visit method based on its type.

When you want to perform an operation on the document structure, you create a concrete visitor and traverse the elements of the document, passing the visitor to each element's accept method. The appropriate visit method is then called based on the element's type.

Benefits:

Allows you to add new operations to existing object structures without modifying their classes.

Encapsulates operations in separate classes, promoting better separation of concerns.

Makes it easier to add new operations to the object structure without modifying existing code.

Drawbacks:

Can make the code more complex by introducing additional classes.

Visitors may have access to private members of element classes, violating encapsulation in some cases.

Here is the sample code in Swift

// Define the Element protocol representing the elements of the document structure.
protocol Element {
    func accept(visitor: Visitor)
}

// Concrete element: Paragraph
class Paragraph: Element {
    func accept(visitor: Visitor) {
        visitor.visit(paragraph: self)
    }
}

// Concrete element: Table
class Table: Element {
    func accept(visitor: Visitor) {
        visitor.visit(table: self)
    }
}

// Concrete element: Image
class Image: Element {
    func accept(visitor: Visitor) {
        visitor.visit(image: self)
    }
}

// Define the Visitor protocol with visit methods for each type of element.
protocol Visitor {
    func visit(paragraph: Paragraph)
    func visit(table: Table)
    func visit(image: Image)
}

// Concrete visitor: SpellChecker
class SpellChecker: Visitor {
    func visit(paragraph: Paragraph) {
        print("Spell checking paragraph...")
        // Perform spell checking on the paragraph
    }

    func visit(table: Table) {
        // Spell checking not applicable to tables
    }

    func visit(image: Image) {
        // Spell checking not applicable to images
    }
}

// Define the ObjectStructure class representing the document structure.
class Document {
    private var elements: [Element] = []

    func addElement(element: Element) {
        elements.append(element)
    }

    func accept(visitor: Visitor) {
        for element in elements {
            element.accept(visitor: visitor)
        }
    }
}

// Example usage:
let document = Document()
document.addElement(element: Paragraph())
document.addElement(element: Table())
document.addElement(element: Image())

let spellChecker = SpellChecker()
document.accept(visitor: spellChecker)

Enter fullscreen mode Exit fullscreen mode

In this Swift implementation:

We define the Element protocol to represent the elements of the document structure (e.g., Paragraph, Table, Image).

Each concrete element class (Paragraph, Table, Image) implements the accept method to accept a visitor.

We define the Visitor protocol with visit methods for each type of element.

The SpellChecker class is a concrete visitor that implements the visit methods to perform spell checking on paragraphs.

The Document class represents the document structure and maintains a collection of elements. It provides the accept method to traverse the elements and apply the visitor.

Finally, we create a document, add elements to it, and apply the SpellChecker visitor to perform spell checking on paragraphs.

Image description

Usage

Document Parsing: Consider a document processing application where you have a complex document structure consisting of paragraphs, headings, images, etc. You can use the Visitor pattern to traverse the document structure and perform various operations such as validation, transformation, or extracting specific information.

GUI Components: In a graphical user interface (GUI) framework, you might have a hierarchy of GUI components such as buttons, panels, and text fields. You can use the Visitor pattern to traverse the component hierarchy and perform operations such as rendering, event handling, or layout calculations.

Compiler or Interpreter: When building a compiler or interpreter for a programming language, you often need to traverse the abstract syntax tree (AST) representing the code and perform various operations such as type checking, optimisation, or code generation. The Visitor pattern can be used to separate these operations from the AST nodes.

File System Analysis: Suppose you're developing a file system analysis tool that needs to traverse the directory structure and perform operations such as calculating the total size of files, identifying duplicate files, or generating reports. The Visitor pattern can help in separating the traversal logic from the operations performed on the files and directories.

Network Packet Processing: In networking applications, you may need to process network packets received from different protocols such as TCP, UDP, or HTTP. Each protocol may have its own structure and processing logic. By using the Visitor pattern, you can define visitors for each protocol to process the packets accordingly.

Data Serialisation/Deserialisation: When serialising or deserialising complex data structures, you may need to traverse the data structure and convert it to/from a specific format. The Visitor pattern can be used to separate the traversal logic from the serialisation/deserialisation logic, making it easier to support different formats or data structures.

Game Development: In game development, you might have a scene graph representing the game world with different types of objects such as characters, obstacles, or collectibles. You can use the Visitor pattern to traverse the scene graph and perform operations such as collision detection, AI behavior, or rendering.

Database Querying: When querying a database with a complex schema, you may need to traverse the query results and perform operations such as aggregation, filtering, or mapping to domain objects. The Visitor pattern can help in separating the query processing logic from the domain-specific operations.

These examples demonstrate how the Visitor pattern can be applied in various domains to separate the logic for traversing complex data structures from the operations performed on the elements of those structures.

Summary

Overall, the Visitor pattern is a powerful tool for separating algorithms from the objects on which they operate, providing a clean and extensible way to add new operations to existing object structures.

Overview of GoF Design Patterns

Top comments (0)