JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
THIS CODE CANNOT BE RUN, IT IS JUST AN EXPLANATION!
Subject:
Very simple example how to implement the singleton pattern.
The singleton pattern limits the instantiation of a class to one object.
So it can be used when exactly one object is needed.
*/
public class Singleton {
private static Singleton instance = null;
protected Singleton() {
// Doesn't allow instantiation.
}
// Only way to access the class:
public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run