views:

281

answers:

5

I recently started digging into design patterns. Generally speaking, I thought the design issues that most of the design patterns solve do not generally occur in Ruby. Most of the design issues were because of datatypes (No dynamic typing and arrays can hold objects belonging to same class at a time etc. and many more.). Being a ruby and ruby on rails developer, I realized that absence of concepts of interfaces, abstract classes, references, pointers etc and presence of concepts like modules, dynamic typing, array with objects of any class together, features like eval really makes life easier for the developers. I also went through the book by Russ Olsen. I however, thought that the book tried to simulate design patterns instead of actually implementing them, reason being, the design issues just do no generally occur in Ruby. Is it that, the Ruby programming language has evolved with time and takes care of the design issues internally making developers concentrate just on the business logic more?

I would like to know views of other Ruby developers on this. Thanks.

+1  A: 

I think you misunderstand design patterns. It is not true that

"Most of the design issues were because of datatypes."

Instead, design patterns usually solve structural problems of object oriented design. For example, in Ruby, you can have the problem that you have to objects of different classes that must be processed by the same code. When they have different interfaces (that means different sets of methods), you probably need the adapter pattern to solve that incompatibility. Likewise, it could be that you want to implement differnt sets of behavior and will use the strategy pattern to do this.

Some patterns like the Null Object may not be relevant because they are integrated in the language directly and are used out of habit without thinking of them in as patterns.

In general, design patterns apply to Ruby as well as to other object oriented languages.

Sebastian Dietz
You are right. But I had said *Most* of the patterns. I was careful about using the word *most* because there are sure some problems that genuinely occur. What I meant to say was that compared to other OOP languages like Java, C# or even C++, Ruby somehow manages to avoid *some* of the problems
Chirantan
When you need to deal with abstract classes and interfaces in the structure of a design pattern, how would the structure map to ruby components and features is what I wanted to know. Since, abstract classes and interfaces are not really present in Ruby by default.
Chirantan
You are right, it is harder to implement design patterns in languages that lack these features. For example, you can't enforce the implementation of a certain set of methods in strategy classes. You will have to define those interfaces using comments and documentation to make this pattern work.
Sebastian Dietz
So, is it a feature or a drawback? :)
Chirantan
It's mostly just not something that turns out to be a problem once you start working in Ruby...
Mike Woodhouse
Design patterns don't necessarily have to do with OO at all. See definition.
Joe Soul-bringer
+1  A: 

Ruby is a relatively modern language, though not THAT modern, as its been around for about a decade. Because of that many common programming patterns were engineered into the language itself.

On various places, it has been claimed that use of DesignPatterns, especially complex ones like VisitorPattern, are actually indicators that the language being used isn't powerful enough.

Link to Wiki about this topic(the Wiki)

Robert Gould
Thanks Robert, then is it fair to say that Ruby has an edge over other languages where you do need to explicitly implement certain patterns that are not really needed in Ruby? Also, where could the problem may arise when it comes to design while using Ruby?
Chirantan
Well Ruby gives you an edge during programming, however Ruby's runtime performance isn't as good as many of its competitors, so you pay the bill during runtime. As they say there is no such thing as a free lunch
Robert Gould
As for patterns, as Sean McSomething says, there is still stuff missing, some of which is part of RoR, and other which isn't, and even others which probably can't be done easily because the patterns conflict with Ruby's inherent patterns. Can't think of one right now but I'm sure its there.
Robert Gould
Concurrency patterns, there we go. Ruby has a fairly poor concurrency (threading) model when compared with stuff like C# or Java, so it can't implement concurrent patterns as well as languages with solid concurrency built in
Robert Gould
+2  A: 

Considering that the MVC and ActiveRecord Ruby on Rails' core, you've picked a poor example for arguing that design patterns are missing language features.

Sean McSomething
I never meant to say design patters are missing features in a language. I am curious about know how they fit into Ruby when there is absence and presence of features. And since Ruby does not have interfaces and abstract classes, is it that is has evolved so that it does not need them.
Chirantan
Actually, MVC is not Gang-of-four pattern and Martin Fowler has said it is not a pattern in this sense.
Joe Soul-bringer
+1  A: 

Design patterns are simply a set of "ideas" on how to solve some common specific problems. Some of them are useful... some of them trivial, or even contraproductive (look up antipattern :) Generally if the need arise to follow a workflow predefined to a degree comparable to that as stated in most "design patterns", implies one or both of the following:

  1. Language beeing used is flawed, thence solutions to common problems are difficult and require following of rigorous predefined way of thought (design pattern or antipattern), in order to avoid excessive bugs and/or not beeing able to solve the problem at all. Using design anti/patterns as consequence of having chosen a language with poor expressivness often leads to nonsense.

  2. Programmer following design pattern or antipattern lacks the skill, knowledge, creativity or simply IQ to understand, analyze and/or solve the problem, thence he/she chooses to follow pattern or antipattern. Following anti/pattern as result of ones inadequate knowledge, rather than acquiring that knowledge prior to attepting solution, often leads to nonsense.

The ruby language is extremely expressive, and thence solving common tasks, especially those outlined in "design patterns" most often doesn't require explicite usage or following of rigid, predefined workflow aka. design pattern.

+3  A: 

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design.. Naturally, the problems that commonly occur vary from language to language and system to system. Ruby may indeed solve some of the problems which occur in other languages but it will have commonly occurring problems of its own.

Joe Soul-bringer