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

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Design Pattern » Factory PatternScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.