Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the visitor modify the object it visits

When using the Visitor Pattern, can the visit(object) method update or modify the object it visits or is it simply supposed to only use the object in performing some computation and returning the computation result?

Thanks

like image 678
david jefferson Avatar asked Sep 16 '17 23:09

david jefferson


People also ask

What does the visitor pattern do?

The purpose of a Visitor pattern is to define a new operation without introducing the modifications to an existing object structure. Imagine that we have a composite object which consists of components.

When should you use the visitor pattern?

2 Answers. Show activity on this post. The visitor pattern is useful when you want to process a data structure containing different kinds of objects, and you want to perform a specific operation on each of them, depending on its type.

What is visitor pattern in Java?

Visitor is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code.


Video Answer


2 Answers

All examples of the visitor pattern I've seen don't change the objects visited.

But as far as I understand the definition given on Wikipedia, it's just not defined what the visitor can do with the visited objects.

Even the definition from Gang of Four (also in the wiki article) doesn't touch that subject:

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

Therefore I'd say that a visitor may call any accessible method on an object it visits as this is not part of the visitor pattern - the visitor pattern just describes how this objects are made accessible.

like image 103
sruetti Avatar answered Sep 22 '22 15:09

sruetti


In the following example the mailer is the "element" and the "visitor" is the lambda expression which is passed to send().

As you can see here (and in many other examples) the visitor can modify the element's internal state. That said, changing the element's internal state is not a requirement: it could be that the visitor will call some of the element's methods in order to do all sorts of actions: pass a message to another object, run a computation and print to screen the result and etc.

import java.util.function.Consumer;

public class Main {

    public static void main(String[] args) {
        Mailer.send(mailer ->
            mailer.from("[email protected]")
                    .to("[email protected]")
                    .body("what's up bud?")
        );
    }
}


class Mailer {

    String fromStr;
    String toStr;
    String bodyStr;

    public Mailer from(String from) {
        this.fromStr = from;
        return this;
    }

    public Mailer to(String to) {
        this.toStr = to;
        return this;
    }

    public Mailer body(String body) {
        this.bodyStr = body;
        return this;
    }

    public static void send(Consumer<Mailer> loader) {
        Mailer mailer = new Mailer();
        loader.accept(mailer);
        System.out.println(mailer);
        // ... send the email
    }

    @Override
    public String toString() {
        return "From: " + fromStr + "\n" +
                "To: " + toStr + "\n" +
                "Body: " + bodyStr;
    }
}
like image 21
Nir Alfasi Avatar answered Sep 25 '22 15:09

Nir Alfasi