官术网_书友最值得收藏!

Singleton design pattern

Ensure a class has only one instance and provide a global point of access to it - GOF Design Patterns

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
主站蜘蛛池模板: 荆州市| 彭山县| 边坝县| 安阳县| 竹溪县| 保德县| 滦南县| 威信县| 济阳县| 河曲县| 翁牛特旗| 民勤县| 射洪县| 正镶白旗| 麻江县| 石狮市| 山阳县| 西华县| 西林县| 临泉县| 淳化县| 大名县| 华容县| 宜川县| 阿拉尔市| 威海市| 郓城县| 岚皋县| 万州区| 长宁县| 宜君县| 曲阜市| 丹阳市| 本溪市| 舟山市| 安吉县| 徐州市| 塔河县| 庆安县| 桓台县| 诸暨市|