singleton

Why is this Python Borg / Singleton pattern working

折月煮酒 提交于 2019-12-09 07:55:12
问题 i just stumbled around the net and found these interesting code snipped: http://code.activestate.com/recipes/66531/ class Borg: __shared_state = {} def __init__(self): self.__dict__ = self.__shared_state # and whatever else you want in your class -- that's all! I understand what a singleton is but i don't understand that particular code snipped. Could you explain me how/where "__shared_state" is even changed at all? I tried it in ipython: In [1]: class Borg: ...: __shared_state = {} ...: def

Need advice on OOP philosophy

穿精又带淫゛_ 提交于 2019-12-09 07:03:11
问题 I'm trying to get the wheels turning on a large project in C#. My previous experience is in Delphi, where by default every form was created at applicaton startup and form references where held in (gasp) global variables. So I'm trying to adapt my thinking to a 100% object oriented environment, and my head is spinning just a little. My app will have a large collection of classes Most of these classes will only really need one instance. So I was thinking: static classes. I'm not really sure why

Singleton in Conjunction with the Factory Pattern in PHP5

北战南征 提交于 2019-12-09 06:46:26
问题 What is the best method for using singleton design pattern in conjunction with the factory method pattern in PHP5? My simplest usage scenario for this is instantiation selective database connection only once for each database type. 回答1: singleton factory for DB connection: class Registry { private static $_objects; public static function set($key, $object) { if (!array_key_exists($key, self::$_objects)) self::$_objects[$key] = $object; } public static function get($key) { if (array_key_exists

Global instances of class

好久不见. 提交于 2019-12-09 00:18:09
问题 Still trying to get to know C# (Mostly worked with C). I have a class "Device" and would like to create an instance of the class, but would also like access to the instances globally because I use them so much in my GUI methods. public class Device { public string Name; public List<string> models = new List<string>(); public List<string> revisions = new List<string>(); ... } Somehow declare this globally: Device Device1 = new Device(); Device1.Name = "Device1"; Then access it later in a WPF

Java - is there any reason to check if a singleton is null twice?

六眼飞鱼酱① 提交于 2019-12-08 22:27:11
问题 I have come across some code, where the developer is constantly checking if the singleton is null twice with a nested if - like in the code below: private static processManager singleton = null; ... public synchronized static processManager getInsatnce() throws Exception { if(singleton == null) { if(singleton == null){ singleton = new processManager(); } } return singleton } I cannot see any reason why this might be, but there are numerous instances in the code, so thought there might be a

can some one confirm if this is a thread safe implementation of singleton

岁酱吖の 提交于 2019-12-08 18:35:30
#include "iostream" #include "atomic" using namespace std; class Singleton { Singleton(); static Singleton * _pInstance; public: ~Singleton() { } static Singleton* getInstance() { Singleton * tmp = _pInstance; atomic_thread_fence(std::memory_order_acquire); if (tmp == nullptr){ tmp = _pInstance; if (!tmp) { _pInstance = new Singleton(); atomic_thread_fence(std::memory_order_release); _pInstance = tmp; } return _pInstance; } }; Singleton* Singleton::_pInstance = nullptr; Your implementation seems to be thread safe, but the simplest way to make a thread safe singleton looks like class Singleton

Singletons to facilate unit tests in a legacy code base. A good idea or not?

自作多情 提交于 2019-12-08 18:09:37
问题 Folks, I've a large legacy .Net code base, and I'm trying to introduce Unit Testing to the team. They're good guys but this is all new to them (to be honest it's reasonably new to me too). One of the problems is the code base makes heavy use of static classes in System.IO, there are extensive in-house libraries of static classes and classes aren't written to interfaces (unless there's an actual design reason to do so). I'm developing an ease-it-in strategy using NUnit and FakeItEasy. To

Benefits of Log4j singleton wrapper?

纵饮孤独 提交于 2019-12-08 17:11:26
问题 I have recently inherited some Java code and need to integrate it into a project that I am working on. My project is a service agent that processes and transforms XML messages. While looking through the new code, I discovered the following logging class: import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class MyLogger { private static MyLogger instance = null; protected final static Logger log = Logger.getLogger(MyLogger.class);

Getting Spring Application context from a non bean object without using Singleton

前提是你 提交于 2019-12-08 16:09:39
问题 I need to get the spring application context from a non bean object. In another thread in SO, the accepted answer suggests to use singleton to get the application context. Getting Spring Application Context But using singleton makes my code more coupled and less testable, the usual problems discussed in many threads (e.g. What is so bad about Singletons ) The question, is there an elegant way to get the application context from a non bean object without using singleton? 回答1: There's always

Singletons in Swift 5

梦想与她 提交于 2019-12-08 15:33:38
问题 Here's precisely how I make a singleton, public class Model { static let shared = Model() // For ocd friends. Add this line: private init() {} func test()->Double { return 3.33 } } then elsewhere... class ViewController:UIViewController { override func viewDidLoad() { super.viewDidLoad() print("Holy singleton test, Batman! \( Model.shared.test() )") } } What about in Swift 5? Any new dramas or insights? Have they perhaps added "actual" singletons, or? 回答1: Nothing new. It remains the same in