Comments (5)
You're absolutely right, I concur. This pattern is incomplete : accept() method is a mandatory part of visitor pattern.
Since PHP can't use polymorphism based on type-hint, another missing part is a list of visitXXX methods for each concrete elements. To simulate double-dispatch, one could use a get_called_class in "accept()" method.
A complete example for java http://en.wikipedia.org/wiki/Visitor_pattern#Diagram
to simulate type-hint polymorphism :
public function accept(RoleVisitor $visitor)
{
call_user_func(array($visitor, 'visit' . get_called_class()), $this)
}
that's the idea. Warning, this code fails with namespaced classname... More there's no check or whatsoever.
from designpatternsphp.
Wouldn't be better to pass the class name as parameter to visit
method and let the Visitor
decide what to do?
Calling visitXXX will force Visitor to know all Role subclasses.
public function accept(RoleVisitor $visitor)
{
call_user_func(array($visitor, 'visit'), $this, get_called_class());
}
public function visit(Role $role, $className)
{
switch($className) {
case: 'AdminRole':
// for more complicated task call another method
echo "Role: <b>" . $role->getName() . "</b>;
break;
default:
echo "Role: " . $role->getName();
break;
}
}
from designpatternsphp.
Sorry but I don't think so.
For SRP concern, Visitor doesn't have to "decide" what to do regarding the real type of object. It knows what to extract from one visited object but no more. It's not a problem if Role knows all methods of RoleVisitor, it is designed like this (to show that Liskov Substitution Principle is "handled with care" ).
Therefore, in C++ & Java, a subclass of Car, for example, will be visited by visit(Car car) not by a default implementation for an abstract Element.
And since PHP does not use type-hint polymoprhism for methods, there is always the workaround for a subclass of Car to call visitCar() anyway by overriding accept() method.
Furthermore, this kind of big switch you propose tends to be un-maintainable for future changes.
If you're not comfortable with the dynamic call (that's understandable), you can simply let the visitee decide which method to call like this example does :
http://sourcemaking.com/design_patterns/visitor/php#
Anyway, with good check and exception, I think dynamic calls will run smoothly.
I'm not sure if I'm clear, it's difficult to explain this problem. If you give me some times (one or two days) I can show you the implementation I see.
from designpatternsphp.
I've just made a PR between two beach sessions βοΈ
(I'm currently now in Caribbean Sea) πΈ
from designpatternsphp.
Thanks, your code shows it really nicely.
Have a nice holiday
from designpatternsphp.
Related Issues (20)
- Broken documentation url in readme HOT 3
- CircuitBreaker pattern HOT 1
- Bulgarian Translation HOT 4
- Hello γWhen I run phpunit, this error occurs. How can I solve it? Thank you HOT 2
- AbstractFactory - how does it know which WriterFactory to return HOT 1
- Design Patters PHP HOT 1
- composer install - FAIL HOT 2
- Translation error in readthedocs HOT 3
- use standard coding style from PHP Code Sniffer HOT 3
- Translate the Structural pages into Spanish
- Incomplete translation to Brazilian Portuguese HOT 1
- Observer pattern is unclear as implementation of update is not clear
- strategy pattern php HOT 1
- undefined class TestCase. HOT 1
- Help with Docker HOT 2
- Class diagram doesn't match the code. Pattern "State" HOT 1
- Project marked as spam HOT 2
- How to translate languages from other countries
- language issue
- *
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from designpatternsphp.