singleton

Singleton Pattern

Deadly 提交于 2019-12-31 15:50:11
问题 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

Using room as singleton in kotlin

坚强是说给别人听的谎言 提交于 2019-12-31 10:47:14
问题 I'm trying to use Room as singleton so I didn't have to invoke Room.databaseBuilder() -which is expensive- more than once. @Database(entities = arrayOf( Price::class, StationOrder::class, TicketPrice::class, Train::class, TrainCategory::class ), version = 2) @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { abstract fun dao(): TrainDao companion object { fun createDatabase(context: Context): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java,

Extending singletons in PHP

早过忘川 提交于 2019-12-31 08:06:10
问题 I'm working in a web app framework, and part of it consists of a number of services, all implemented as singletons. They all extend a Service class, where the singleton behaviour is implemented, looking something like this: class Service { protected static $instance; public function Service() { if (isset(self::$instance)) { throw new Exception('Please use Service::getInstance.'); } } public static function &getInstance() { if (empty(self::$instance)) { self::$instance = new self(); } return

How to implement a Singleton in an application with DLL

旧城冷巷雨未停 提交于 2019-12-31 03:03:30
问题 I have an application (in MS Visual Studio) that contains 3 projects: main (the one that contains the main function) device (models some hardware device) config (contains some configuration for both other projects) So the dependency graph is: main depends on device , which depends on config main depends on config The config project contains a Singleton, which holds some configuration parameters. I decided to turn the device project into a DLL. When i did this, it seems that i got two

Static methods: are they still bad considering PHP 5.3 late static binding?

£可爱£侵袭症+ 提交于 2019-12-30 17:23:21
问题 If you search on the reasons why static methods are bad the first thing you find it is because you can't override it when you are unit testing. So is this still true considering in PHP 5.3 you can do whatever you want with the introduction of static:: ? Add: http://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html Note he explains even how to use singleton without any testing problem: http://sebastian-bergmann.de/archives/882-Testing-Code-That-Uses-Singletons.html

Use and flow of Static statements in Singleton Program

戏子无情 提交于 2019-12-30 10:45:40
问题 I know there are lot of questions on Singleton pattern. But here what I would like to know about the output which might also cover how "static" works in Java. public class Singleton { private static Singleton currentSingleton = new Singleton(); public static Singleton getSingleton() { return currentSingleton; } private Singleton() { System.out.println("Singleton private constructor..."); } public static void main(String[] args) { System.out.println("Main method..."); } } This is the output

Mocking singleton/sharedInstance in Swift

泄露秘密 提交于 2019-12-30 09:43:29
问题 I have a class that I want to test using XCTest, and this class looks something like this: public class MyClass: NSObject { func method() { // Do something... // ... SingletonClass.sharedInstance.callMethod() } } The class uses a singleton that is implemented as this: public class SingletonClass: NSObject { // Only accessible using singleton static let sharedInstance = SingletonClass() private override init() { super.init() } func callMethod() { // Do something crazy that shouldn't run under

Mocking singleton/sharedInstance in Swift

巧了我就是萌 提交于 2019-12-30 09:43:14
问题 I have a class that I want to test using XCTest, and this class looks something like this: public class MyClass: NSObject { func method() { // Do something... // ... SingletonClass.sharedInstance.callMethod() } } The class uses a singleton that is implemented as this: public class SingletonClass: NSObject { // Only accessible using singleton static let sharedInstance = SingletonClass() private override init() { super.init() } func callMethod() { // Do something crazy that shouldn't run under

Should we seal Singletons? Should we try to inherit from Singletons in the first place?

回眸只為那壹抹淺笑 提交于 2019-12-30 06:52:10
问题 Should a Singleton class be allowed to have children? Should we seal it? What are the pro's and con's? For being able to inherit from a Singleton class, we would have to make the constructor protected instead of private. Now, that will be fine in c#, but the protected word in java gives both child-classes and package-classes access to the constructor. Which means not only classes that inherit from our Singleton can access the constructor but other classes in the same package can do it. I'm a

What's wrong with this fix for double checked locking?

戏子无情 提交于 2019-12-30 06:47:53
问题 So I've seen a lot of articles now claiming that on C++ double checked locking, commonly used to prevent multiple threads from trying to initialize a lazily created singleton, is broken. Normal double checked locking code reads like this: class singleton { private: singleton(); // private constructor so users must call instance() static boost::mutex _init_mutex; public: static singleton & instance() { static singleton* instance; if(!instance) { boost::mutex::scoped_lock lock(_init_mutex); if(