2024-07-11
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
When you need to control resource sharing, perform configuration management, and log logging in an application, a common requirement is to have a global access point so that the program can obtain relevant instance information through this global access point no matter where it is, as long as it can be accessed. To meet this requirement, we can use the Singleton Pattern. The Singleton Pattern ensures that a class has only one instance and provides a global access point to access the instance.
Specifically, the singleton pattern usually provides a static method (such as getInstance()) that returns the only instance of the class. Since this method is static, it can be called without creating an instance of the class. This means that any code that has access to the class can get the singleton instance by calling this static method.
This article will introduce the basic concepts and implementation steps of the singleton pattern in detail. Through this article, you will be able to understand how the singleton pattern works and learn how to use it effectively in actual projects.
To give an interesting example, the singleton pattern is like the protagonist's halo in an anime world. No matter how the plot develops, there is only one protagonist, and everyone knows that he is the core of the story. In this way, no matter how the story unfolds, everyone can find the same person to drive the plot.
advantage
Ensuring Single Instance: Avoid creating instances repeatedly and save resources.
Global Access Point: Convenient global access and simplified calling.
Lazy Initialization: Create instances on demand to improve performance.
shortcoming
Difficult to scale: Singleton classes are usually difficult to extend because the constructor is private.
Potential performance issues: In a high-concurrency environment, some implementations may have performance issues.
Testing Difficulty: The singleton pattern can make testing difficult because it is a global state.
According to the characteristics of the singleton pattern, its usage scenarios can be divided into the following:
The implementation of the singleton pattern usually includes three elements:
- Private Constructor, set the class constructor to private so that external instances cannot be created using the new keyword.
- Private static reference points to own instance, create a static instance variable inside the class to hold the only instance.
- Public static method that returns its own instance, providing a static method so that the outside world can obtain the only instance through this method.
For the hungry singleton pattern, the singleton instance is constructed when the class is loaded, which is thread-safe because a static object is created when the class is loaded, and the response speed is fast when it is called. The disadvantage is also obvious, the resource efficiency is not high, as long as other static methods of the class are executed or the class is loaded, the instance will still be initialized.
/**
* 饿汉单例模式:在还没有实例化的时候就初始化
*/
public class Hungry {
//1. 开始时就创建实例
private static final Hungry instance=new Hungry();
// 2. 私有化的构造方法
private void hungry() {
}
public static Hungry getInstance() {
// 返回单例名
return instance;
}
}