Where exactly the Singleton Pattern is used in real application?

前端 未结 12 507
囚心锁ツ
囚心锁ツ 2021-01-30 06:35

I was just curious where exactly the singleton pattern is used... I know how the pattern works and where it can be used but i personally never used in any real application. Can

12条回答
  •  清歌不尽
    2021-01-30 07:19

    Singleton pattern is generally useful when the object that is created once and shared across different threads/Applications. Consider the case that you are implementing the properties load class for a printer.

    Now Printers can be of variant properties. For eg:- Mono Printer, Color Printer, Automatic Scanner Support Printer and so on...

    Every time on boot this config file has to load to enable a few buttons/ applications on the UI say tab or any Printer UI.

    The value of the supported features are stored in form of a config table like say 1 if feature supported and 0 if not supported.

    Based on the supported features we enable disable certain functionalities and application on the UI. Now this config file location in case of printers manufactured by a single company are always stored at a fixed path.

    The file values would change/would be read only in the following scenarios:- 1. On Boot. 2. on Addition or deletion of any new hardware peripheral.

    We can use a singleton class to implement the reading of configuration files. As the same values i.e. the config does not change on UI intervention and can be changed only on hardware intervention.

    This is one example I can think of where we can implement Singleton design pattern.

提交回复
热议问题