singleton

C++: Templates and the singleton pattern

坚强是说给别人听的谎言 提交于 2020-01-01 09:39:29
问题 It happens so that I have a need of the infamous singleton pattern. Better yet, it happens so that I have a need of infamous C++ templates in combination with that pattern. So, what troubles me is this: template <class T> class PDatabaseTable { ... static PDatabaseTable <T> & instance() { static PDatabaseTable <T> singleton; return singleton; } ... }; This is a typical way to implement a singleton that's supposed to be created on the first use. Now, here we have a static variable singleton .

How can I configure Structuremap to auto scan type in Assembly and Cache by Singleton?

元气小坏坏 提交于 2020-01-01 09:37:25
问题 I am using mvc.net with StructureMap to scan and register all repositories and services for me. Now I want to register and cache by Singleton. How can I do? IContainer container = new Container(x => { // Register Repositories and Services x.Scan(y => { y.AssemblyContainingType<SomeRepository>(); y.AssemblyContainingType<SomeService>(); y.IncludeNamespaceContainingType<SomeRepository>(); y.IncludeNamespaceContainingType<SomeService>(); }); // Register Controllers x.Scan(y => { y

Do I need to lock singleton in ASP.NET Core?

邮差的信 提交于 2020-01-01 09:27:08
问题 Here is my code: public class RouteSingleton { private IDictionary<string, string> _dealCatLinks; private IDictionary<string, string> _sectionLinks; private IDictionary<string, string> _categoryLinks; private IDictionary<string, string> _materials; private IDictionary<string, string> _vendors; public RouteSingleton(IDealService dealService , ICategoryService categoryService , IVendorService vendorService) { this._dealCatLinks = dealService.GetDealCatLinks("PLV").Distinct().ToDictionary(x => x

Implement a PHP singleton: static class properties or static method variables?

家住魔仙堡 提交于 2020-01-01 08:03:29
问题 So, I've always implemented a singleton like so: class Singleton { private static $_instance = null; public static function getInstance() { if (self::$_instance === null) self::$_instance = new Singleton(); return self::$_instance; } private function __construct() { } } However, it recently struck me that I could also implement it with member-wise static variables: class Singleton { public static function getInstance() { //oops - can't assign expression here! static $instance = null; // = new

Python singleton pattern

跟風遠走 提交于 2020-01-01 07:03:40
问题 someone can tell me why this is incorrect as a singleton pattern: class preSingleton(object): def __call__(self): return self singleton = preSingleton() # singleton is actually the singleton a = singleton() b = singleton() print a==b a.var_in_a = 100 b.var_in_b = 'hello' print a.var_in_b print b.var_in_a Edit: The above code prints: True hello 100 thank you very much Part Two Maybe this is better? class Singleton(object): def __new__(cls): return cls a = Singleton() b = Singleton() print a ==

Python singleton pattern

杀马特。学长 韩版系。学妹 提交于 2020-01-01 07:03:06
问题 someone can tell me why this is incorrect as a singleton pattern: class preSingleton(object): def __call__(self): return self singleton = preSingleton() # singleton is actually the singleton a = singleton() b = singleton() print a==b a.var_in_a = 100 b.var_in_b = 'hello' print a.var_in_b print b.var_in_a Edit: The above code prints: True hello 100 thank you very much Part Two Maybe this is better? class Singleton(object): def __new__(cls): return cls a = Singleton() b = Singleton() print a ==

Can this Java singleton get rebuilt repeatedly in WebSphere 6?

和自甴很熟 提交于 2020-01-01 04:16:28
问题 I'm trying to track down an issue in our system and the following code worries me. The following occurs in our doPost() method in the primary servlet (names have been changed to protect the guilty): ... if(Single.getInstance().firstTime()){ doPreperations(); } normalResponse(); ... The singleton 'Single' looks like this: private static Single theInstance = new Single(); private Single() { ...load properties... } public static Single getInstance() { return theInstance; } With the way this is

Simple C++ logger by using singleton pattern

 ̄綄美尐妖づ 提交于 2020-01-01 03:16:08
问题 Due to the flooding examples of implementing logger using Singleton pattern, I have just written a simple C++ logger in the same approach for my program. However, since the famous double-checked locking approach is known to be no more thread-safe, I wonder if I should: 1) Forget about the use of Singleton pattern in this case? 2) Continue to use double-checked locking even though it is unsafe? 3) Use the expensive pure sync lock method for every access to its public interfaces? Any

How to correctly make a thread safe Singleton Factory in Java? [duplicate]

允我心安 提交于 2020-01-01 02:35:07
问题 This question already has answers here : What is an efficient way to implement a singleton pattern in Java? [closed] (29 answers) Closed 5 years ago . This is the first time I am writing a Factory class. Below is my Factory class, I am not sure whether this is the correct way of making thread safe Singleton Factory class or not. I will be returning instance of my Client using this factory? public class ClientFactory { private static ClientFactory instance = null; private ClientFactory() { }

Singleton Pattern

和自甴很熟 提交于 2019-12-31 15:51:45
问题 This question, like my previous question, references Effective Java . This time I have quite a few sub-questions. A privileged client can invoke the private constructor reflectively with the aid of the AccessibleObject.setAccessible() method. If you need to defend against this, modify the constructor. How, exactly, can a private constructor be invoked? And what is AccessibleObject.setAccessible() ? What approach do you experts follow with singletons? // Approach A public class Test{ public