java - Avoid multiple instances of Singleton? -
java - Avoid multiple instances of Singleton? -
this question has reply here:
what efficient way implement singleton pattern in java? 25 answersi believe when singleton object gets serialized , deserialized upon plethora of times in series, there multiple instances of singletons created.
how can avoid such situation?
normally, when creating singleton, create sure there 1 instance of it. way:
public static class singleton{ private final static singleton instance; private singleton(){} public static singleton getinstance(){ if(instance == null) instance = new singleton(); homecoming instance; } } putting constructor private makes sure no 1 except class can create instance of singleton.
you need phone call singleton getinstance , singleton work on side.
java scala design-patterns singleton
Comments
Post a Comment