- Spring 5 Design Patterns
- Dinesh Rajput
- 243字
- 2021-07-08 09:59:32
Singleton design pattern
The Singleton pattern is a creational design pattern, it is one of the simplest design patterns in Java. According to the singleton design pattern, the class provides the same single object for each call--that is, it is restricting the instantiation of a class to one object and provides a global point of access to that class. So the class is responsible for creating an object and also ensures that only a single object should be created for each client call for this object. This class doesn't allow a direct instantiation of an object of this class. It allows you to get an object instance only by an exposed static method.
This is useful when exactly one object is needed to coordinate actions across the system. You can create a single pattern using two forms, as listed here:
- Early instantiation: Creation of instance at load time
- Lazy instantiation: Creation of instance when required
Benefits of the Singleton pattern:
- It provides controller access to crucial (usually heavy object) classes, such as the connection class for DB and the SessionFactory class in hibernate
- It saves heaps of memory
- It is a very efficient design for multithreaded environments
- It is more flexible because the class controls the instantiation process, and the class has the flexibility to change the instantiation process
- It has low latency
- Spring 5.0 Microservices(Second Edition)
- Android Jetpack開發:原理解析與應用實戰
- 摩登創客:與智能手機和平板電腦共舞
- MongoDB for Java Developers
- Python機器學習實戰
- Mastering KnockoutJS
- Machine Learning With Go
- Access 2010數據庫應用技術實驗指導與習題選解(第2版)
- Java程序設計入門(第2版)
- AngularJS UI Development
- JavaScript設計模式與開發實踐
- Java程序設計(項目教學版)
- Game Programming using Qt 5 Beginner's Guide
- PHP程序設計高級教程
- Java算法從菜鳥到達人