JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

Things about the singleton design pattern

Author:JIYIK Last Updated:2025/03/19 Views:

The singleton design pattern is one of the most commonly used design patterns. The singleton design pattern, just by its name, you can roughly know its meaning. Single means one; instance means instance object. So a singleton has only one instantiated object. Therefore, we can define the singleton design pattern as follows:

The singleton design pattern is used to restrict a particular object to be created only once and provide shared access to itself to the entire system.

The singleton design pattern generally only allows one instance of itself to be created, so when it is first instantiated, an object is created and stored inside itself. When the object is used next, the object will first check the storage to see if the object has been instantiated. If it already has its own instance, it returns a reference, otherwise it creates an object of its own as described above and stores it inside itself for use by requests.

The principle of the singleton design pattern can be said to be what is mentioned above, and it can be said that it is independent of the programming language. However, due to the differences in the syntax of different programming languages, the implementation of the singleton design pattern is also different. Let's take Java and PHP as examples to illustrate.

For Java, the singleton design pattern can be divided into lazy and hungry styles according to the time when the object is instantiated.

The so-called hungry singleton means that when the singleton class is loaded, it instantiates an object of itself and stores it inside itself.

The code is as follows:

public class Singleton{
         private static Singleton singleton = new Singleton();
         private Singleton(){}
         public static Singleton __Instance(){
                   return singleton;
         }       
}

From the above code, we can see that when the class starts loading, a private static variable singleton is declared, and its own object is instantiated and assigned to the variable singleton. This is the so-called hungry singleton.

The so-called lazy singleton means that it will only instantiate its own object and store it inside itself when the method that generates the instance object is called.

The code is as follows:

public class Singleton {
    private static Singleton singleton;
    private Singleton(){}
    public static synchronized Singleton __Instance(){
        if(singleton==null){
            singleton = new Singleton();
        }
        return singleton;
    }
}

From the above code, we can see that only when the __Instance() method is called, it will first determine whether the variable singleton is empty. If it is not empty, it means that the instantiated object has been created. Otherwise, the instantiated object is created and assigned to the variable singleton.

The above is Java code. Because Java is a compiled language, it supports instantiating the object when the class is loaded.

But for PHP, PHP is an interpreted language. If the class is not loaded, it cannot instantiate the object of the class. Therefore, PHP does not support hungry singletons, which means that the following code has errors and cannot pass.

class Singleton{
         private static $singleton = new self;
         private function __construct(){}
         public static function __Instance(){
                   return self::$singleton;
         }
}

Therefore, PHP only supports lazy singletons, the code is as follows:

class Singleton{
         private static $singleton;
         private function __construct(){}
         public static function __Instance(){
                   if(!self::$singleton instanceof self){
                         self::$singleton = new self;
                   }
                   return self::$singleton;
         }       
}

The most common scenario for using the singleton design pattern is to create a database connection object. The database access object is responsible for creating a database connection instance. Whenever a specific method of the object is called, it will use the connection resource it has already created. It is well known that the cost of creating a database connection resource is very high (it takes a lot of time and resources). Therefore, when designing the code, you should create as few database connection resources as possible.

In most cases, after the data in the database is retrieved, there is no need to keep the database connection resources and they can be released. In view of this situation, it makes sense to make the database access object a singleton. Every time a new data operation requires a new database access object, the previous object will be reused, and there is no need to instantiate a new database access object to establish additional database connection resources.

Therefore, we can know that using the singleton design pattern can save memory space, avoid repeated creation and destruction of objects, improve performance, and so on.

Because the singleton design pattern is often used to connect to the database, I wrote a class for operating the database, and tried to use the singleton design pattern when designing the class.

The source code is hosted on github , and there is a brief introduction to the use of the class functions. Everyone is welcome to download it.

 

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

C# 中的单例类

Publish Date:2024/02/03 Views:126 Category:编程语言

在应用程序的整个生命周期中只有一个实例的类在 C# 中称为单例类。

Java 单例类

Publish Date:2023/10/10 Views:193 Category:Java

本教程介绍 Java 单例类。它还演示了为什么它很重要,如何使用不同的设计模式编写 Java 单例类。本文将在定义级别讨论单例类并强调其重要性。我们将描述编写 Java 单例类的各种设计模式,并

在 Kotlin 中创建单例

Publish Date:2023/05/13 Views:190 Category:Java

设计模式是一组基于手头需求的常见问题的解决方案,这些解决方案可以在试图解决相同问题的其他系统中重复使用。使用 Kotlin API 创建单例

如何在 Java 中使用复合设计模式?

Publish Date:2023/02/09 Views:174 Category:算法

你好 Java 程序员,如果大家想了解 Java 中的 复合设计模式 ,例如如何实现复合设计模式以及何时使用它,那么来对地方了。 在本文中,我们将讨论 复合设计模式 。 复合设计模式(

Java 中的观察者设计模式

Publish Date:2023/02/09 Views:97 Category:算法

Java 中的 观察者设计模式 是一种基本的核心 Java 模式,其中 Observe 监视 Subject 的状态或属性的任何变化。 例如,公司向所有股东更新他们在这里做出的任何决定公司是主体,股东是观

Java 中的适配器 vs 装饰器 vs 门面 vs 代理设计模式

Publish Date:2023/02/09 Views:262 Category:算法

Adapter(适配器)、Decorator(装饰器)、Facade(门面) 和 Proxy(代理) 设计模式之间有一些惊人的相似之处,因为它们都使用组合和委托来解决问题。 适配器模式 包装一个接口,并将

如何在 Java 中使用适配器设计模式

Publish Date:2023/02/08 Views:162 Category:算法

Java 中的 适配器设计模式 ,也称为 Wrapper 模式,是另一个非常有用的 GOF 模式,它有助于弥合 Java 中两个类之间的差距。 根据四人组模式列表,适配器是一种结构模式,很像 Java 中的代

如何在 Java 中使用状态设计模式?

Publish Date:2023/02/08 Views:137 Category:算法

状态(State)设计模式 是一种行为模式。 状态模式 看起来类似于策略模式,但它有助于管理对象状态,从而使它们在不同状态下表现不同。 在这个例子中,我们将采用一个著名的面向对

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial