Your SlideShare is downloading. ×
0
Singleton pattern
Singleton pattern
Singleton pattern
Singleton pattern
Singleton pattern
Singleton pattern
Singleton pattern
Singleton pattern
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Singleton pattern

463

Published on

This ppt is about singleton design patterns in designing …

This ppt is about singleton design patterns in designing
We have used java as our main language for example.
It consists of
Defination
Purpose
Intent
Example of NOTICE BOARD OF AN INSTITUTION
java program to implement the above example

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
463
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 6483Tejas J Ghalsasi 6487 Amit Goregaonkar 6493 Kapil Khond 6494 Rohan Kotian
  • 2. What’s Singleton?  A design pattern that is used to restrict instantiation of a class to one object Class with a single instance
  • 3. Intent & Problem Purpose •Ensure a class has only one instance, and provide a global point of access to it. •This property is useful to create singleton class in java. Singleton pattern helps us to keep only one instance of a class at any time. •Encapsulated "just-in-time initialization" or "initialization on first use". •The purpose of singleton is to control object creation by keeping private constructor.  ProblemProblem •Application needs one, and only one, instance of an object.
  • 4. Java Program  We consider an example of a notice-board.  For the sake of simplicity we consider the case of our IT dept Notice Board. A peon comes and updates message time to time ie say results are put up.  NOTICE BOARD REMAINS THE SAME hence inspite of new objects created the messages will be stuck to the ”SAME NOTICEBOARD””SAME NOTICEBOARD”
  • 5. Implementation in JAVA: /* TEJAS J GHALSASI TEIT 6483 */ import java.util.*; public class NoticeBoard { private static NoticeBoard a; String message; /** * Create private constructor */ private NoticeBoard(){ } /** * Create a static method to get instance. */ public static NoticeBoard getInstance(){ if(a == null){ a = new NoticeBoard(); } return a; }
  • 6. public void getMessage(String msg){ message=msg; System.out.println("message updated n"); } public void postMessage(){ System.out.println("Current message is: "+message); }
  • 7. public static void main(String a[]){ Scanner sc=new Scanner(System.in); NoticeBoard nb = NoticeBoard.getInstance(); NoticeBoard nb1 = NoticeBoard.getInstance(); System.out.println("enter message"); String msg=sc.nextLine(); nb.getMessage(msg); System.out.println("message stored in nb"); nb.postMessage(); System.out.println("message stored in nb1"); nb1.postMessage(); System.out.println("enter a new message message"); msg=sc.nextLine(); nb.getMessage(msg); System.out.println("message stored in nb"); nb.postMessage(); System.out.println("message stored in nb1"); nb1.postMessage(); } } /* THIS PROGRAM IS CODED BY TG-6483 COPY RIGHTS RESERVED. */
  • 8. Thank You! Rohan KotianTejas J Ghalsasi 6483 Amit Goregaonkar 6487 Kapil Khond 6493 6494

×