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.

Slides:



Advertisements
Similar presentations
Chapter 5: The Singleton Pattern
Advertisements

GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
1 Scenario: Audio Clip Imagine that your application played an audio clip Based on user action, a different audio clip may begin playing You want only.
Graphical User Interfaces
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
GUI programming Graphical user interface-based programming.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
1 Java Swing Application Flow By mr. Hanley 11/7/2005.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
For (int i = 1; i
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Computer Science 209 GUIs Model/View/Controller Layouts.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Errors, GUIs, and Patterns Dr. Doug Twitchell August 30, 2005.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Import javax.swing.*; class Check { public static void main(String[] args) { JFrame frame = new JFrame("Check"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//DO_NOTHING_ON_CLOSE.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Multiple buttons and action calls
Java Applet What is a Java Applet? How is applet compiled?
Graphical User Interfaces -- GUIs
Web Design & Development Lecture 11
GUIs Model/View/Controller Layouts
Design Patterns C++ Java C#.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Abstract Factory Pattern
Design Patterns C++ Java C#.
A Quick Java Swing Tutorial
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Graphical user interface-based programming
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns
Drawing in Java.
Singleton design pattern
class PrintOnetoTen { public static void main(String args[]) {
A Quick Java Swing Tutorial
Advanced Programming in Java
CS 350 – Software Design Singleton – Chapter 21
Design pattern Lecture 6.
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

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.

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.

+getInstance(): Singleton

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(); …

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;

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);

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