Wednesday, March 22, 2006

OO Patterns and Functional Paradigm

Regarding my earlier post on design patterns and programming languages, someone in an LtU thread has the following observation
My problem with this paper is that they are using functional languages to implement OO patterns. What's the point? Document the patterns that programmers use when doing functional programming. That's more interesting.

The main point that I was trying to drive home in the post is that implementation of any design pattern needs to consider the best practices and idioms of the language that you are using. A Command pattern implemented in C++ will not be the same as one implemented in Ruby. Though the end result is to provide an abstraction to the commands executed, the language artifacts used in the above two cases will be different. In case of C++ or Java, which do not provide higher order functions, closures etc., the implementation will be through a class hierarchy manifesting as "Command Objects". But in case of a Ruby implementation, it should be all "Closures".

The above is more important from the very fact that modern day languages provide you options - in C# you can choose to use the classical OO model of polymorphic class hierarchies or you can also use the anonymous classes and delegates offered by 2.0 or the classical lambdas coming forth in 3.0. We need not necessarily have to implement OO patterns using OO artifacts ONLY - in some cases functional paradigms may very well be a better fit!

No comments: