Presentation is loading. Please wait.

Presentation is loading. Please wait.

Real World Scenario Sometimes it's appropriate to have exactly one instance of a class: window manager print spooler Filesystems press Ctrl-F to display.

Similar presentations


Presentation on theme: "Real World Scenario Sometimes it's appropriate to have exactly one instance of a class: window manager print spooler Filesystems press Ctrl-F to display."— Presentation transcript:

1 Real World Scenario Sometimes it's appropriate to have exactly one instance of a class: window manager print spooler Filesystems press Ctrl-F to display a Find dialog. Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system, and therefore require a global point of access.

2 Singleton pattern Intent Ensure a class only has one instance, and provide a global point of access to it Motivation It’s important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system (or file system manager) and one window manager.

3 +getInstance(): Singleton

4 public class Singleton {
private static Singleton instance = null; private Singleton() { } public static Singleton getInstance() { if(instance == null) { instance = new Singleton(); return instance; public class TestSingleton{ public static void main(String[] args){ Singleton s = Singleton.getInstance();

5 package singleton; import javax.swing.*; public class SingletonFrame extends JFrame { private static SingletonFrame myInstance = null; // the constructor private SingletonFrame() { this.setSize(400, 100); this.setTitle("Singleton Frame. Timestamp:" + System.currentTimeMillis()); this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); } public static SingletonFrame getInstance() { if (myInstance == null) myInstance = new SingletonFrame(); return myInstance;

6 package singleton; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyFrame extends JFrame { JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); public MyFrame() { init(); } public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setSize(300, 250); frame.setVisible(true);

7 public void actionPerformed(ActionEvent e) {
private void init() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); jButton1.setText("Show Singleton Frame"); jButton1.setBounds(new Rectangle(12, 12, 220, 40)); jButton1.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { SingletonFrame singletonFrame = SingletonFrame.getInstance(); singletonFrame.setVisible(true); } ); jButton2.setText("Show the same Singleton Frame"); jButton2.setBounds(new Rectangle(12, 72, 220, 40)); jButton2.addActionListener( this.getContentPane().setLayout(null); this.getContentPane().add(jButton1, null); this.getContentPane().add(jButton2, null); } // end of class MyFrame

8


Download ppt "Real World Scenario Sometimes it's appropriate to have exactly one instance of a class: window manager print spooler Filesystems press Ctrl-F to display."

Similar presentations


Ads by Google