r/programming • • Dec 08 '13

Design Pattern Cheat Sheet

http://www.celinio.net/techblog/wp-content/uploads/2009/09/designpatterns1.jpg
1.7k Upvotes

273 comments sorted by

View all comments

3

u/[deleted] Dec 09 '13 edited Dec 09 '13

I usually find Design Patterns materials frivolous/unnecessary, but I really liked this one for being direct to the point!

The "idea" is the part I think can be relevant. The diagrams are OOP-dependent, but the text can be implemented in any paradigm/way, sometimes in a very simple way:

  • 'Visitor' - just a function map

  • 'Factory' - type inferring can be used, in most cases, as a default "built-in" factory.

6

u/[deleted] Dec 09 '13

[removed] — view removed comment

5

u/Peaker Dec 09 '13

The Visitor Pattern gives you a cumbersome, but type safe encoding of a closed sum type, in a language that has products (tuples/class fields) and exponents (functions) but doesn't have closed sum types.

How would a function map give you type safety around the sum type encoding?

2

u/[deleted] Dec 09 '13

Indeed! Correcting myself, the visitor pattern is a clunky/hack to implement an inferior-and-limited version of map.

Looking in a better light, the visitor pattern is an idea/example in how to put a map function to good use!

2

u/seruus Dec 09 '13

[a] pattern is a clunky/hack to implement an inferior-and-limited version of [a more expressive construction].

Seems to be the gist of it. I remember someone talking about how most design patterns are basically structured ways to make up for the deficiencies of the language you are using.

4

u/Peaker Dec 09 '13

Patterns are exactly workarounds for language deficiencies. That said, Visitor isn't a "function map" but a closed sum type.

3

u/Peaker Dec 09 '13

Visitor is a cumbersome encoding of a sum type on top of exponentials and products, in languages that only have exponentials and products.