Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visitor pattern: change object state?

Is it acceptable that visitor change the state of object in which it operates? These kind of operation are not considered as anti patterns?

like image 514
user405458 Avatar asked Dec 30 '15 23:12

user405458


People also ask

How does the visitor pattern work?

The Visitor pattern represents an operation to be performed on the elements of an object structure without changing the classes on which it operates. This pattern can be observed in the operation of a taxi company. When a person calls a taxi company (accepting a visitor), the company dispatches a cab to the customer.

What problems can the visitor design pattern solve?

In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying the structures.

What does visit () do in Java?

The accept() method accepts a visitor. It is provided by the Visitable class. The visit() method is called every time when we visit an element. It is implemented by the Visitor class.


1 Answers

It's completely fine to modify the objects that are visited.

Just make sure that your visitor class is named appropriately, so it is obvious that it has a side-effect.

like image 76
Paul Avatar answered Jan 02 '23 23:01

Paul