- Singleton Class in java
Singleton class is used when we want to restrict the creation of object outside the class, By keeping the constructor private. In singleton class object is created only once and we use single instance throughout the other classes ie only one instance.
Singleton class allows us to create object only once and allows global point of access to that instance.
http://www.linkedin.com/groupItem?view=&srchtype=discussedNews&gid=118012&item=174721706&type=member&trk=eml-anet_dig-b_pd-ttl-cn&ut=04hL0rGxGJyls1
- Simply Singleton
Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and filesystems are prototypical examples. 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
The Singleton design pattern addresses all of the previous paragraph's concerns. With the Singleton design pattern you can:
Ensure that only one instance of a class is created
Provide a global point of access to the object
Allow multiple instances in the future without affecting a singleton class's clients
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
- How do I implement a Singleton pattern?
Singleton pattern used when we want to allow only a single instance of a class can be created inside our application. Using this pattern ensures that a class only have a single instance by protecting the class creation process, by setting the class constructor into private access modifier.
http://www.kodejava.org/examples/12.html
- Singleton Pattern
Motivation
Sometimes it's important to have only one instance for a class. For example, in a system there should be only one window manager (or only a file system or only a print spooler).
Intent
Ensure that only one instance of a class is created.
Provide a global point of access to the object.
Implementation
The implementation involves a static member in the "Singleton" class, a private constructor and a static public method that returns a reference to the static member
Applicability & Examples
Example 1 - Logger Classes
Example 2 - Configuration Classes
Example 3 - Accesing resources in shared mode
Example 4 - Factories implemented as Singletons
http://www.oodesign.com/singleton-pattern.html
- How many ways you can write Singleton Class in Java?
1) Singleton by synchronizing getInstance() method
2) Singleton with public static final field initialized during class loading.
3) Singleton generated by static nested class, also referred as Singleton holder pattern.
4) From Java 5 on-wards using Enums
more questions
14) Singleton vs Static Class?
15) When to choose Singleton over Static Class?
16) Can you replace Singleton with Static Class in Java?
17) Difference between Singleton and Static Class in java?
18) Advantage of Singleton over Static Class?
http://javarevisited.blogspot.com/2011/03/10-interview-questions-on-singleton.html#ixzz2HxMh1zPr
No comments:
Post a Comment