This week I was being interviewed for some of my upcoming talks, when the interviewer asked about limiting our use of design patterns in code, linking specificially to this post by Jim Bird, where part of his post says:
Don’t use patterns unless you need to
My response is simple: You can’t not use patterns. Because patterns aren’t something we use. They are something inherent in the problem domain. Here’s an example:
print "Hello, World!"
The code above is the world’s smallest implementation of the Model-View-Controller pattern. How?
- The Model above is the literal string “Hello, World!”
- The View is whatever is going to have that string printed on. Likely a terminal, but perhaps a log file or a digital display
- The Controller is the application that statement is contained within.
However, we’ve become so skewed in our thinking about patterns that we can’t imagine that as an implementation of MVC without it looking something like:
class Response def to_s 'Hello, World!' end end class TerminalWriter def print(str) puts str end end class TerminalWriterController def write_to_terminal model = Response.new view = TerminalWriter.new view.print(model.to_s) end end
So if there’s one piece of advice I’d share, it would be to stop thinking of patterns as traditional recipes that look a certain way, and start understanding them as a solution hiding inside the problem.
For more information, see my talk on “Thinking in Patterns”