Java Common design Pattern 09: Single-case mode hardening (number of control instances N)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.