Design patterns are tried-and-tested solutions to common problems that arise in software design. They provide a template or guide for solving these problems in a flexible and reusable way.
Each pattern represents a best practice that developers can adapt to their specific context. Design patterns are often classified into three main categories.
To kickstart this series, let's talk about the Singleton pattern.
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance.
This is useful in cases where you need to manage a shared resource, such as database connections or configuration settings.
Often there is the need to ensure that only one instance of a class exists, such as when managing configurations or interacting with hardware resources. Without Singleton, creating multiple instances can lead to issues like inconsistent data or resource locks.
This is very common to happen when working with asynchronous code, where multiple goroutines can create new instances of a class or access shared resources.
Consider a database connection pool: If multiple parts of your application create new connections at the same time, you may end up with redundant or conflicting database interactions. A Singleton ensures only one connection is created and used across the application.
package main import ( "fmt" "sync" ) type Singleton interface { DoSomething() string } type singleton struct{} var lock = &sync.Mutex{} var instance *singleton func NewSingletonInstance() *singleton { if instance == nil { lock.Lock() defer lock.Unlock() if instance == nil { fmt.Println("Creating single instance now.") instance = &singleton{} } else { fmt.Println("Single instance already created.") } } else { fmt.Println("Single instance already created.") } return instance } func (s *singleton) DoSomething() string { return "Doing something." } func main() { instance1 := NewSingletonInstance() instance2 := NewSingletonInstance() fmt.Printf("%p\n", instance1) fmt.Printf("%p\n", instance2) }
The function NewSingletonInstance ensures that only one instance of singleton is created, even when called multiple times.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3