Practical application of Singleton Mode

Source: Internet
Author: User

Practical application of Singleton Mode
From the perspective of face object: although they can achieve their goals, they are object-based and object-oriented, just as we can solve problems without face object, the code of the Surface 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. In terms of functionality: In general, the singleton mode and static method can solve this problem. It is in Object-Oriented consideration and the singleton mode is preferred. 1. When spring starts, it loads and copies the 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) ;}} copy code 2. In Singleton mode, copy the code public class Config {private static Config instance = new rConfig (); private Map <String, St Ring> info = new HashMap <String, String> (); // obtain the public static Config getInstance () {return instance ;}// control the private Config () of the unique instance () {} public void init () throws Exception {info. put ("aa", "bb");} public Map <String, String> getUInfo () {return info ;}} the singleton mode was literally known before the code was copied, I have never actually used it. I only know how to obtain a unique instance, but I am not sure how to perform other operations in this instance. Now I understand that o (∩ _ ∩) o haha

Related Article

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.