In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to achieve the Singleton singleton pattern", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve the Singleton singleton pattern" article.
Singleton (singleton mode)
Singleton (singleton pattern) is a creative mode, which provides a way to obtain objects to ensure that it is unique in a certain range.
Intent: make sure that a class has only one instance and provide a global access point to it.
In fact, the singleton model is not obvious at the front end for the following reasons:
The front-end code itself runs on a stand-alone, and any variables created are naturally distributed and do not need to worry about affecting another user.
The back-end code is one-to-many, and it is important to distinguish which resources are shared between requests and which are unique within the request.
In addition, when we talk about a singleton, it implies a scope, which means that the scope of a singleton is different in different scenarios, such as in a context, in a room, or in a process, or in a thread.
As an example
If you do not understand the above intention introduction, it does not matter, the design pattern needs to be used in daily work, combined with examples can deepen your understanding, below I have prepared three examples to let you experience in what scenarios this design pattern will be used.
Shared items for multiplayer games
Students who have played the game know that the public goods we use in each game are unique in the current room, but they are not unique in the game room, so there must be different categories to describe these public goods. then how to get public goods in each game can be guaranteed to be the only one in the current bureau?
Redux data flow
In fact, the front-end Redux data flow itself is a singleton mode. In an application, the data is unique, but different UI can use this unique data, or even display a table component in two different places, such as full-screen mode, but the data is still one. We do not need to send it another fetch request in order to display the table full screen. You can share a data with the original table.
Database connection pool
Every SQL query relies on database connection pooling. If database connection pooling is established once for each query, the speed of establishing connections will be much slower than that of SQL queries, so how do you design the acquisition method of database connection pooling?
Intention interpretation
The intention of the singleton pattern is simple, almost literally:
Intent: make sure that a class has only one instance and provide a global access point to it.
For shared items in multiplayer games, such as a pot, to be unique within a game, provide a way to access the unique instance.
The connect decorator for Redux data streams is a design of global access points.
Database connection pooling can be initialized in advance and this unique instance is provided through a fixed API.
Structure diagram
Singleton is the interface of the singleton mode, and customers can only access the instance through its defined instance () to guarantee the singleton.
Code example
The following example is written in typescript.
Class Ball {
Private _ instance = undefined
/ / if the constructor is declared as private, the new Ball () behavior can be blocked.
Private constructor () {}
Public static instance = () = > {
If (this._instance = undefined) {
This._instance = new Ball ()
}
Return this._instance
}
}
/ / use
Const ball = Ball.getInstance ()
If you think about it, why does this example write the singleton as a static method instead of a global variable? In fact, global variables can also solve the problem, but because it will pollute the overall situation, it should be solved in a modular way as much as possible. The above example is a better way of encapsulation.
Of course, this is only the simplest example, but in fact there are several modes for the singleton pattern:
Hungry Han style
An instance is generated at initialization so that it can be obtained directly when called.
Lazy style
It is written in the code example, instantiating on demand, that is, instantiating when it is called.
Note that demand is not necessarily a good thing. If New is expensive and instantiated on demand, it may leave the risk of system exception to a random trigger time, making it difficult to troubleshoot BUG. In addition, it will also affect the time consuming of the system during the first instantiation.
For JAVA, the singleton also needs to consider concurrency, which can be solved by double detection, static inner class, enumeration and so on.
Malpractice
The problems with the singleton model are:
Not very friendly to object-oriented. It is not friendly to encapsulation, inheritance and polymorphic support.
It is not conducive to sort out the dependency relationship between classes. After all, the singleton is called directly, not declared in the constructor, so you have to look at each line of code to determine the relationship.
The extensibility is not good. If you want to support multiple instances, it is difficult to expand. For example, the global data flow may be changed to multiple instances because of the micro-front-end solution, and the database connection pool may be changed to multiple instances in order to divide and conquer SQL. At the beginning of the system design, it is necessary to consider whether the singleton will be maintained in the future.
Testability is not good because singletons are globally shared and cannot guarantee isolation between test cases.
Cannot use constructor to pass parameters.
In addition, the singleton pattern can also be replaced by the factory method, so you don't have to worry about a design pattern, you can use it in combination, and the factory function can embed the singleton pattern.
The above is about the content of this article on "how to achieve the Singleton singleton mode". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.