What is the difference between a direct static method call and a singleton pattern?

Source: Internet
Author: User
Title, the static method is initialized only once in memory, and Singleton mode is also guaranteed to instantiate only one object in memory, what is the difference between the two? I'm just going to put the database link in the static, okay?

Reply content:

Title, the static method is initialized only once in memory, and Singleton mode is also guaranteed to instantiate only one object in memory, what is the difference between the two? I'm just going to put the database link in the static, okay?

One can inherit, one cannot inherit.

Actually did not understand the main question to ask what.

Singleton mode is for classes, static methods are methods, and objects are not directly linked.

Of course different, think about a bit, static method can use this?

Actually, you need to see exactly what the class is for.

Take the main database connection as an example (in fact many entities can be used as an example), depending on what is considered as an entity to analyze the design:

For example, when we think of a Web site as an entity, the connection to the database as an action of the site is to implement that static method in the site class. However, in order to save memory, we think that this static method is loaded during the initialization of the program, thus saving memory, in fact, we use the connection to the database is still called this method, still need to reconnect once, in memory is still the duplication of the connection resources.

If we think of the database as an entity, then the connection to the database is of course done by this entity, but in order to save memory, I want all of the database entities to use only that existing instance. Instead of creating a connection every time, the singleton mode plays a role.

It is of course to see what the designer is applying to the case of the entity.

To improve the problem of the first example I mentioned, we can write a static variable inside the class to store the connection resource:

class website {    private static $_conn = NULL;        public static function db_conn()    {        if (empty(self::$_conn))        {            $_conn = ...;//造一个连接        }        return $_conn;    }}

This equates to the case where a similar singleton pattern keeps only one resource.

Said so much, in fact, in the use of a single case mode is convenient, at least we can put the various operations of the database into a very useful tool class, when needed to load, how good.

I'm talking about it in memory.

Static maintenance has at least one copy, and a single case is maintained at most and only one copy is stored
Memory is limited, if all the singleton are replaced with static, in principle there is no problem, but the memory will explode.

The singleton will only occupy memory (or stacks, for example) when it is used for the first time.

The singleton pattern is for class instances, and static methods are methods that belong to classes. Static data members and Singleton have only one-time characteristics, you can use static members to implement the 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.