课程: Go Design Patterns

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Singleton pattern overview

Singleton pattern overview - Go教程

课程: Go Design Patterns

Singleton pattern overview

- [Instructor] Now let's take a look at the Singleton design pattern. There are some scenarios where you want to be able to ensure that there's only one instance of a particular class in your program. The purpose of the Singleton pattern is to restrict the instantiation of a class to a single instance and provide global access to that instance. The Singleton pattern also has other benefits. For example, it allows us to provide what's called lazy initialization of the class. In other words, the class isn't instantiated until it's actually needed. So what are some useful examples where a Singleton object might help? Well, as I mentioned earlier, the Singleton comes in handy when you want to make sure that there's only one instance of a particular class in your application. And some common scenarios where a single object are useful, include things like logging, or application configuration, or telemetry and analytics…

内容