Java Reference
In-Depth Information
Factory Method
The following example uses the Factory Method pattern to produce an editor for the PIM. The PIM tracks a lot of
information, and there are many cases where users need an editor to create or modify data. The example uses
interfaces to improve the overall flexibility of the system.
The
Editable
interface defines a builder method,
getEditor
, which returns an
ItemEditor
interface. The
benefit is that any item can provide an editor for itself, producing an object that knows what parts of a business
object can change and how they can be changed. The only thing the user interface needs to do is use the
Editable
interface to get an editor.
Example A.21
Editable.java
1. public interface Editable {
2. public ItemEditor getEditor();
3. }
The
ItemEditor
interface provides two methods:
getGUI
and
commitChanges
. The
getGUI
method is another
Factory Method—it returns a
JComponent
that provides a Swing GUI to edit the current item. This makes a very
flexible system; to add a new type of item, the user interface can remain the same, because it only uses the
Editable
and the
ItemEditor
interfaces.
The
JComponent
returned by
getGUI
can have anything in it required to edit the item in the PIM. The user
interface can simply the acquired
JComponent
in its editor window and use the
JComponent
functionality to edit
the item. Since not everything in an application needs to be graphical, it could also be a good idea to include a
getUI
method that would return an
Object
or some other nongraphical interface.
The second method,
commitChanges
, allows the UI to tell the editor that the user wants to finalize the changes he or
she has made.
Example A.22
ItemEditor.java
1. import javax.swing.JComponent;
2. public interface ItemEditor {
3. public JComponent getGUI();
4. public void commitChanges();
5. }
The following code shows the implementation for one of the PIM items,
Contact
. The
Contact
class defines two
attributes: the name of the person and their relationship with the user. These attributes provide a sample of some
of the information, which could be included in an entry in the PIM.
Example A.23
Contact.java
1. import java.awt.GridLayout;
2. import java.io.Serializable;
3. import javax.swing.JComponent;
4. import javax.swing.JLabel;
5. import javax.swing.JPanel;
6. import javax.swing.JTextField;
7.
8. public class Contact implements Editable, Serializable {
9. private String name;
10. private String relationship;
11.
12. public ItemEditor getEditor() {
13. return new ContactEditor();
14. }
15.
16. private class ContactEditor implements ItemEditor, Serializable {
17. private transient JPanel panel;
18. private transient JTextField nameField;
19. private transient JTextField relationField;
20.
21. public JComponent getGUI() {
22. if (panel == null) {
23. panel = new JPanel();
24. nameField = new JTextField(name);
25. relationField = new JTextField(relationship);
26. panel.setLayout(new GridLayout(2,2));
27. panel.add(new JLabel("Name:"));