Giter Site home page Giter Site logo

Comments (5)

Trismegiste avatar Trismegiste commented on May 12, 2024

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.

JindrichPilar avatar JindrichPilar commented on May 12, 2024

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.

Trismegiste avatar Trismegiste commented on May 12, 2024

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.

Trismegiste avatar Trismegiste commented on May 12, 2024

I've just made a PR between two beach sessions β˜€οΈ

(I'm currently now in Caribbean Sea) 🍸

from designpatternsphp.

JindrichPilar avatar JindrichPilar commented on May 12, 2024

Thanks, your code shows it really nicely.

Have a nice holiday

from designpatternsphp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.