Java design mode serialization (5)-single-State Mode
Name:Singleton
Intent:
Ensure that a class has only one instance and provides a global access point to it.
Applicability:
When a class can only have one instance and the customer can access it from a well-known access point.
This unique instance should be extensible through subclass and customers should not need to changeCodeYou can use an extended instance.
Instance:
1. Construct a singleton king
Package Singleton; public class King {Private Static king King; Private King () {} public static King getinstance () {If (King = NULL) {King = new king () ;}return King ;}}
2. Test class
Package Singleton; public class test {public static void main (string [] ARGs) {King k1 = King. getinstance (); King k2 = King. getinstance (); system. out. println (K1); system. out. println (K2); system. out. println ("whether the same:" + (k1 = k2); system. out. println ("identical:" + (k1.equals (K2 )));}}