singleton

Why does PHP create a new instance of a singleton each time the script calling Foo::getInstance() is run?

↘锁芯ラ 提交于 2019-12-23 21:00:25
问题 I've suspected for while that PHP singletons are not real singletons, so I did a test. I created the following class: class MySingleton { private static $instance; private function __construct() { error_log("I am a new instance of MySingleton. I was instantiated at " . time()); } private function __clone(){} public static function getInstance() { if(!is_object(self::$instance)) { self::$instance = new MySingleton(); } return self::$instance; } } Next I created a file called test.php: require(

Singleton class: static properties or non-static properties?

£可爱£侵袭症+ 提交于 2019-12-23 20:51:03
问题 I´m programming a Class which acts as a Singleton. I was wondering, does it make sense to have non-static properties for this class? Pseudocode example: class Foo extends MySingletonClass { private static string bar; private string baz; /* more code here */ } 回答1: It's not wrong to have static properties, but it's redundant in a singleton. Also, if you have static properties, and later you need to change the class to not be a singleton no more, you'll need to change the properties either (as

C++ checking singleton pointers

徘徊边缘 提交于 2019-12-23 19:11:44
问题 I have an application, which has a (Qt C++) singleton logger class. The GetInstance() implementation is: if(m_Instance == NULL) { try { m_Instance = new Logger(); } catch(...){} } return m_Instance; Now I have following macro in the .h file: "#define LOG Logger::Instance()->Log" Everything is fine as long as new() operation works. What is the best way to ensure that pointer has set (I was thinking some try-catch blocks to catch the std::bad_alloc, but I don't know how to implement that in a

Is implementation of double checked singleton thread-safe?

六月ゝ 毕业季﹏ 提交于 2019-12-23 18:13:02
问题 I know that the common implementation of thread-safe singleton looks like this: Singleton* Singleton::instance() { if (pInstance == 0) { Lock lock; if (pInstance == 0) { Singleton* temp = new Singleton; // initialize to temp pInstance = temp; // assign temp to pInstance } } return pInstance; } But why they say that it is a thread-safe implementation? For example, the first thread can pass both tests on pInstance == 0 , create new Singleton and assign it to the temp pointer and then start

Presence and location of a property in a QML singleton source results in arbitrary bugs

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 17:59:40
问题 So I was finally able to reproduce it in an MCVE: // S_Settings.qml pragma Singleton import QtQuick 2.7 Item { property real bsize: 50 } // in main() qmlRegisterSingletonType(QUrl(QStringLiteral("qrc:/S_Settings.qml")), "Set", 1, 0, "SS"); // main.qml import QtQuick 2.7 import QtQuick.Window 2.2 import Set 1.0 Window { id: main visible: true width: 300 height: 200 Rectangle { anchors.centerIn: parent width: SS.bsize * 4 height: SS.bsize * 2 radius: SS.bsize * .5 color: "red" border.color:

MonoState, Singleton, or Derived Forms: Best approach for CRUD app?

不打扰是莪最后的温柔 提交于 2019-12-23 12:53:41
问题 I have a fairly large CRUD WinForm app that has numerous objects. Person, Enrollment, Plan, CaseNote etc . There are over 30 forms that make up the app with the UI broken down logically. Member, Enrollments, Plans, CaseNotes, etc . I am trying to figure out how I can create my Person Object after searching on the Search Form and pass THE object to the next requested form. Whatever that may be, let's say Demographics . The short of it is that I need the Person object to be available throughout

abstract class extends abstract class in php?

拜拜、爱过 提交于 2019-12-23 12:35:36
问题 I am working on a simple abstract database class. In my usage of this class, I'll want to have some instance be a singleton. I was thinking of having a abstract class that is not a singleton, and then extend it into another abstract class that is a singleton. Is this possible? Recommended? Edit: I want to have two abstract that are practically identical, except one is a singleton. So the only difference will be that one will have all the functions of the other, but will have the other

@singleton behaving like @stateless bean

梦想的初衷 提交于 2019-12-23 12:32:08
问题 i am working on an application(enterprize application in java) in which i need is single instance to be shared by multiple thread concurrently for which i have used @singleton . when each user login a value is set in telecallers List by invoking setTeleCallersDetails() remote method. but at certain point when number of user logged in exceed 15 then @singleton starts behaving like @stateless bean as setTeleCallersDetails() start adding value in new tellcaller arraylist. can anybody tell to

@Inject and @PostConstruct not working in singleton pattern

你离开我真会死。 提交于 2019-12-23 10:18:13
问题 I have a class as below: public class UserAuthenticator { private static UserAuthenticator authenticator = @Inject private UserRepository userRepository; @PostConstruct public void init() { List<User> allUsers = userRepository.findAll(); for (User user : allUsers) { users.put(user.getEmail(), user.getPassword()); serviceKeys.put(user.getServiceKey(), user.getEmail()); } } public static UserAuthenticator getInstance() { if (authenticator == null) { authenticator = new UserAuthenticator(); }

Static constructors in F# - when do they run?

和自甴很熟 提交于 2019-12-23 09:54:48
问题 I am experimenting with various ways of creating singletons in F#, so that I understand the subtleties better. I don't know if the singleton pattern is ever useful in F#, but I wanted to experiment. And I was surprised by one result involving static constructors on those singleton instances. First I'll show you my code, and then I'll go into more details about my question. In one project called TrySingleton , I created three modules. Here's Eager.fs : module TrySingleton.Eager type EagerClass