Visitor: How I Mastered the Toughest Programming Pattern

preview_player
Показать описание
Unity C# Architecture: This is how I finally understood the Visitor Programming Pattern - Building a Power Up System in Unity.

#unity3d #gamedev #indiedev

▬ Contents of this video ▬▬▬▬▬▬▬▬▬▬

0:00 Classic Visitor
5:58 Intrusive Visitor
7:13 Reflective Visitor
9:43 GetOrAdd Extension Method

Extension Methods and Utils

*Assets Shown In This Video* (Affiliate Links)

*Follow me!*
Рекомендации по теме
Комментарии
Автор

Hi Everyone! Hope you find this explanation of the Visitor pattern useful - it can help to implement your own Visitor and try it out; you'll see it's not too much code!
Links in description to Extension methods and more!

git-amend
Автор

Thank you so much for making this channel!
I've been developing in Unity for a decade now and after discovering git-amend I found out I was thirsty for more advanced tutorials and I didn't even know.
Please, never stop!

dan.ec
Автор

This channel is so good. I always run into issues when it comes to "mutators" and buffs/debuffs systems. Thats when my technical debt either starts, or comes to collect because of my previous sins.

GettingRektGaming
Автор

I was recommend to this YouTube channel and I’ve got to say, you’ve quickly become one of my favorite coding YouTube channels to watch, great stuff!

trustytea
Автор

To avoid reflection you could just add a type parameter to the visitor interface method:

public interface IVisitor
{
void Visit<T>(T visitable) where T : Component, IVisitable
}

public interface IVisitable
{
void Accept(IVisitor);
}


public MyComponent : MonoBehaviour, IVisitable {
public void Accept(IVisitor visitor)
{
visitor.Visit(this); // Type param is inferred to be MyComponent
}
}


public MyVisitor : MonoBehaviour, IVisitor {
public void Visit<T>(T visitable) where T : Component
{
// use pattern matching etc.
if(visitable is MyComponent myComponent)
{
// do stuff
}
}
}

Now you could actually also create a concrete interface that visits only a specific type of component, and then register those visitor on an aggregating visitor.

public interface IVisitor<T> where T : Component, IVisitable
{
void Visit(T component);
}


public MyAdvancedVisitor : MonoBehaviour, IVisitor {
private Dictionary<Type, object> visitors = new Dictionary<Type, object>();

public void visitor)
{
visitors[typeof(T)] = visitor;
}

public void Visit<T>(T visitable) where T : Component
{
// this is a bit naive, you could easily add support for inheritance
// e.g. a IVisitor<BaseType> should be callable with a DerivedType
if(!visitors.TryGetValue(typeof(T), out var boxedVisitor)
{
return;
}

if(!boxedVisitor is IVisitor<T> concreteVisitor)
{
return; // or throw or log error whatever
}

}
}

Sindrijo
Автор

learned the visitor design pattern in my compilers class, i couldnt wrap my head around it until we finally got to the code gen part. my dopamine spiked so high when i realized what was going on and was able to implement it haha

cit
Автор

Wow that null coalescing operator tidbit at the end was super helpful! Just started using them on a new project because they looked fancy haha. Thanks for the great video!

feildpaint
Автор

Nice walkthrough showing how to apply a general pattern to Unity!

BooleanIndecisive
Автор

This video has been on my recommendation for a week. Before the algorithm changes, I only saw such things only with the biggest channels.

Scott_Stone
Автор

It's funny how part of "object oriented" programming seems to be giving formal names to things we just used to DO in the old days. This is effectively making local dispatch tables with function pointers and varargs argument lists... but fancied up so it's pretty and the compiler can do a little bit more checking that you're using it correctly.

quixadhal
Автор

God bless your channel! One of the best I'v seen on YouTube so far. Please keep making videos like this one, they're really helpful

SabreGoose-we
Автор

Great video & very happy I've found your channel!

Krahkarq
Автор

Brain got bigger, stuff has been learned, you've earned a subscriber. Keep chadding ö7

FidosWideWorld
Автор

The reflection visitor is a really interesting idea that I have not come across yet. However, I think to avoid writing lots of visit functions it would be better to use a base visitor class that has empty functions for each visitable. The visitor interface will still need to grow as more visitable objects are added.

I'm glad to see you're posting about this pattern, everyone on my team was fearful of this pattern and stayed out of the codebase where it was heavily used. But I had to dive into it to add features and fix bugs, it was very challenging at the beginning. I think if you went into more detail about how to debug this pattern that would be great. I think navigating the indirection within the IDE was my big breakthrough.

crazyfox
Автор

I was just thinking about the best way to incorporate collision logic into my game while minimizing the coupling between game objects.
I'm currently using a collision manager that only checks if the game objects are touching.
I came across the Visitor pattern by chance and discovered your channel.
I am in the process of implementing it and it sounds very interesting.

marmont
Автор

Wow i really need to do more research on general programing, all this just went through one ear and out the other without me processing any of it. I've managed just fine only worrying about what I need to know but I think it would be worth knowing more than I need to if I want to a smoother experience.

cheesymcnuggets
Автор

Not bad implementation, but too coupled and uses reflection, which you don't need at all. You already have all the code in there to make it useful, you need more abstraction to make the pattern shine

EskemaGames
Автор

I never knew this was a pattern or that it had a specific name when I implemented a buff/debuff system for my game using classes that "attach" themselves to objects (such as a health system or movement system) and interact with them (reduce max health, increase/decrease move speed etc).

deathtidegame
Автор

I love your channel man! Newly discovered! :)

ragerungames
Автор

Thinking about how to use Visitor pattern in my spaceship game, i think about how the spaceships will take part in the planet's atmosphere by communicating through Visitor Pattern.

pliniojrm