1. A Hungry man type
1 Packagesingleinstance;2 /**a hungry man, class loading creates instances, wastes resources, threads are safe*/3 Public classSINGLEDOME01 {4 //private construction methods to prevent direct new objects5 PrivateSingleDome01 () {6 }7 //when the class loads, the object is created8 Private StaticSingleDome01 instance =NewSingleDome01 ();9 //provides getter for easy access to private objectsTen One Public StaticSingleDome01 getinstance () { A returninstance; - } -}
2. Lazy Type
1 Packagesingleinstance;2 /**lazy-type, when called to instantiate, save resources, high concurrency thread is not secure*/3 Public classSINGLEDOME02 {4 //private construction methods to prevent direct new objects5 PrivateSingleDome02 () {6 }7 Private StaticSINGLEDOME02 instance;8 //provides getter for external calls to private objects, returning SINGLEDOME02 objects9 Public StaticSingleDome02 getinstance () {Ten if(instance==NULL){ OneInstance =NewSingleDome02 (); A } - returninstance; - } the}
Java 3 single-instance mode