singleton

Service instantiated twice after APP_INITIALIZER

送分小仙女□ 提交于 2021-01-23 06:22:13
问题 Problem is: I need to make an http call and store an object that is needed for generate dynamic routes. So, I was taking advantage of the APP_INITIALIZER. // app.module.ts import { ApplicationService } from './application.service'; providers: [ ApplicationService, { provide: APP_INITIALIZER, useFactory: appServiceFactory, deps: [Injector, ApplicationService], multi: true }, ], function appServiceFactory(injector: Injector, appService: ApplicationService): Function { return () => { return

Service instantiated twice after APP_INITIALIZER

亡梦爱人 提交于 2021-01-23 06:22:13
问题 Problem is: I need to make an http call and store an object that is needed for generate dynamic routes. So, I was taking advantage of the APP_INITIALIZER. // app.module.ts import { ApplicationService } from './application.service'; providers: [ ApplicationService, { provide: APP_INITIALIZER, useFactory: appServiceFactory, deps: [Injector, ApplicationService], multi: true }, ], function appServiceFactory(injector: Injector, appService: ApplicationService): Function { return () => { return

C++ templated class implementation of the multiton pattern

拥有回忆 提交于 2020-12-29 07:53:50
问题 I implemented the multiton pattern using a templated class in C++. #ifndef MULTITON_H #define MULTITON_H #include <map> template <typename Key, typename T> class Multiton { public: static void destroy() { for (typename std::map<Key, T*>::iterator it = instances.begin(); it != instances.end(); ++it) { delete (*it).second; } } static T& getRef(const Key& key) { typename std::map<Key, T*>::iterator it = instances.find(key); if (it != instances.end()) { return *(T*)(it->second); } T* instance =

PHP singleton database connection pattern

混江龙づ霸主 提交于 2020-12-23 01:51:07
问题 I've been working on a small project using PHP and MySQL. I've read a lot around about best practices on managing a connection and so on. I've also implemented (following some posts found around) a singleton class to manage MySQL connections. require_once 'config.inc.php'; class DbConn { private static $instance; private $dbConn; private function __construct() {} /** * * @return DbConn */ private static function getInstance(){ if (self::$instance == null){ $className = __CLASS__; self::

python Singleton 模式

筅森魡賤 提交于 2020-12-17 15:36:16
Singleton 模式,中文既为 单实例模式 。 一、代码示例 看一下普通的类: class Normal(object): _instance = [] config = '' def set(self,value): self.config = value def get(self): return self.confg if __name__ == '__main__': s1 = Normal () s1.set('s1') s2 = Normal() s2.set('s2') print id(s1) print id(s2) print s1.get() print s2.get() 运行结果 : ubuntu@yee:/tmp$ python t.py 139782948272976 139782948273040 s1 s2 可以看到,代码中生成了两个实例,每个实例都拥有自己的内存空间。 接下来看单实例的代码示例: class Singleton(object): _instance = [] config = '' def __new__(cls,*args,**kwargs): if not cls._instance: cls._instance = super(Singleton,cls).__new__(cls,*args,**kwargs)

Why enum singleton is lazy?

杀马特。学长 韩版系。学妹 提交于 2020-11-29 11:11:36
问题 I saw answers like these, tried to clarify via comments, and was unsatisfied by examples here. Maybe it's time for this specific question... Why enum singleton implementation is called lazy ? public enum EnumLazySingleton { INSTANCE; EnumLazySingleton() { System.out.println("constructing: " + this); } public static void touchClass() {} } How it is different from eager implementation? public class BasicEagerSingleton { private static final BasicEagerSingleton instance = new BasicEagerSingleton

Why enum singleton is lazy?

生来就可爱ヽ(ⅴ<●) 提交于 2020-11-29 11:09:34
问题 I saw answers like these, tried to clarify via comments, and was unsatisfied by examples here. Maybe it's time for this specific question... Why enum singleton implementation is called lazy ? public enum EnumLazySingleton { INSTANCE; EnumLazySingleton() { System.out.println("constructing: " + this); } public static void touchClass() {} } How it is different from eager implementation? public class BasicEagerSingleton { private static final BasicEagerSingleton instance = new BasicEagerSingleton