Created
October 5, 2011 17:20
-
-
Save amrishodiq/1265067 to your computer and use it in GitHub Desktop.
Java: Singleton concept implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.durianapp.tutorial; | |
/** | |
* @author amrishodiq | |
*/ | |
public class SingletonClass { | |
// inilah variable yang menyimpan satu-satunya instance class SingletonClass | |
private static SingletonClass instance; | |
public static synchronized SingletonClass getInstance() { | |
// langkah ini memastikan hanya ada satu instance yang akan dibuat | |
if (instance == null) instance = new SingletonClass(); | |
return instance; | |
} | |
/** | |
* | |
* Constructor class ini dibuat private untuk mencegah class ini dibuat | |
* instance-nya oleh class lain. private akan mencegak baris seperti | |
* berikut: | |
* | |
* SingletonClass x = new SingletonClass(); | |
* | |
*/ | |
private SingletonClass() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment