Replacing loong if chain

Hi there! I’m working on a live performance that uses websocket to communicate between my patch and the public via their smartphones.

I have a series of incoming messages of different type and I would like to do different things (namely: call different functions) based on the type of the message. Is there any good pattern for this?

In the previous version I used a loong chain of If statements, but it doesn’t feel right. Server side (node.js) i’ve approached this with Object Literals, using the type of the message as a key and the function as a value. (more here)

Could I do something similar also in gamma? Another way to approach this maybe could be something like the composition pattern, but then again, how to switch dynamically between nodes?

ciao ciao


This sound like StrategyPattern see example here by @sebescudie and the example I made for TextureFX basically shows how to switch between nodes using a simple factory.

Also worth having a look at Reactive nodes to create a pipeline that would process messages as they arrive.

Definitely a sign that some refactoring is necessary but you should provide a patch so we can understand better the situation.

Thanks a lot, the factory example is a perfect starting point!

You can also use a dictionary with the message type as the key.

The value is then the logic that you want to execute specific to each message. it could be a delegate that does something or a class implementing a common interface.

but also, if it works as it is, it works… it might not look great, but sometimes that’s just what it is. :)

The correct solution on the VL side would be a switch statement, which we don’t have yet: