views:

131

answers:

4

Hi Coders,

I am a complete newbie when it comes to the use of factory classes and methods, patterns, etc - in fact I first learned of them here on Stackoverflow when browsing Java related questions :-)

In response to a previous question of mine it was suggested that I look into the use of the Builder Pattern in the development of my GUI's and so I am seeking good easily understood examples demonstrating how an application's user interface could be put toghether using this pattern and method-chaining, etc.

Thanks for reading.

+3  A: 

I think "Source Making" does a nice job of introducing design patterns (as well as UML, Antipatterns and Refactoring). You may want to check the site out.

You can read about the Builder here: Source Making: Builder Design Pattern

S.Jones
I initially dismissed this, but the builder discussion includes nice _before_ and _after_ examples in several languages: http://sourcemaking.com/design_patterns/builder
trashgod
+1 The source making book is IMHO much more enjoyable to read than the GoF book.
Helper Method
Hi S.Jones - Great link, thanks for sharing :-)
The Thing
+4  A: 

Joshua Bloch's Item 2: Consider a builder is always a good place to start. Regarding GUI development, many layout managers use the builder pattern. A Visual Guide to Layout Managers is a good introduction.

trashgod
Thanks trashgod, I've up voted and accepted this as the answer due to the excellent Dr.Dobbs article.
The Thing
BTW, Item 2 is excerpted from Joshua Bloch's _Effective Java_, 2nd ed. http://java.sun.com/docs/books/effective/
trashgod
+2  A: 

There are probably other (and better) examples but here is one.

When working with GridBagConstraints, one could use this horrible constructor:

public GridBagConstraints(int gridx, int gridy,
                          int gridwidth, int gridheight,
                          double weightx, double weighty,
                          int anchor, int fill,
                          Insets insets, int ipadx, int ipady) 

But I consider it unusable. And people most often end up using the empty constructor and setting the various public attributes to override the defaults values.

As an alternative, one could use a builder, something like this:

somePanel.add(
    getContent(),
    new ConstraintsBuilder()
        .gridLocation(1, 1)
        .gridSize(1, 1)
        .weight(0.0, 0.0)
        .anchor(NORTHWEST)
        .build() );

Just an example.

Pascal Thivent
One sees something similar in NetBeans' GUI editor with `GroupLayout`: http://stackoverflow.com/questions/2561540
trashgod
+2  A: 

Here is good BuilderPattern example related to building UI. (There is not explanation but easy to understand if you know Builder Pattern)

http://www.java2s.com/Code/Java/Design-Pattern/BuilderPatternExample.htm

Builder Pattern more information :

http://www.allapplabs.com/java_design_patterns/builder_pattern.htm

http://www.java2s.com/Code/Java/Design-Pattern/BuilderPatterninJava.htm

YoK