© Kelt Dockins 2017

Kelt Dockins, Design Patterns in PHP and Laravel, 10.1007/978-1-4842-2451-9_26

26. Visitor

Kelt Dockins

(1)Dolph, Arkansas, USA

$> git checkout visitor

Intent

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

Applications

This is probably one of the most complicated GoF patterns I’ve seen. The first time I stared at the UML diagram after a while I said, “hmm, wat 2?” At its basic form, this pattern is all about extracting methods from semi-related or unrelated classes. When you do this, you no longer have to change or revisit (pun intended) the code. After reading more about this pattern I ...

Get Design Patterns in PHP and Laravel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.