Singleton Pattern 2 : Singleton Pattern « Design Pattern « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Design Pattern » Singleton PatternScreenshots 
Singleton Pattern 2
Singleton Pattern 2

//[C] 2002 Sun Microsystems, Inc.---

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class RunSingletonPattern {
  public static void main(String[] arguments) {
    System.out.println("Example for Singleton pattern");
    System.out.println();
    System.out.println("This example will demonstrate the use of");
    System.out.println(" the Singleton pattern by creating two GUI");
    System.out.println(" editors, both of which will reference the");
    System.out.println(" same underlying history list.");

    System.out.println("Creating the first editor");
    System.out.println();
    SingletonGui editor1 = new SingletonGui();
    editor1.createGui();

    System.out.println("Creating the second editor");
    System.out.println();
    SingletonGui editor2 = new SingletonGui();
    editor2.createGui();
  }
}

class SingletonGui implements ActionListener {
  private JFrame mainFrame;

  private JTextArea display;

  private JButton newContact, newAppointment, undo, refresh, exit;

  private JPanel controlPanel, displayPanel;

  private static int historyCount;

  public void createGui() {
    mainFrame = new JFrame("Singleton Pattern Example");
    Container content = mainFrame.getContentPane();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

    displayPanel = new JPanel();
    display = new JTextArea(2060);
    display.setEditable(false);
    displayPanel.add(display);
    content.add(displayPanel);

    controlPanel = new JPanel();
    newContact = new JButton("Create contact");
    newAppointment = new JButton("Create appointment");
    undo = new JButton("Undo");
    refresh = new JButton("Refresh");
    exit = new JButton("Exit");
    controlPanel.add(newContact);
    controlPanel.add(newAppointment);
    controlPanel.add(undo);
    controlPanel.add(refresh);
    controlPanel.add(exit);
    content.add(controlPanel);

    newContact.addActionListener(this);
    newAppointment.addActionListener(this);
    undo.addActionListener(this);
    refresh.addActionListener(this);
    exit.addActionListener(this);

    mainFrame.addWindowListener(new WindowCloseManager());
    mainFrame.pack();
    mainFrame.setVisible(true);
  }

  public void refreshDisplay(String actionMessage) {
    display.setText(actionMessage + "\nCOMMAND HISTORY:\n"
        + HistoryList.getInstance().toString());
  }

  public void actionPerformed(ActionEvent evt) {
    Object originator = evt.getSource();
    if (originator == newContact) {
      addCommand(" New Contact");
    else if (originator == newAppointment) {
      addCommand(" New Appointment");
    else if (originator == undo) {
      undoCommand();
    else if (originator == refresh) {
      refreshDisplay("");
    else if (originator == exit) {
      exitApplication();
    }
  }

  private class WindowCloseManager extends WindowAdapter {
    public void windowClosing(WindowEvent evt) {
      exitApplication();
    }
  }

  private void addCommand(String message) {
    HistoryList.getInstance().addCommand((++historyCount+ message);
    refreshDisplay("Add Command: " + message);
  }

  private void undoCommand() {
    Object result = HistoryList.getInstance().undoCommand();
    historyCount--;
    refreshDisplay("Undo Command: " + result);
  }

  private void exitApplication() {
    System.exit(0);
  }
}

class HistoryList {
  private List history = Collections.synchronizedList(new ArrayList());

  private static HistoryList instance = new HistoryList();

  private HistoryList() {
  }

  public static HistoryList getInstance() {
    return instance;
  }

  public void addCommand(String command) {
    history.add(command);
  }

  public Object undoCommand() {
    return history.remove(history.size() 1);
  }

  public String toString() {
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < history.size(); i++) {
      result.append("  ");
      result.append(history.get(i));
      result.append("\n");
    }
    return result.toString();
  }
}


           
       
Related examples in the same category
1. Singleton
2. Singleton pattern in Java 1
3. A static class implementation of Singleton patternA static class implementation of Singleton pattern
4. Singleton pattern in Java 2Singleton pattern in Java 2
5. Singleton pattern in Java 3Singleton pattern in Java 3
ww_w_._j___a__v_a___2__s___.__c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.