This article is a mirror article of machine translation, please click here to jump to the original article.

View: 15843|Reply: 0

[Source] Analysis of static methods and singleton patterns

[Copy link]
Posted on 10/10/2019 6:15:05 PM | | |
We often have such confusion in programming, some functions are solved using singletons, and some functions are solved using static methods, so what are the principles and usage scenarios of static methods and singletons? Let's make this matter clear today.

1. Let's first look at the difference between static methods and non-static methods?

        Many programmers have this understanding that static methods load first, non-static methods load later, static methods will always exist in memory, and non-static methods will not. Therefore, it is recommended to use a non-static method. First of all, it is wrong to emphasize that this understanding is wrong.

        First of all, let's analyze from memory, when the application is initialized, the CLR (CLR is the Common Language Runtime) is also a runtime environment like the Java virtual machine, which is responsible for resource management (memory allocation and garbage collection, etc.) and ensures the necessary separation between the application and the underlying operating system. The CLR has two different translation names: the Common Language Runtime and the Common Language Runtime. Allocates an address space to each process in the available space of the process, which is the managed heap. The managed heap is divided into multiple regions, the most important of which are the garbage collection heap (GC Heap) and the loader heap (Loader Heap), which is used to store object instances and is managed by the GC. The most important information that Loader Heap stores through the MethodTable table is metadata-related information, such as base types, static fields, implementation interfaces, and all methods. Loader Heap does not accept GC control, and its life cycle is from creation to destruction. In other words, once a class is loaded, both static and non-static methods of this class are stored in the MethodTab table of Load Heap, without GC control, and they are all resident in memory for the first load.

        What is the difference between a static method and a non-static method? The difference is that when creating an object, the static method only has a copy, while the non-static method will copy a copy of the information about this instance on GC Heap for each new new object, and at the same time put the new object on the stack. The address pointed to by the stack pointer is the memory address that was just copied to GC Heap. Therefore, in terms of method call speed, static methods are faster because non-static methods need to be instantiated and allocate memory.

From the perspective of programming history, almost all methods of early structured programming were static methods, and the introduction of instantiation methods was a matter of object-oriented programming, so the instantiation method was not to solve the problem of running efficiency and memory. It is to make development more patterned and object-oriented.

        From the above analysis, a conclusion can be drawn: 1. The static method and the non-static method are the distinction between the solution mode. 2. If inheritance, polymorphism, or if a method has nothing to do with the object of its class, you should choose a static method, such as a tool class.        

2. What is the difference between singleton mode and static method?

        We sometimes need to maintain a piece of information in engineering, such as some configuration attributes that are loaded at runtime, which must exist throughout the life of the application and are public, so only one copy is needed. At this time, we will consider using a singleton or static method to maintain this data, but at this time, this data is obtained in an object-oriented way, we will use a singleton.

        First, static methods are class-based, and singletons are object-based. If the resolution pattern is object-based, use a singleton, otherwise use a static approach. For example, you need to inherit classes, implement interfaces, delay initialization, rewrite parent classes, etc. to use singletons, second, static methods are process-oriented, and singletons are object-oriented. Third, static attribute variables will not be cleared by GC, so the singleton objects will not be cleared by GC, and objects generated in static methods will be released when the static method is executed.




Previous:Distributed Messaging Middleware Practice (by Ni Wei) Full Chinese PDF
Next:Dell Latitude 5501 laptop shutdown and auto power on issue resolved
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com