I. Singleton – Creational Patterns.
- Singleton is no longer strange to programmers, is the design pattern that everyone remembers first when it comes to design patterns. Almost 80% of the programmer’s questions are for Singleton when asking about Design Patterns.
- True to its name, Singleton is a single instance of an object type, certainly there is no second such object in our entire program. At the first call, the instance will be initialized and
reusefor all subsequent uses in the program.
II. When to use Singleton?
Then when we should use Singleton, I will list a few cases where we often use Singleton:
- When we want to take advantage of a single connection to the database for each different query.
- When we need to open a
Secure Shell(SSH) connection to the server to do some work, and we don’t want to reopen this connection when doing each of those jobs.
- We need to limit the number of accesses to a variable, or a memory area, then the Singleton acts as a way(s) by which we limit the number of concurrent accesses (commonly known as the Connection Pool). Design Patterns). With Golang, we can use
channelsto handle this.
Also very applicative of the Singleton pattern, I just listed a few things above ^^.
III. Practical examples
- We have a
function Printthat is executed when the user clicks
In đơn hàngbutton and a Counter object to record the number of Clicks of the User during the lifetime. Then we have to always make sure that the Counter object is always unique, then the Singleton will be useful to us (but there are still many ways that don’t use the Singleton that can still be done, but it will probably be more messy)
- To function properly, the Counter object above needs to adhere to the following rules:
1. When no Counter object has been initialized before, Counter will be initialized with initial value of 0.
2. In case the Counter is already initialized, return that Counter instance.
3. If we trigger the
I will create a simple program to implement the Singleton Counter in the above example, starting with the following project structure:
- File main.go (entry point)
- Package Singleton: implement coutner singleton
- File go.mod
The code is also short, so I will source the entire source in the image below:
- In the counter.go file, we define a private struct that is a
singletonthat includes a counter field of type int. Struct singleton includes 2 methods Increase and Get.
- Because it is a private struct, we cannot access / initialize this
singletonobject directly from the outside, but must go through the public function
GetInstance, this function is responsible for checking whether the singleton instance has been declared before. . Otherwise, it will initialize a new object, otherwise return the current object.
- Through the main.go file here I have a few comments. Simply understand that when we want to increase the value of the counter variable or get the value from it, we must pass GetInstance, this ensures that we always use the right Singleton object in the whole program.
- Run the program with the command:
go run main.goand the results give us:
Above, I have explained to you about Singleton, the most common Design Pattern for developers. The above
Counter is just a small example, for larger, more complex programs, initializing an object will sometimes undergo a complex computation, or consume a lot of resources and time. , we will see the importance of Singleton more clearly.
In this article, I only limit myself to the most basic way of implementing Singleton in Golang, there are many issues to pay attention to when implementing Singleton, such as
Not Thread Safe or
Thread Safe . Hope to see you in the next chapters.
Thank you for reading ^^
Source code: https://github.com/khaaleoo/golang-design-patterns
- Go Design Patterns (Mario Castro Contreras)