quartz/content/Obsidian Vault/android/Singleton Design Pattern.md
2022-06-07 14:39:39 -06:00

468 B

#cs240 When you want to have only one instance of an object in all of your code, you define its constructor as private, and create a public static getInstance method. The single instance is put into a private static variable.

public class DataCache {
	private static DataCache instance;
	
	public static DataCache getInstance() {
		if(instance == null) {
			instance = new DataCache();
			}
		
			return instance;
		}
	
	private DataCache(){
	
	}
}