Factory Method Pattern in Java : Factory Pattern « Design Pattern « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Factory Pattern 




Factory Method Pattern in Java

//[C] 2002 Sun Microsystems, Inc.---
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.Serializable;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class RunFactoryMethodPattern {
    public static void main(String [] arguments){
        System.out.println("Example for the FactoryMethod pattern");
        System.out.println();
        
        System.out.println("Creating a Contact object");
        System.out.println();
        Contact someone = new Contact();
        
        System.out.println("Creating a GUI editor for the Contact");
        System.out.println();
        System.out.println("The GUI defined in the EditorGui class is a truly generic editor.");
        System.out.println("It accepts an argument of type ItemEditor, and delegates");
        System.out.println(" all editing tasks to its ItemEditor and the associated GUI.");
        System.out.println(" The getEditor() Factory Method is used to obtain the ItemEditor");
        System.out.println(" for the example.");
        System.out.println();
        System.out.println("Notice that the editor in the top portion of the GUI is,");
        System.out.println(" in fact, returned by the ItemEditor belonging to the");
        System.out.println(" Contact class, and has appropriate fields for that class.");
        
        EditorGui runner = new EditorGui(someone.getEditor());
        runner.createGui();
    }
}
class EditorGui implements ActionListener{
    private JFrame mainFrame;
    private JTextArea display;
    private JButton update, exit;
    private JPanel controlPanel, displayPanel, editorPanel;
    private ItemEditor editor;
    
    public EditorGui(ItemEditor edit){
        editor = edit;
    }
    
    public void createGui(){
        mainFrame = new JFrame("Factory Pattern Example");
        Container content = mainFrame.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        
        editorPanel = new JPanel();
        editorPanel.add(editor.getGUI());
        content.add(editorPanel);
        
        displayPanel = new JPanel();
        display = new JTextArea(1040);
        display.setEditable(false);
        displayPanel.add(display);
        content.add(displayPanel);
        
        controlPanel = new JPanel();
        update new JButton("Update Item");
        exit = new JButton("Exit");
        controlPanel.add(update);
        controlPanel.add(exit);
        content.add(controlPanel);
        
        update.addActionListener(this);
        exit.addActionListener(this);
        
        mainFrame.addWindowListener(new WindowCloseManager());
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
    
    
    public void actionPerformed(ActionEvent evt){
        Object originator = evt.getSource();
        if (originator == update){
            updateItem();
        }
        else if (originator == exit){
            exitApplication();
        }
    }
    
    private class WindowCloseManager extends WindowAdapter{
        public void windowClosing(WindowEvent evt){
            exitApplication();
        }
    }
    
    private void updateItem(){
        editor.commitChanges();
        display.setText(editor.toString());
    }
    
    private void exitApplication(){
        System.exit(0);
    }
}

class Contact implements Editable, Serializable {
    private String name;
    private String relationship;

    public ItemEditor getEditor() {
        return new ContactEditor();
    }
    
    private class ContactEditor implements ItemEditor, Serializable {
        private transient JPanel panel;
        private transient JTextField nameField;
        private transient JTextField relationField;
        
        public JComponent getGUI() {
            if (panel == null) {
                panel = new JPanel();
                nameField = new JTextField(name);
                relationField = new JTextField(relationship);
                panel.setLayout(new GridLayout(2,2));
                panel.add(new JLabel("Name:"));
                panel.add(nameField);
                panel.add(new JLabel("Relationship:"));
                panel.add(relationField);
            else {
                nameField.setText(name);
                relationField.setText(relationship);
            }
            return panel;
        }
        
        public void commitChanges() {
            if (panel != null) {
                name = nameField.getText();
                relationship = relationField.getText();
            }
        }
        
        public String toString(){
            return "\nContact:\n" +
                "    Name: " + name + "\n" +
                "    Relationship: " + relationship;
        }
    }
}

interface Editable {
    public ItemEditor getEditor();
}

interface ItemEditor {
    public JComponent getGUI();
    public void commitChanges();
}


           
       














Related examples in the same category
1.Abstract Factory Pattern- Example
2.Abstract Factory Pattern in Java 2
3.Illustrates use of Abstract Factory patternIllustrates use of Abstract Factory pattern
4.Fly weight FactoryFly weight Factory
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.