What would be considered good examples of implementing the builder pattern when used in the development of a GUI?
Asked Answered
M

4

6

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.

Mitman answered 28/8, 2010 at 23:19 Comment(0)
T
4

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.

Turoff answered 29/8, 2010 at 2:11 Comment(2)
Thanks trashgod, I've up voted and accepted this as the answer due to the excellent Dr.Dobbs article.Mitman
BTW, Item 2 is excerpted from Joshua Bloch's Effective Java, 2nd ed. java.sun.com/docs/books/effectiveTuroff
P
6

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.

Pippin answered 29/8, 2010 at 3:55 Comment(2)
One sees something similar in NetBeans' GUI editor with GroupLayout: stackoverflow.com/questions/2561540Turoff
Cay Horstmann has a great example of a GBC builder like this too... horstmann.com/articles/GBC.javaJeannettajeannette
T
4

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.

Turoff answered 29/8, 2010 at 2:11 Comment(2)
Thanks trashgod, I've up voted and accepted this as the answer due to the excellent Dr.Dobbs article.Mitman
BTW, Item 2 is excerpted from Joshua Bloch's Effective Java, 2nd ed. java.sun.com/docs/books/effectiveTuroff
A
3

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

Agnola answered 29/8, 2010 at 2:8 Comment(2)
I initially dismissed this, but the builder discussion includes nice before and after examples in several languages: sourcemaking.com/design_patterns/builderTuroff
+1 The source making book is IMHO much more enjoyable to read than the GoF book.Conveyancing
S
2

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

Swec answered 29/8, 2010 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.