O
O
OOPNoob2019-11-07 09:52:48
PHP
OOPNoob, 2019-11-07 09:52:48

Matt Zandstra objects design patterns and methods, Visitor pattern, vulnerability?

Good morning. The book describes the Visitor pattern.
The example shows the implementation as follows,
function accept ( ArmyVisitor $visitor ) {$method = "visit" . get_class( $this ); $visitor->$method( $this)}
Generates a method name and calls it on the fly...
But what happens if we add a new Unit, or rather a new type of unit?
It is clear that when checking the code, this error will immediately fly out.
And the author just teaches that the code should be obvious. And what else at the coding stage, in the same IDE, the message "implement the method" will be displayed, or at the launch stage, And we get no interface that obliges us to implement the "visit" method. And in the end, for example, the code is used once a year, on holidays, we will not implement the visit method, we won’t know about it, and that’s it, fire .. urgent departure for work ....
Maybe you can advise other more “correct” implementations of this template ? Or explain why I'm wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gomonov, 2019-11-07
@Gomonov

Make this method (accept) abstract, that way you will force it to be implemented in every new unit.
The implementation will be like
and your IDE will highlight that visitNewUnit is not in the visitor. After you add the visitNewUnit visitor to the interface, add an implementation for a specific visitor. Everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question