3 ms·
Tell them it's like the command pattern in OO.
by edflsafoiewq 7y ago
Tell them it's like the command pattern in OO.
- bogdanoff_2 7y agoThat's an interesting comparison. On one hand, it feels similar, especially if the Command class is "sealed". On the other hand, it feels like completely the opposite since the command classes contain the code they execute and act like lambdas. I guess you could apply a visitor pattern on top of the command pattern to make it truly like defunctionalization. I hope nobody actually does this.
- lidHanteyk 7y agoYou're just describing an AST interpreter. Not only do people do this, but it's a reasonable thing to do.
- choeger 7y agoIn fact, the command pattern is the opposite in a certain way. Defunctionalization corresponds to deeply embedded DSLs, whereas the command pattern corresponds to shallowly embedded DSLs. In a proper functional language, you do not need objects for the latter, as you can directly compose functions. That being said, the ability to add more operations than just "execute" to your commands allows you to emulate the features of deep embedding (e.g., serialization). But you have to extend every command for a single new operation (read up on the expression problem). That style is known as tagless in the FPL community, btw.
- richard_todd 7y agoIt's really more like the Interpreter pattern (another one of the original GoF patterns)[0]. Instead of doing something directly, you create a description of it, and then provide an interpreter to do the work elsewhere in the program. That leaves you free to serialize/optimize/test/etc. the description prior to or instead of actually executing the work. [0]: https://en.wikipedia.org/wiki/Interpreter_pattern https://en.wikipedia.org/wiki/Interpreter_pattern