1. The nature of the singleton pattern:
Number of control instances ( to conserve resources )
2. Some ideas embodied in the Singleton model:
(1) Delay load (lazy load): lazy Type
(2) Cache: a hungry man type
3. The variant of the singleton mode is used :
The number of control usages is 3 (instance pool):
1 Public classPerson {2 //define a container to cache data3 Private StaticMap map =NewHashMap ();4 //used to record which instance is currently in use, initialized with a value of 15 Private Static intnum = 1;6 //The total number of instances used to record the need for control is 37 Private Static intCount = 3;8 9 //Privatization of the person construction methodTen PrivatePerson () {} One Public StaticPerson getinstance () { A //1. Find the data you want to use in the cache -Person person =(person) map.get (num); - //1.1 If you can't find the the if(Person = =NULL) { - //Create a new data (or get a data) -person =NewPerson (); - //and then set the new data back into the cache. + Map.put (Num,person); - } + //for each invocation, NUM plus 1 is set; Anum++; at //if num exceeds the total required to be controlled, set NUM to 1 and start repeating the cycle - if(Num >count) { -num = 1; - } - //1.2 If you find it, use it directly. - returnPerson ; in } -}
Java Common design Pattern 09: Single-case mode hardening (number of control instances N)