Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of design patterns  RSS feed

 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'd like to ask what is your recommended approach to using design patterns. Shall a programmer always start with looking for a appropriate design pattern when solving some problem, or should it emerge from his initial effort to find the solution? I suppose these decisions come easier with experience, but what would you suggest to new developers? Especially when they are not that familiar with real world pattern usage. Also, are there any design patterns (I mean the original from GoF), that are outdated, or that you would not recommend to use? I often hear this for example about Singleton pattern (though I've never really understood the "why").

Thanks.
Tomas
 
Jack Dwaltz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Linhart wrote:
I'd like to ask what is your recommended approach to using design patterns. Shall a programmer always start with looking for a appropriate design pattern when solving some problem, or should it emerge from his initial effort to find the solution?


Well sometimes people tend to abuse of patterns, and I would suggest to study GoF patterns outside of Java EE, writing your own small sample programs to test if you got the point, and the benefits. Don't just copy and paste a sample to test it.
Later you can try with Java EE specific patterns: search Java EE blueprints, or have a look at EAI patterns and Martin Fowler book: Pattern of Entrerprise Application Architecture, and the text presented here of course.

Tomas Linhart wrote:pattern usage. Also, are there any design patterns (I mean the original from GoF), that are outdated, or that you would not recommend to use? I often hear this for example about Singleton pattern (though I've never really understood the "why").

Some think the singleton is broken, I don't, and Java EE even has a @Singleton annotation for services that starts a single instance (beware, one in each server you start of you run a cluster).
You can think that dependency injection (CDI in Java EE) is a replacement for the factory o builder patterns, but in general any pattern has it uses and times you can do without, and sometimes you use them not knowing that you do (see e.g. Java IO patterns).
Learn to use them by doing some test programs and reading.
 
Alex Theedom
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tomas,

I'd like to ask what is your recommended approach to using design patterns. Shall a programmer always start with looking for a appropriate design pattern when solving some problem, or should it emerge from his initial effort to find the solution?


To use design patterns well requires a good knowledge of what problem they solve and the pitfalls of using a particular pattern. This knowledge can be obtained by studying the GoF patterns. There are some good books available that illustrate the implementation of the design pattern in Java, for this you might want to look the HeadFirst series, for a Java EE specific implementation you might want to have a read of Professional Java EE Design Patterns. In this book we show how traditional implementations of common design patterns have been advanced by the use of EE specific features, namely annotations. As mentioned by the previous poster.

I suppose these decisions come easier with experience, but what would you suggest to new developers? Especially when they are not that familiar with real world pattern usage.


Experience makes everything easier. But in the meantime you should learn the structure and specific implementation (ie boiler plate code), search the internet for examples, build a personal library of examples that you can refer to (I use evernote to keep notes of things like this) and in our book (mentioned above) we tell a 'war story' of how each pattern saved the day or make life worse, showing the real life use of design pattens.

Also, are there any design patterns (I mean the original from GoF), that are outdated, or that you would not recommend to use? I often hear this for example about Singleton pattern (though I've never really understood the "why").


Remember why patterns exist: they solve know and common problems, and as long as those problems continue the pattern will still have value. There has been a lot of talk about the Singleton pattern, some love it why others hate it. I suggest that you learn the value and limitations that each patter has. This knowledge will help you decide whether or not the pattern is right for the given problem this will also help you avoid making the mistake of mindlessly using a pattern just because it convention or expected that you use it in a given situation. Think before you use it.

Why dont people like the Singleton pattern. Well, many reason are given, too many list here. We discuss it in depth in our book but here an interesting post: so-singletons-are-bad-then-what.


 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for guidance and valuable links. The Head First book (together with Object Analysis and Design) is on my list. I was in doubt about reading the original GoF book, as it's from 1998, but you are right that they provide solutions to common problems that stay relevant over time.
 
Alex Theedom
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The GoF book is very good but a bit heavy going. The Head First book, or our book ;-), are far more accessible full of lots of examples for Java.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the Head First serie attracts me in general, but I'm not really sure the teaching style is what I like the most (at least I'm used to learn the different way). But I definitelly want to give it a try. And your book has the big advantage that it applies the examples and aims at Java EE, which is where I'm heading and what is my long-range goal.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!