Design pattern – singleton pattern

Design pattern

It is a summary of the experience of classification, cataloging and code design that is repeatedly used and known by most people.

objective

Make the code easier for others to understand and ensure the reliability of the code.   

Singleton mode:

Application: we only need one object, such as configuration file, tool class, thread pool, cache, log object, etc. If multiple instances are created, it will lead to many problems, such as excessive resource occupation, inconsistent results and so on.

Function: ensure that there is only one instance in the whole application

Type: hungry man mode, lazy man mode

Difference:

Hungry man mode is characterized by slow class loading, but fast object acquisition at runtime. Thread safety

Lazy mode is characterized by fast class loading and slow object acquisition in single run time. Thread unsafe

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>