DEV Community

Discussion on: The Visitor Pattern in PHP

Collapse
 
doekenorg profile image
Doeke Norg • Edited

I don't see why 🤷

Because every visitor has a different reason for being, so they all will have different results. There is no single return type or return value type.

TL;DR makin small interfaces & traits gives you the flexibility to only implement what you need 😎

This will however replace one VisitorInterface with several new interfaces. Which doesn't make much sense to me either. Because now my Book can only receive a BookPageCountVisitorInterface which, by its name only serves one purpose. It needs to have the generic VisitorInterface to be able to receive any visitor.

And those trait BookPageCountVisitor traits will never be reused, because they serve the purpose of one visitor. So they might as well live on that visitor only, right?

I personally would rather have one interface that has a function for every type, and create an abstract base class that implements all those functions with an empty body. Then there would be less overhead in files, and my visitors only need to overwrite the functions that matter.