Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which GoF Design pattern will be changed or influenced by the introduction of lambdas in Java8?

Many claims that the biggest part of the GoF design patterns are just workarounds for the absence of first class functions. Now that Java is about to get lambda expressions, which of those patterns will be influenced by them? Which ones can be dramatically simplified or generalized? And which ones will basically remain the same? Any practical example is welcome.

like image 826
Mario Fusco Avatar asked Jun 09 '13 17:06

Mario Fusco


People also ask

Does functional programming replace GoF design patterns?

So yes, several GoF design patterns are rendered redundant in FP languages, because more powerful and easier to use alternatives exist.

What are the major categories of GoF design pattern?

The GoF Design Patterns are broken into three categories: Creational Patterns for the creation of objects; Structural Patterns to provide relationship between objects; and finally, Behavioral Patterns to help define how objects interact.

What does GoF stand for in the context of software design patterns )?

More than a decade ago by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides known as the Gang of Four (GoF) published their seminal book "Design Patterns: Elements of Reusable Object-Oriented Software".


2 Answers

I think your will see the most changes in Behavioral patterns.

Template Method - Template methods will be more and more seldomly used, and instead we will see objects pass functions to the AbstractTemplate instead of subclassing the AbstractTemplate. I blogged about this a loooong time ago here: http://hamletdarcy.blogspot.ch/2007/11/groovy-closures-end-of-template-method.html

Observer Pattern - Observer becomes simplified because you no longer need to keep a list of observers that get updated on new events, but instead keep a list of functions that need to be called back on new events. So there is no more Observer interface and just function objects.

State/Strategy Pattern - I group these together because they are structurally equivalent, just different in intent. Strategy usage become much more common because it is easier to implement. You don't need a parent strategy and strategy subclasses, you just need functions. So it is simple to just pass a function as a parameter, which in effect is using the strategy pattern.

Overall, I think any pattern that requires a one-method interface becomes easier to implement. This will have the two effects. 1) We will use these functional patterns more, and 2) we will stop referring to them as patterns but just as "passing a function".

You do what you want, but I think "JavaScript the Good Parts" gives a pretty nice introduction to leveraging functions in a language. You might pick it up and read it!

like image 135
Jen S. Avatar answered Sep 30 '22 23:09

Jen S.


I tried to reply to this question myself writing a series of articles where I analyzed some GoF pattern and their functional counterpart with practical code examples. In particular I revisited: Command and Strategy, Template and Observer, Decorator and Chain of Responsibility, Interpreter and Visitor.

like image 34
Mario Fusco Avatar answered Sep 30 '22 23:09

Mario Fusco