Practical application of Singleton Mode

Source: Internet
Author: User
The project needs to load data to the memory when the program starts, so static or Singleton mode is used.
How to choose?
Why is the singleton mode not static?

From the perspective of the face object:
Although they can achieve their goals, they are object-based and object-oriented. Just like we can solve problems without looking at objects, the code of the object provides a better programming idea.
If a method has nothing to do with the instance object of its class, it should be static, and vice versa, it should be non-static. If we do need to use a non-static method, but we only need to maintain an instance when creating a class, we need to use the singleton mode.
For example, when the system is running, we need to load some configurations and attributes. These configurations and attributes must exist and are public and must exist throughout the lifecycle, so you only need one copy. At this time, if you need a new one and assign another value to the new one, it is obviously a waste of memory and it makes no sense to assign another value, therefore, we need to maintain one copy in the singleton mode or static method, but these configurations and attributes are obtained through object-oriented coding, we should use the singleton mode or not object-oriented, but its own attributes should be oriented to objects. Although we can solve the problem using static methods, however, the best solution is to use the singleton mode.
Function:
In Singleton mode, you can control the number of Singleton instances, perform meaningful derivation, and create instances in a more flexible manner.

In general, the singleton mode and the static method can solve this problem. It is in Object-Oriented consideration and the singleton mode is preferred.

1. loading at Spring startup
Public class userconfiglistener extends contextloaderlistener {protected final log logger = logfactory. getlog (getclass (); Public void contextinitialized (servletcontextevent event) {try {config. getinstance (). init ();} catch (exception e) {logger. error ("initialization information error:", e );}}}
2. Singleton Mode
Public class config {private static config instance = new rconfig (); Private Map <string, string> info = new hashmap <string, string> (); // obtain the public static config getinstance () {return instance;} // control the private config () {} public void Init () throws exception {info. put ("AA", "BB");} public Map <string, string> getuinfo () {return info ;}}
In the past, we only knew the singleton mode literally and never actually used it. We only knew how to obtain a unique instance, but we did not know how to perform other operations in this instance. Now I understand that O (∩ _ ∩) O haha
 

Practical application of Singleton Mode

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.