File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Scala and the fly likes Design Patterns with Scala Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "Design Patterns with Scala" Watch "Design Patterns with Scala" New topic
Author

Design Patterns with Scala

Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402

Hi Venket?

What kind of design patters are available with Scala?

Thanks
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

I think this is really an interesting question!

Are there already approved patterns or best practices available for Scala which were really tested extensivelly for real world projects? I don't even know if users of pure functional languages are really concerned (or have to be concerned) about design patterns as much as it's the case for users of OO languages. But I guess he interesting mix of OO and functional programming in Scala gives the possibility to discover even new patterns or best practices. Anyway, I d'like to hear about already existing ones, too

Marco
Edwin Buck
Greenhorn

Joined: Aug 13, 2009
Posts: 1
Any design pattern can be implemented in Scala. Design patterns are not specific to a language, although the best ways of implementing them might depend on the particular constructs available within the language. In some languages, certain design patterns are not needed at all; because, the language has built in solutions to the common problems the pattern was meant to address.

For example, you would never need a common implementation of the Singleton design pattern in Scala; because, Scala does not lack the feature of guaranteeing that only one object of a class exists in memory. With Scala, the language itself supports a singleton concept through the use of the keyword "object". It would be somewhat silly to document the problem, and it's common solutions when the common solution is just to use the correct keyword in the language.

Think of this, there's no "print line" design pattern in most languages because most languages already provide a simple means of directing a line of text to the screen. But if you were programming in assembly, where no single command will do the job, then a design pattern for "print line" might become useful.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Edwing,

thanks for your input! Of course I know that it should be possible to use most patterns equally in programming languages which are different but have enough similarities to apply a specific pattern (if we're talking only about design patterns at the "code level"). But my question wasn't very precise As you may have guessed I'd be more interested in patterns which are more specific to Scala. Some ideas about patterns which should be handled differently in Scala would be interesting as well.

But it intersting enough that you pointed out that some patterns are just kind of workarounds for inconveniences of a particular programming language or not really necessary in other languages

Marco
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
In almost all cases where one would use the Command/Strategy pattern, you would probably use a higher order function in Scala.

Likewise, for most cases where one would use the Visitor pattern, you would probably use pattern matching in Scala.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Venkat Subramaniam
Author
Ranch Hand

Joined: Jan 28, 2008
Posts: 79
The patterns we're used to in Java can be implemented in Scala with mostly fewer lines of code.
Edwin mentioned about Singleton. In another post I mentioned about decorator pattern using traits.

In addition to these common or Gang-of-four patterns, as you start working with Scala, you will find
yourself using some additional patterns that make use of function values (and closures). One example
is the execute-around-method pattern or a variation of it called the loan pattern. The idiomatic
differences will lead you to explore some different patterns than you're used to in Java.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Thanks a lot!

That's exactly the kind of information I asked for. I'm wondering if there are already any good, comprehensive lists with more of these Scala patterns (or functional programming patterns) available?!?

Marco
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I don't think there is any one single resource for this, but here are a few blog postings you can peruse:

Scala's pattern matching
Design Patterns in Scala
Typesafe Builder Pattern in Scala
Venkat Subramaniam
Author
Ranch Hand

Joined: Jan 28, 2008
Posts: 79
In addition you may also want to look at the Smalltalk Design Patterns.
Some of the patterns that make use of function values can be used with Scala as well.
Refer to Kent Beck's book on Smalltalk Best Practice Patterns (you can find a list of
the patterns in the book with a quick search on the web).
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Thank you both for the valuable input! Looks like very interesting information

Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design Patterns with Scala