Why use a single case pattern?

Source: Internet
Author: User
Tags garbage collection

The most common pattern we use in programming is the single case pattern, however, the single case pattern is used in any situation. Why not use a static method instead of a single case pattern? To make sense of these problems, we need to start with the difference and connection between static and Non-static methods.

Static method resident memory, Non-static method only when used to allocate memory.

This is generally considered to be the case, and it is wrong to suggest that the static method should use a Non-static method if it takes up too much memory.

Why, start with the memory allocation:

Definition of the managed heap: for 32-bit applications, the CLR assigns a reserved address space in the process's available address space, which is a single area of memory on the available address space in a process (4GB per process), but does not correspond to any physical memory, after the application completes the process initialization. The address space is the managed heap.

The managed heap is divided into multiple zones, the most important of which is the garbage collection heap (GC Heap) and the load heap (Loader Heap), the GC Heap is used to store object instances, and is managed by GC; Loader Heap is divided into high-frequency Heap, Low-frequency Heap and stub Heap, different information is stored on various heaps. The most important information of Loader heap is metadata-related information, which is the type object, each of which is represented as a method table on the Loader heap, and the stored metadata information, such as the base type, Static fields, implemented interfaces, all methods, and so on. Loader Heap is not controlled by GC, and its lifecycle is from creation to AppDomain uninstall. (Excerpt from "You Must know. Net")

Thus we understand that static and non-static methods are actually in the method table in memory, and that when a class is first loaded, it writes static methods, Non-static methods, into the method table in the loader heap, and loader Heap is not subject to GC control, so once loaded, the GC is not recycled until AppDomain unloads

As a result, we also understand that static methods and Non-static methods, they are in the first load after the resident memory, so the method itself in memory, there is no difference, so there is no "static method resident memory, Non-static method only use when allocating memory" this conclusion.

Ii. the difference between static and Non-static methods.

The difference in memory is that when a non-static method creates an instance object, because the value of the property is different for each object, the instance property is copied in the GC heap when an instance is new, and the new object is placed on the stack. The stack pointer points to the memory address of the instance that you just copied. Static methods do not need to, because the static method inside the static field, is stored in the methods table, only one copy.

Therefore, static methods and Non-static methods, at the call speed, the static method speed must be faster, because non-static methods need to instantiate, allocate memory, but static methods do not, but this speed difference can be ignored.

Third, why should there be a non-static method.

Early structured programming, almost all of the methods are "static method", the introduction of the concept of the instantiation method is the object of object-oriented concept, the distinction between static methods and instantiation methods can not only understand the performance, create c++,java,c# Such a master of object-oriented language introduction of the instantiation method must not be to solve what performance, memory problems, but to make the development of a more modular, object-oriented. In this case, the distinction between static and instantiation methods is to solve the problem of patterns.

Then continue to think, if we all use static method, not Non-static method, not the same function can be implemented. Yes, yes, but your code is object-based, not object-oriented, because object-oriented inheritance and polymorphism are non-static methods.

The second reason is why it is not recommended to use static methods, if we have multiple threads, if the static method uses a static field, this static field can be modified by multiple threads, so if you use static variables in a static method, this will be a thread security issue, and of course, even if it's not multithreading, Because there is only one copy of the static field, there are also problems that can be modified elsewhere.

From these three points we have reached the following conclusions:

When to use the static method, when the Non-static method.

Since static methods and instantiation methods are differentiated to solve pattern problems, static methods can be used if we consider the need for inheritance and polymorphism, but even if inheritance and polymorphism are not considered, it is not a good programming idea to use static methods.

From another perspective, if a method has nothing to do with the instance object of his class, it should be static, otherwise it should be non-static. So like the tool class, it's generally static.

Second, why use a single case mode instead of a static method.

From the perspective of the object:

Although all can achieve the goal, but they are based on the object, one is object-oriented, just as we do not face objects can solve the problem, the object-oriented code provides a better programming idea.

If a method has nothing to do with the instance object of his class, then it should be static, whereas otherwise he should be non-static. If we really should use non-static methods, but when we create a class and really need to maintain only one instance, we need to use a singleton pattern.

For example, when we run the system, you need to load a few configurations and attributes, which must exist and be public, and need to exist throughout the lifecycle, so just one copy is required, and this time, if I need it again, give him a new one and assign a value. It's obviously a waste of memory and there's no point in assigning it again, so at this point we need either a single pattern or a static method to maintain one copy, but at this point these configurations and attributes are obtained by object-oriented encoding, we should use a single case pattern, or not object oriented, But his own attributes should be to face the object, we use static method although can solve the problem, but the best solution should also be to use the single case mode.

Functionally speaking: Single case mode can control the number of single cases, can make meaningful derivation, have more free control to the creation of instance;

Third, Other:

Database connection can do singleton.

If it is simple to hold a connection object in a single instance of the object, this is wrong, so there are multiple links in the pool can be used, if the use of Singleton, in the Web Access, you can only use a database link, it is not dead miserably.

But the link pool can use the single example mode, the initialization of the time to create such as 100 connection objects, and then the need to provide one, after the return to pool, we use the single case mode, is to ensure that the connection pool has and only one.

For another example, for example, the DAL layer to write a call to the database table class, in the BLL layer when the application of this class, if the new creation every time need to create and recycle frequently, and the DAL layer in this class and object-related value variables, so do not need each time new one, In this case, you can create the DAL instance in singleton mode.


Http://www.cnblogs.com/seesea125/archive/2012/04/05/2433463.html

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.