CSC/ECE 517 Fall 2012/ch2b 2w53 iv: Difference between revisions
No edit summary |
No edit summary |
||
(15 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
= Singleton, directory of sites | =Singleton, Directory of Sites= | ||
== | This article is presented in the form of directory of a number of sites explaining the use of Singleton pattern.Each of the link to the site is accompanied with the topics covered, a brief summary and a highlight of major points covered in the corresponding web page. | ||
== [http://en.wikipedia.org/wiki/Singleton_pattern Singleton on the Wikipedia] == | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Line 44: | Line 45: | ||
The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized. | The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized. | ||
== [http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ Learn Singleton-design-Pattern]== | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Line 60: | Line 61: | ||
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user. | Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user. | ||
== [http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx Singleton - Creational Design Pattern]== | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Line 131: | Line 132: | ||
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown. | Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown. | ||
== [http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html Simply Singleton] == | |||
'''Topics Covered''': | |||
Capabilities of Singleton pattern,Singleton design pattern class diagram, Example, use of protected constructors. | |||
'''Summary''': | |||
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. | |||
As in , [http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html#resources Design Patterns], "Ensure a class has a single instance, and provide a global point of access to it" | |||
{| | |||
|[[File:4.jpg|x300px]] | |||
|} | |||
Other than the lazy initialization process, a classicSingleton class can also implement a protected constructor so client cannot instantiate ClassicSingleton instances; however, you may be surprised to discover that the following code is perfectly legal: | |||
public class SingletonInstantiator { | |||
public SingletonInstantiator() { | |||
ClassicSingleton instance = ClassicSingleton.getInstance(); | |||
ClassicSingleton anotherInstance = | |||
new ClassicSingleton(); | |||
... | |||
} | |||
} | |||
Protected constructors can be called by subclasses and by other classes in the same package. Because ClassicSingleton and SingletonInstantiator are in the same package (the default package), SingletonInstantiator() methods can create ClassicSingleton instances. This dilemma has two solutions: You can make the ClassicSingleton constructor private so that only ClassicSingleton() methods call it; however, that means ClassicSingleton cannot be subclassed. Sometimes, that is a desirable solution; if so, it's a good idea to declare your singleton class final, which makes that intention explicit and allows the compiler to apply performance optimizations. The other solution is to put your singleton class in an explicit package, so classes in other packages (including the default package) cannot instantiate singleton instances. | |||
== [http://javapapers.com/design-patterns/singleton-pattern/ JavaPaper on Singleton]== | |||
'''Topics Covered''': | |||
Strategy for Singleton instance creation, Early and lazy instantiation in singleton pattern, Singleton and Serialization | |||
'''Summary''': | |||
There are only two points in the definition of a singleton design pattern, | |||
* There should be only one instance allowed for a class and | |||
* We should allow global point of access to that single instance. | |||
The key is not the problem and definition. In singleton pattern, trickier part is implementation and management of that single instance. | |||
We suppress the constructor and don’t allow even a single instance for the class. But we declare an attribute for that same class inside and create instance for that and return it. [http://javapapers.com/design-patterns/abstract-factory-pattern/ Factory design pattern] can be used to create the singleton instance. | |||
You need to be careful with multiple threads. If you don’t synchronize the method which is going to return the instance then, there is a possibility of allowing multiple instances in a multi-threaded scenario. Do the synchronization at block level considering the performance issues. In the below example for singleton pattern, you can see that it is threadsafe. | |||
package com.javapapers.sample.designpattern; | |||
public class Singleton { | |||
private static Singleton singleInstance; | |||
private Singleton() {} | |||
public static Singleton getSingleInstance() { | |||
if (singleInstance == null) { | |||
synchronized (Singleton.class) { | |||
if (singleInstance == null) { | |||
singleInstance = new Singleton(); | |||
} | |||
} | |||
} | |||
return singleInstance; | |||
} | |||
'''Singleton and Serialization''' : Using [http://javapapers.com/core-java/java-serialization/ serialization], single instance contract of the singleton pattern can be violated. You can serialize and de-serialize and get a new instance of the same singleton class. Using java api, you can implement the below method and override the instance read from the stream. So that you can always ensure that you have single instance. | |||
ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException; | |||
'''Usage of Singleton Pattern in Java API''': | |||
java.lang.Runtime#getRuntime() | |||
java.awt.Desktop#getDesktop() | |||
== [http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Aspects Positive and Negative aspects of Singleton] == | |||
'''Topics Covered''': | |||
Introduction, Positive sides of Singleton, Lazy and Static initialization, Negative sides of Singleton, When to use a Singleton class. | |||
'''Summary''': | |||
positive sides: The anatomy of a singleton class is very simple to understand. The class typically has a private constructor which will prohibit you to make any instance of the singleton class; instead you will access a static property or static function of the singleton class to get the reference of a preconfigured instance. These properties/methods ensure that there will be only one instance of the singleton class throughout the lifetime of the application. | |||
The one and only instance of a singleton class is created within the singleton class and its reference is consumed by the callers. The creation process of the instance can be done using any of the following methods: | |||
'''Lazy Initialization''' | |||
If you opt for the lazy instantiation paradigm, then the singleton variable will not get memory until the property or function designated to return the reference is first called. This type of instantiation is very helpful if your singleton class is resource intense. | |||
{| | |||
|[[File:5.gif]] | |||
|} | |||
In order to make it thread-safe, One way is the use of double-checked locking. In double-checked locking, synchronization is only effective when the singleton variable is null, i.e., only for the first time call to Instance. | |||
{| | |||
|[[File:6.gif]] | |||
|} | |||
'''Static Initialization''' | |||
In static initialization, memory is allocated to the variable at the time it is declared. The instance creation takes place behind the scenes when any of the member singleton classes is accessed for the first time. The main advantage of this type of implementation is that the CLR automatically takes care of race conditions I explained in lazy instantiation. We don't have to use any special synchronization constructs here. | |||
{| | |||
|[[File:7.gif]] | |||
|} | |||
* Inheriting a singleton class should be prohibited. | |||
* Singleton takes over static classes on the following shortcomings: | |||
- Static classes don’t promote inheritance. If your class has some interface to derive from, static classes makes it impossible. | |||
- You cannot specify any creation logic with static methods. | |||
- Static methods are procedural code. | |||
'''Negative sides of Singleton''': | |||
* They deviate from the Single Responsibility Principle. A singleton class has the responsibility to create an instance of itself along with other business responsibilities. However, this issue can be solved by delegating the creation part to a factory object. | |||
* Singleton classes cannot be sub classed. | |||
* Singletons can hide dependencies. One of the features of an efficient system architecture is minimizing dependencies between classes. This will in turn help you while conducting unit tests and while isolating any part of the program to a separate assembly. | |||
However, it is commonly accepted that the singleton can yield best results in a situation where various parts of an application concurrently try to access a shared resource. An example of a shared resource would be Logger, Print Spooler, etc. When designing a singleton, consider the following points: | |||
* Singleton classes must be memory-leak free. The instance of the singleton class is to be created once and it remains for the lifetime of the application. | |||
* A real singleton class is not easily extensible. | |||
* Derive the singleton class from an interface. This helps while doing unit testing (using Dependency Injection). | |||
== [http://sourcemaking.com/design_patterns/singleton The Design pattern called Singleton]== | |||
'''Topics Covered''' : | |||
Intent, Problem, Discussion, Structure, Example, Check List, Rules of Thumb | |||
'''Summary''': | |||
Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary. | |||
Singleton should be considered only if all three of the following criteria are satisfied: | |||
* Ownership of the single instance cannot be reasonably assigned | |||
* Lazy initialization is desirable | |||
* Global access is not otherwise provided for | |||
If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.The Singleton pattern can be extended to support access to an application-specific number of instances. Make the class of the single instance responsible for access and “initialization on first use”. The single instance is a private static attribute. The accessor function is a public static method. | |||
'''Example''': | |||
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, “The President of the United States” is a global point of access that identifies the person in the office. | |||
{| | |||
|[[File:8.gif]] | |||
|} | |||
'''Check List''': | |||
* Define a private static attribute in the “single instance” class. | |||
* Define a public static accessor function in the class. | |||
* Do “lazy initialization” (creation on first use) in the accessor function. | |||
* Define all constructors to be protected or private. | |||
* Clients may only use the accessor function to manipulate the Singleton. | |||
'''Rules of thumb''': | |||
* Abstract Factory, Builder, and Prototype can use Singleton in their implementation. | |||
* Facade objects are often Singletons because only one Facade object is required. | |||
* State objects are often Singletons. | |||
* The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, | |||
you can change your mind and manage any number of instances. | |||
* The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it. | |||
* When is Singleton unnecessary? Short answer: most of the time. Long answer: when it’s simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. | |||
== [http://c2.com/cgi/wiki?SingletonPattern Singleton explained on c2.com]== | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Line 163: | Line 314: | ||
} | } | ||
== [http://www.oodesign.com/singleton-pattern.html Singleton Applications]== | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Line 185: | Line 336: | ||
==[http://msdn.microsoft.com/en-us/library/ff650316.aspx Singleton Implementation in C#]== | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Line 224: | Line 375: | ||
== [http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html Refactor Singleton Out Of Your Code]== | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Line 234: | Line 385: | ||
'''Steps to refactor Singleton:''' | '''Steps to refactor Singleton:''' | ||
* Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton. | |||
public interface ISingletonRegistry { | public interface ISingletonRegistry { | ||
SingletonClass getSingletonClass(); | SingletonClass getSingletonClass(); | ||
Line 240: | Line 391: | ||
} | } | ||
* Make SingletonClass's constructor public | |||
* Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry. | |||
public class SingletonRegistry implements ISingletonRegistry { | public class SingletonRegistry implements ISingletonRegistry { | ||
Line 262: | Line 413: | ||
} | } | ||
* Change getInstance method of SingletonClass to get instance from SingletonRegistry. | |||
public static SingletonClass getInstance() { | public static SingletonClass getInstance() { | ||
return SingletonRegistry.getInstance().getSingletonClass(); | return SingletonRegistry.getInstance().getSingletonClass(); | ||
} | } | ||
* The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass. | |||
public class Client { | public class Client { | ||
public void clientMethod() { | public void clientMethod() { | ||
Line 275: | Line 426: | ||
} | } | ||
* Delete INSTANCE constant from SingletonClass class. | |||
* You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface. | |||
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry. | And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry. | ||
== [http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern/ Refactoring:Extracting the Singleton Pattern]== | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Line 289: | Line 440: | ||
Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient. | Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient. | ||
== [http://www.roseindia.net/designpattern/singleton_pattern.shtml Singleton Pattern Usage and Benefits]== | |||
'''Topics Covered''' : | |||
Usage, Benefits, Example | |||
'''Summary''' : | |||
This link describes about the benefits like instance control and usage in multithreaded environment.Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables. | |||
'''Example''': | |||
package singleton; | |||
public class Logger { | |||
private String fileName; | |||
private Properties properties; | |||
private Priority priority; | |||
private Logger() { | |||
logger = this; | |||
} | |||
public int getRegisteredLevel() { | |||
int i = 0; | |||
try { | |||
InputStream inputstream = getClass().getResourceAsStream("Logger.properties"); | |||
properties.load(inputstream); | |||
inputstream.close(); | |||
i = Integer.parseInt(properties.getProperty("logger.registeredlevel")); | |||
if(i < 0 || i > 3) | |||
i = 0; | |||
} | |||
catch(Exception exception) { | |||
System.out.println("Logger: Failed in the getRegisteredLevel method"); | |||
exception.printStackTrace(); | |||
} | |||
return i; | |||
} | |||
public static void initialize() { | |||
logger = new Logger(); | |||
} | |||
// singleton - pattern | |||
private static Logger logger; | |||
public static Logger getLogger() { | |||
return logger; | |||
} | |||
} | |||
== [http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern Critique of the Singleton Pattern]== | |||
'''Topics Covered''' : | '''Topics Covered''' : | ||
Critique of the Singleton Pattern | Critique of the Singleton Pattern | ||
Line 297: | Line 489: | ||
'''Summary''' : | '''Summary''' : | ||
This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern. | This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern. | ||
==[http://www.ibm.com/developerworks/webservices/library/co-single/index.html Use Singletons Wisely]== | |||
'''Topics Covered''' : | |||
When to use, Moving away from Singletons, Aggregating Singletons | |||
'''Summary''' : | |||
The programming community discourages using global data and objects. Still, there are times when an application needs a single instance of a given class and a global point of access to that class. The general solution is the design pattern known as singletons. However, singletons are unnecessarily difficult to test and may make strong assumptions about the applications that will use them. In this article the author discusses strategies for avoiding the singleton pattern for that majority of cases where it is not appropriate. He also describes the properties of some classes that are truly singletons. | |||
'''Aggregating Singletons: The Toolbox''': | |||
Singleton abuse can be avoided by looking at the problem from a different angle. Suppose an application needs only one instance of a class and the application configures that class at startup: Why should the class itself be responsible for being a singleton? It seems quite logical for the application to take on this responsibility, since the application requires this kind of behavior. The application, not the component, should be the singleton. The application then makes an instance of the component available for any application-specific code to use. When an application uses several such components, it can aggregate them into what we have called a toolbox. | |||
Put simply, the application's toolbox is a singleton that is responsible either for configuring itself or for allowing the application's startup mechanism to configure it. The general pattern of the Toolbox singleton is as shown in the example provided in this article | |||
The Toolbox is itself a singleton, and it manages the lifetime of the various component instances. Either the application configures it, or it asks the application for configuration information in method initialize. Now the application can decide how many instances of which classes it requires. Changes in those decisions may affect application-specific code, but not reusable, infrastructure-level code. Moreover, testing infrastructure code is much easier, as those classes do not rely on the way in which any application may choose to use them. | |||
== [http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious When Not to Use Singleton]== | |||
'''Topics Covered''' : | |||
Appropriate Use of the Singleton Pattern | |||
'''Summary''' : | |||
The Gang of Four states that you'll want to use Singleton there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code. | |||
=References= | |||
http://en.wikipedia.org/wiki/Singleton_pattern | |||
http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/ | |||
http://www.c-sharpcorner.com/UploadFile/faraz.rasheed/SingletonPattern12052005063955AM/SingletonPattern.aspx | |||
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html | |||
http://javapapers.com/design-patterns/singleton-pattern/ | |||
http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Aspects | |||
http://sourcemaking.com/design_patterns/singleton | |||
http://c2.com/cgi/wiki?SingletonPattern | |||
http://www.oodesign.com/singleton-pattern.html | |||
http://msdn.microsoft.com/en-us/library/ff650316.aspx | |||
http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html | |||
http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern | |||
http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern | |||
http://www.roseindia.net/designpattern/singleton_pattern.shtml | |||
http://www.ibm.com/developerworks/webservices/library/co-single/index.html | |||
http://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious |
Latest revision as of 02:11, 20 November 2012
Singleton, Directory of Sites
This article is presented in the form of directory of a number of sites explaining the use of Singleton pattern.Each of the link to the site is accompanied with the topics covered, a brief summary and a highlight of major points covered in the corresponding web page.
Singleton on the Wikipedia
Topics Covered : Common uses, UML, Implementation, Example, Prototype based singleton, Example of use with factory method pattern
Summary : This link firstly provides the basic definition of the singleton pattern as a design pattern that restricts the instantiation of a class to one object. Singleton patters are mostly used in Abstract Factory, Builder, and Prototype, and Facade patterns.
Both the UML representation of singleton where the same single instance is always returned and the implementation concerning the mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among the class objects. The link also points out that if a class has to realize a contract expressed by an interface, it really has to be a singleton.
Lazy initialization uses double-checking and eager initialization which always creates an instance. For instance, Lazy initialization and Eager Initialization example codes are: Lazy Initialization:
public class SingletonDemo { private static volatile SingletonDemo instance = null; private SingletonDemo() {} public static SingletonDemo getInstance() { if (instance == null) { synchronized (SingletonDemo .class){ if (instance == null) { instance = new SingletonDemo (); } } } return instance; } }
Eager Initialization:
public class Singleton { private static final Singleton instance = new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } }
It also talks about Prototype-based programming in which objects but not classes are used, a 'singleton' simply refers to an object without copies or that is not used as the prototype for any other object. Eg :->
Foo := Object clone Foo clone := Foo
Drawbacks : The pattern makes unit testing far more difficult as it introduces global state into an application. It should also be noted that this pattern reduces the potential for parallelism within a program, because access to the singleton in a multi-threaded context must be serialized.
Learn Singleton-design-Pattern
Topics Covered : Definition, When to use, how to create, Sharing across all users, Sharing across a request, Sharing across a single user
Summary : This article explains what Singleton pattern is, what kind of problem it generally solves and how should it be implemented in ASP.NET. The Singleton pattern which ensures that only one instance of a given object can exist at a context solves problems related to object creation and hence is a type of creational pattern.
It can be used in a class that wraps the settings related to an application. In other words, whenever we want something to be shared across multiple locations, we use a singleton pattern. In order to create a Singleton pattern, we can render the constructor private so that no user can create a new instance outside the class, that way ensuring only one instance of the objects always exists. In that case, we also need to create a static method that returns the single object.
Singleton patterns in ASP.NET are implemented by using static objects which maintain their values and reside in the memory as long as the application which contains it does. The sharing can occur across users, or requests or across a single user.
Singleton - Creational Design Pattern
Topics Covered : Intent, Description, An Example, Implementation, Benefits
Summary : It firstly talks about the intent behind singleton design pattern, in that there is a need to have a class that can be instantiated only once. Then, it describes two solutions for implementing the singleton class. In the first, there should be only one shared object and reference to that shared object should be available through a static method GetInstance() while the constructor is private. The second solution expects the constructor to be public but once an object has been instantiated, an exception should be thrown for each successive constructor call.
The code for each case is given as follows:
Case 1 :
class Singleton { private static Singleton instance; private static int numOfReference; private string code; private Singleton() { numOfReference = 0; code = "Maasoom Faraz"; } public static Singleton GetInstance() { if(instance == null) { instance = new Singleton(); } numOfReference++; return instance; } public static int Reference { get { return numOfReference; } } public string Code { get { return code; } set { code = value;} } }
The constructor is made private and used to initialize the numOfReference and default value of code. GetInstance() method checks the instance, if it is null then it assign it an instance of Singleton otherwise return the old reference.
Case 2 :
class Singleton2 { private static int numOfInstance = 0; public Singleton2() { if(numOfInstance == 0) { Console.WriteLine("\r\nCreating First Object of Singleton2 class..."); numOfInstance++; } else { throw new Exception("This class is Singleton, + so only one object of it can be instantiated."); } } }
Here we make the constructor public and use a private field numOfInstance which is incremented for each constructor call. If numOfInstance is zero (no object is yet instantiated), a new object is allowed to made. But, if this value is not zero (there is already an object of Singleton2 class, an exception is thrown.
Simply Singleton
Topics Covered: Capabilities of Singleton pattern,Singleton design pattern class diagram, Example, use of protected constructors.
Summary: 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.
As in , Design Patterns, "Ensure a class has a single instance, and provide a global point of access to it"
Other than the lazy initialization process, a classicSingleton class can also implement a protected constructor so client cannot instantiate ClassicSingleton instances; however, you may be surprised to discover that the following code is perfectly legal:
public class SingletonInstantiator { public SingletonInstantiator() { ClassicSingleton instance = ClassicSingleton.getInstance(); ClassicSingleton anotherInstance = new ClassicSingleton(); ... } }
Protected constructors can be called by subclasses and by other classes in the same package. Because ClassicSingleton and SingletonInstantiator are in the same package (the default package), SingletonInstantiator() methods can create ClassicSingleton instances. This dilemma has two solutions: You can make the ClassicSingleton constructor private so that only ClassicSingleton() methods call it; however, that means ClassicSingleton cannot be subclassed. Sometimes, that is a desirable solution; if so, it's a good idea to declare your singleton class final, which makes that intention explicit and allows the compiler to apply performance optimizations. The other solution is to put your singleton class in an explicit package, so classes in other packages (including the default package) cannot instantiate singleton instances.
JavaPaper on Singleton
Topics Covered: Strategy for Singleton instance creation, Early and lazy instantiation in singleton pattern, Singleton and Serialization
Summary: There are only two points in the definition of a singleton design pattern,
- There should be only one instance allowed for a class and
- We should allow global point of access to that single instance.
The key is not the problem and definition. In singleton pattern, trickier part is implementation and management of that single instance.
We suppress the constructor and don’t allow even a single instance for the class. But we declare an attribute for that same class inside and create instance for that and return it. Factory design pattern can be used to create the singleton instance.
You need to be careful with multiple threads. If you don’t synchronize the method which is going to return the instance then, there is a possibility of allowing multiple instances in a multi-threaded scenario. Do the synchronization at block level considering the performance issues. In the below example for singleton pattern, you can see that it is threadsafe.
package com.javapapers.sample.designpattern; public class Singleton { private static Singleton singleInstance; private Singleton() {} public static Singleton getSingleInstance() { if (singleInstance == null) { synchronized (Singleton.class) { if (singleInstance == null) { singleInstance = new Singleton(); } } } return singleInstance; }
Singleton and Serialization : Using serialization, single instance contract of the singleton pattern can be violated. You can serialize and de-serialize and get a new instance of the same singleton class. Using java api, you can implement the below method and override the instance read from the stream. So that you can always ensure that you have single instance.
ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
Usage of Singleton Pattern in Java API:
java.lang.Runtime#getRuntime() java.awt.Desktop#getDesktop()
Positive and Negative aspects of Singleton
Topics Covered: Introduction, Positive sides of Singleton, Lazy and Static initialization, Negative sides of Singleton, When to use a Singleton class.
Summary: positive sides: The anatomy of a singleton class is very simple to understand. The class typically has a private constructor which will prohibit you to make any instance of the singleton class; instead you will access a static property or static function of the singleton class to get the reference of a preconfigured instance. These properties/methods ensure that there will be only one instance of the singleton class throughout the lifetime of the application.
The one and only instance of a singleton class is created within the singleton class and its reference is consumed by the callers. The creation process of the instance can be done using any of the following methods:
Lazy Initialization If you opt for the lazy instantiation paradigm, then the singleton variable will not get memory until the property or function designated to return the reference is first called. This type of instantiation is very helpful if your singleton class is resource intense.
In order to make it thread-safe, One way is the use of double-checked locking. In double-checked locking, synchronization is only effective when the singleton variable is null, i.e., only for the first time call to Instance.
Static Initialization In static initialization, memory is allocated to the variable at the time it is declared. The instance creation takes place behind the scenes when any of the member singleton classes is accessed for the first time. The main advantage of this type of implementation is that the CLR automatically takes care of race conditions I explained in lazy instantiation. We don't have to use any special synchronization constructs here.
- Inheriting a singleton class should be prohibited.
- Singleton takes over static classes on the following shortcomings:
- Static classes don’t promote inheritance. If your class has some interface to derive from, static classes makes it impossible. - You cannot specify any creation logic with static methods. - Static methods are procedural code.
Negative sides of Singleton:
- They deviate from the Single Responsibility Principle. A singleton class has the responsibility to create an instance of itself along with other business responsibilities. However, this issue can be solved by delegating the creation part to a factory object.
- Singleton classes cannot be sub classed.
- Singletons can hide dependencies. One of the features of an efficient system architecture is minimizing dependencies between classes. This will in turn help you while conducting unit tests and while isolating any part of the program to a separate assembly.
However, it is commonly accepted that the singleton can yield best results in a situation where various parts of an application concurrently try to access a shared resource. An example of a shared resource would be Logger, Print Spooler, etc. When designing a singleton, consider the following points:
- Singleton classes must be memory-leak free. The instance of the singleton class is to be created once and it remains for the lifetime of the application.
- A real singleton class is not easily extensible.
- Derive the singleton class from an interface. This helps while doing unit testing (using Dependency Injection).
The Design pattern called Singleton
Topics Covered : Intent, Problem, Discussion, Structure, Example, Check List, Rules of Thumb
Summary: Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary. Singleton should be considered only if all three of the following criteria are satisfied:
- Ownership of the single instance cannot be reasonably assigned
- Lazy initialization is desirable
- Global access is not otherwise provided for
If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.The Singleton pattern can be extended to support access to an application-specific number of instances. Make the class of the single instance responsible for access and “initialization on first use”. The single instance is a private static attribute. The accessor function is a public static method.
Example: The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, “The President of the United States” is a global point of access that identifies the person in the office.
Check List:
- Define a private static attribute in the “single instance” class.
- Define a public static accessor function in the class.
- Do “lazy initialization” (creation on first use) in the accessor function.
- Define all constructors to be protected or private.
- Clients may only use the accessor function to manipulate the Singleton.
Rules of thumb:
- Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
- Facade objects are often Singletons because only one Facade object is required.
- State objects are often Singletons.
- The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and,
you can change your mind and manage any number of instances.
- The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.
- When is Singleton unnecessary? Short answer: most of the time. Long answer: when it’s simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally.
Singleton explained on c2.com
Topics Covered : Simulation of global variables, Appropriate Use of Singleton, Singleton in threaded environment
Summary : This link firstly explains aspects like testing, creational logic,polymorphism involved in singleton pattern.It explains situations when singleton pattern can be used.It goes on to explain the anti-pattern of singletons simulating global variables and appropriate use of singleton in mutithreaded environment.
Problem of using Singleton in Multithreaded Environment :
static private synchronized Singleton instance_helper(){ _instance = new Singleton(); return _instance; }
"_instance" can be null in the logic for multiple threads before instance_helper is called, leading to multiple instances being created
Solution for Singleton in Multithreaded Environment :
static private synchronized Singleton instance_helper() { if(flag) { _instance = new Singleton(); } flag = false; return _instance; }
Singleton Applications
Topics Covered : Motivation, Intent, Implementation, Applicability and Examples
Summary : This link gives some extra information about applicability of Singleton in logger classes,configuration classes, accessing resources in shared environment,factories implemented as Singleton,Implementations and problems involved
Serialization :
If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be implemented.
public class Singleton implements Serializable { ... // This method is called immediately after an object of this class is deserialized. // This method returns the singleton instance. protected Object readResolve() { return getInstance(); } }
Singleton Implementation in C#
Topics Covered : Implementation in C#
Summary : This link explains how to implement a singleton in C# both in static and threaded environment.
Static Initialization:
One of the reasons Design Patterns [Gamma95] avoided static initialization is because the C++ specification left some ambiguity around the initialization order of static variables. Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization:
public sealed class Singleton { private static readonly Singleton instance = new Singleton(); private Singleton(){} public static Singleton Instance { get { return instance; } } }
Implementing Singleton in C# results in the following benefits and liabilities:
Benefits:
- The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.
- The Double-Check Locking idiom described earlier in "Multithreaded Singleton" is implemented correctly in the common language runtime.
Liabilities: If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.
Refactor Singleton Out Of Your Code
Topics Covered : Solution to refactor singleton
Summary : Gang of Four mentions Singleton as an anti-pattern and not as a pattern. This Link provides a solution to refactor Singleton out of the code
Steps to refactor Singleton:
- Create simple interface for Registry with two simple methods - getter and setter for instance of class which is currently implemented as singleton.
public interface ISingletonRegistry { SingletonClass getSingletonClass(); void setSingletonClass(SingletonClass singleton); }
- Make SingletonClass's constructor public
- Then implement the interface as a simplest Registry design pattern implementation - SingletonRegistry.
public class SingletonRegistry implements ISingletonRegistry { private static final SingletonRegistry INSTANCE = new SingletonRegistry(); private SingletonClass singleton; public static ISingletonRegistry getInstance() { return INSTANCE; } private SingletonRegistry() { //this is the reason why the constructor should be public singleton = new SingletonClass(); } public SingletonClass getSingletonClass() { return singleton; } public void setSingletonClass(SingletonClass singleton) { this.singleton = singleton; } }
- Change getInstance method of SingletonClass to get instance from SingletonRegistry.
public static SingletonClass getInstance() { return SingletonRegistry.getInstance().getSingletonClass(); }
- The method getInstance from SingletonClass disappears and all it's client classes uses SingletonRegistry to access SingletonClass.
public class Client { public void clientMethod() { SingletonRegistry.getInstance() .getSingletonClass().voidMethod("param"); Object object = SingletonRegistry.getInstance() .getSingletonClass().objectMethod("param"); } }
- Delete INSTANCE constant from SingletonClass class.
- You can Extract Interface (Alt+Shift+T, E) from SingletonClass. Write just new interface name, select all methods from SingletonClass which you can extract into new interface and press OK. All references to SingletonClass will be refactored to references to your newly created interface.
And that is all. Now you have SingletonClass with totally same functionality but you are able to mock it, extend or replace by different implementation setting up your instance of SingletonClass to SingletonRegistry.
Refactoring:Extracting the Singleton Pattern
Topics Covered : Extracting the Singleton Pattern
Summary : Here, the author demonstrates the refactoring of Singleton Pattern by means of an example of a Device Manger class. So the main class that will be changing is the DeviceManager. This class manages a collection of Devices (or a dictionary, to be more precise) and allows to set which is the active device and to get a device by id. Note that this last feature could be handled by the Devices collection, but we are taking advantage of the dictionary to make that look-up more efficient.
Singleton Pattern Usage and Benefits
Topics Covered : Usage, Benefits, Example
Summary : This link describes about the benefits like instance control and usage in multithreaded environment.Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables.
Example:
package singleton; public class Logger { private String fileName; private Properties properties; private Priority priority; private Logger() { logger = this; } public int getRegisteredLevel() { int i = 0; try { InputStream inputstream = getClass().getResourceAsStream("Logger.properties"); properties.load(inputstream); inputstream.close(); i = Integer.parseInt(properties.getProperty("logger.registeredlevel")); if(i < 0 || i > 3) i = 0; } catch(Exception exception) { System.out.println("Logger: Failed in the getRegisteredLevel method"); exception.printStackTrace(); } return i; } public static void initialize() { logger = new Logger(); } // singleton - pattern private static Logger logger; public static Logger getLogger() { return logger; } }
Critique of the Singleton Pattern
Topics Covered : Critique of the Singleton Pattern
Summary : This link describes about the benefits like instance control and flexibility and drawbacks like overhead, development confusion and Object lifetime while using the singleton pattern.
Use Singletons Wisely
Topics Covered : When to use, Moving away from Singletons, Aggregating Singletons
Summary : The programming community discourages using global data and objects. Still, there are times when an application needs a single instance of a given class and a global point of access to that class. The general solution is the design pattern known as singletons. However, singletons are unnecessarily difficult to test and may make strong assumptions about the applications that will use them. In this article the author discusses strategies for avoiding the singleton pattern for that majority of cases where it is not appropriate. He also describes the properties of some classes that are truly singletons.
Aggregating Singletons: The Toolbox: Singleton abuse can be avoided by looking at the problem from a different angle. Suppose an application needs only one instance of a class and the application configures that class at startup: Why should the class itself be responsible for being a singleton? It seems quite logical for the application to take on this responsibility, since the application requires this kind of behavior. The application, not the component, should be the singleton. The application then makes an instance of the component available for any application-specific code to use. When an application uses several such components, it can aggregate them into what we have called a toolbox. Put simply, the application's toolbox is a singleton that is responsible either for configuring itself or for allowing the application's startup mechanism to configure it. The general pattern of the Toolbox singleton is as shown in the example provided in this article The Toolbox is itself a singleton, and it manages the lifetime of the various component instances. Either the application configures it, or it asks the application for configuration information in method initialize. Now the application can decide how many instances of which classes it requires. Changes in those decisions may affect application-specific code, but not reusable, infrastructure-level code. Moreover, testing infrastructure code is much easier, as those classes do not rely on the way in which any application may choose to use them.
When Not to Use Singleton
Topics Covered : Appropriate Use of the Singleton Pattern
Summary : The Gang of Four states that you'll want to use Singleton there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.
References
http://en.wikipedia.org/wiki/Singleton_pattern
http://www.c-sharpcorner.com/UploadFile/SukeshMarla/learn-design-pattern-singleton-pattern/
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
http://javapapers.com/design-patterns/singleton-pattern/
http://www.codeproject.com/Articles/307233/Singleton-Pattern-Positive-and-Negative-Aspects
http://sourcemaking.com/design_patterns/singleton
http://c2.com/cgi/wiki?SingletonPattern
http://www.oodesign.com/singleton-pattern.html
http://msdn.microsoft.com/en-us/library/ff650316.aspx
http://bosy.dailydev.org/2007/08/refactor-singleton-out-of-your-code.html
http://www.e-pedro.com/2010/05/refactoring-extracting-the-singleton-pattern
http://www.dotnetobject.com/Thread-Benefits-of-Singleton-Pattern-and-Drawbacks-of-Singleton-Pattern
http://www.roseindia.net/designpattern/singleton_pattern.shtml
http://www.ibm.com/developerworks/webservices/library/co-single/index.html