Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 6
Listing 1. Employee: The Builder Context
1 import java.util.Locale;
2
3 public class Employee
4 { private Name name;
5 private EmployeeId id;
6 private Money salary;
7
8 public interface Exporter
9 { void addName ( String name );
10 void addID ( String id );
11 void addSalary ( String salary );
12 }
13
14 public interface Importer
15 { String provideName();
16 String provideID();
17 String provideSalary();
18 void open();
19 void close();
20 }
21
22 public Employee( Importer builder )
23 { builder.open();
24 this.name = new Name ( builder.provideName() );
25 this.id = new EmployeeId( builder.provideID() );
26 this.salary = new Money ( builder.provideSalary(),
27 new Locale("en", "US") );
28 builder.close();
29 }
30
31 public void export( Exporter builder )
32 { builder.addName ( name.toString() );
33 builder.addID ( id.toString() );
34 builder.addSalary( salary.toString() );
35 }
36
37 //...
38 }
39 //----------------------------------------------------------------------
40 // Unit-test stuff
41 //
42 class Name
43 { private String value;
44 public Name( String value )
45 { this.value = value;
46 }
47 public String toString(){ return value; };
48 }
49
50 class EmployeeId
51 { private String value;
52 public EmployeeId( String value )
53 { this.value = value;
54 }
55 public String toString(){ return value; }
56 }
57
58 class Money
59 { private String value;
60 public Money( String value, Locale location )
61 { this.value = value;
62 }
63 public String toString(){ return value; }
64 }
Let's look at an example. The following code builds Figure 1's UI:
Employee wilma = ...; JComponentExporter uiBuilder = new JComponentExporter(); // Create the builder wilma.export( uiBuilder ); // Build the user interface JComponent userInterface = uiBuilder.getJComponent(); //... someContainer.add( userInterface );
Listing 2 shows the source for the JComponentExporter. As you can see, all the UI-related code is concentrated in the Concrete Builder (the JComponentExporter), and the Context (the Employee) drives the build process without knowing exactly what it's building.

Figure 1. A Builder-constructed output UI
Listing 2. Exporting to a client-side UI
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 class JComponentExporter implements Employee.Exporter
6 { private String name, id, salary;
7
8 public void addName ( String name ){ this.name = name; }
9 public void addID ( String id ){ this.id = id; }
10 public void addSalary( String salary ){ this.salary = salary; }
11
12 JComponent getJComponent()
13 { JComponent panel = new JPanel();
14 panel.setLayout( new GridLayout(3,2) );
15 panel.add( new JLabel("Name: ") );
16 panel.add( new JLabel( name ) );
17 panel.add( new JLabel("Employee ID: ") );
18 panel.add( new JLabel( id ) );
19 panel.add( new JLabel("Salary: ") );
20 panel.add( new JLabel( salary ) );
21 return panel;
22 }
23 }
The JComponentExporter follows the structure of the example in the Gang of Four book, but you can eliminate that getJComponent() call in the earlier example by rewriting the Builder as a JPanel that implements Employee.Exporter. Listing 3's EmployeeUI class does just that. You would use it to create Figure 1's UI as follows:
Archived Discussions (Read only)