On Thu, Feb 8, 2018 at 12:17 AM, Rob Pike <r...@golang.org> wrote:

> Isn't the visitor pattern just a way to implement type switch in languages
> that don't implement type switch?
>
> That's certainly how I saw it when using C++. Go has a type switch, and so
> has no need for the visitor pattern, a perfect example of the principle
> that a design pattern is just a way to work around shortcomings in the
> language.
>

That is likely the origin of the visitor pattern, but that is not its only
use. JVM languages can switch on type, yet visitors are still prevalent in
several libraries there. The value above-and-beyond a type switch only
applies to OO languages (i.e. class-based inheritance):
re-using/specializing code by sub-classing a visitor implementation and
only overriding/decorating subset of methods.

Also, visitor patterns are sometimes used to abstract away the mechanics of
the tree traversal, too (such as in the "go/ast" package's Walk function).

And they can provide API symmetry for code that both consumes and produces
a data structure. So you implement the visitor to consume; you accept a
visitor and invoke its methods to produce. (For example, the ASM
<http://download.forge.objectweb.org/asm/asm4-guide.pdf> library for Java.)


>
> -rob
>
>
> On Thu, Feb 8, 2018 at 2:14 PM, Josh Humphries <jh...@bluegosling.com>
> wrote:
>
>> FWIW, it looks like someone else has gone through this exercise:
>> https://github.com/tmrts/go-patterns
>>
>> ----
>> *Josh Humphries*
>> jh...@bluegosling.com
>>
>> On Fri, Feb 2, 2018 at 12:03 PM, <matthewju...@gmail.com> wrote:
>>
>>> I’m looking at patterns summarized on Wikipedia from “Design Patterns:
>>> Elements of Reusable Object-Oriented Software” and writing out a few as the
>>> equivalent in Go.
>>>
>>> Visitor: https://play.golang.org/p/A5tNzxMmetH
>>>
>>> Abstract Factory: https://play.golang.org/p/SWwuX49eysd
>>>
>>> Factory Method: https://play.golang.org/p/FRgDBx2CLFf
>>>
>>> Facade: https://play.golang.org/p/forPdwy9VCi
>>>
>>> Proxy: https://play.golang.org/p/DFWuDPTOzEP
>>>
>>> I’m curious how more experienced people rank these and the other
>>> patterns.
>>>
>>> Matt
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to golang-nuts+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to