singleton

How to pass an object to a method in Scala

纵饮孤独 提交于 2019-12-20 00:08:57
问题 How can I pass the reference of an object to method in Scala? E.g. I want this to compile object Constants { val constantA:Double = ??? } def calc(numbers:Seq[Double], Constants) = ??? // does not compile def calc(numbers:Seq[Double], constants:Constants) = ??? // does not compile Of course I can just reference Constants without passing it through the argument list, but I would prefer to list pass all dependencies of the method explicitly as arguments. 回答1: Constants is an object. You don't

How to pass an object to a method in Scala

亡梦爱人 提交于 2019-12-20 00:07:13
问题 How can I pass the reference of an object to method in Scala? E.g. I want this to compile object Constants { val constantA:Double = ??? } def calc(numbers:Seq[Double], Constants) = ??? // does not compile def calc(numbers:Seq[Double], constants:Constants) = ??? // does not compile Of course I can just reference Constants without passing it through the argument list, but I would prefer to list pass all dependencies of the method explicitly as arguments. 回答1: Constants is an object. You don't

How to make an immutable singleton in Java?

懵懂的女人 提交于 2019-12-19 19:13:37
问题 An immutable object is initialized by its constuctor only, while a singleton is instantiated by a static method. How to make an immutable singleton in Java? 回答1: while a singleton is instantiated by a static method While this is the usual way of doing it, this is by no means the only way. In Java 1.5 a new version of Singleton is the enum singleton pattern: public enum Elvis{ INSTANCE // this is a singleton, no static methods involved } And since enums can have constructors, methods and

Explicit template static member instantiation in a derived class

旧巷老猫 提交于 2019-12-19 18:15:53
问题 I am trying to implement a template class with a static member. Classes that are derived from the template class shall be instantiated without the need to write extra code. Here is my naive (and not successful) approach: Singleton.h: template <class T> class Singleton { protected: Singleton(); static T instance_; } // explicit instantiation of 'instance_' ???, // where 'instance_' is an instance of the derived class template <class T> T Singleton<T>::instance_; ConcreteA.h: class ConcreteA :

Need of privatizing assignment operator in a Singleton class

怎甘沉沦 提交于 2019-12-19 17:14:31
问题 Can someone justify the need of privatizing the assignment operator in a Singleton class implementation? What problem does it solve by making Singleton& operator=(Singleton const&); private? class Singleton { public: static Singleton& Instance() { static Singleton theSingleton; return theSingleton; } private: Singleton(); // ctor hidden Singleton(Singleton const&); // copy ctor hidden Singleton& operator=(Singleton const&); // assign op. hidden ~Singleton(); // dtor hidden }; 回答1: Assignment

Need of privatizing assignment operator in a Singleton class

ぐ巨炮叔叔 提交于 2019-12-19 17:13:37
问题 Can someone justify the need of privatizing the assignment operator in a Singleton class implementation? What problem does it solve by making Singleton& operator=(Singleton const&); private? class Singleton { public: static Singleton& Instance() { static Singleton theSingleton; return theSingleton; } private: Singleton(); // ctor hidden Singleton(Singleton const&); // copy ctor hidden Singleton& operator=(Singleton const&); // assign op. hidden ~Singleton(); // dtor hidden }; 回答1: Assignment

Need of privatizing assignment operator in a Singleton class

浪尽此生 提交于 2019-12-19 17:13:09
问题 Can someone justify the need of privatizing the assignment operator in a Singleton class implementation? What problem does it solve by making Singleton& operator=(Singleton const&); private? class Singleton { public: static Singleton& Instance() { static Singleton theSingleton; return theSingleton; } private: Singleton(); // ctor hidden Singleton(Singleton const&); // copy ctor hidden Singleton& operator=(Singleton const&); // assign op. hidden ~Singleton(); // dtor hidden }; 回答1: Assignment

In Objective C, Usual implementation of singleton design pattern contains “static id theInstance = nil” in a method, why not outside?

瘦欲@ 提交于 2019-12-19 11:52:44
问题 When i was going through Singleton design pattern in Objective C, I found lot of people using the below code to create it. @interface Base : NSObject {} +(id)instance; @end @implementation Base +(id) instance { static id theInstance = nil; if (theInstance == nil) { theInstance = [[self alloc] init]; } return theInstance; } @end Here i did not get the why do we have to assign the static variable to nil in a method instead it can be declared outside the method and assigned to nil. Because

Is DCL still broken?

梦想与她 提交于 2019-12-19 10:09:08
问题 As far as I understand with old JMM the DCL (Double checked Locking) trick to implement lazy singletone was broken, but i tought that it was fixed with new JMM and volatile field... However in this nice article which is obviously new enought to refer new and old JMM and volatile field in DCL states that it is still broken... Here and there i read that it is fixed then i discover this... Can someone just say finally is it broken or not? My understanding is that with volatile guaranteeing the

Should I implement IDisposable on a singleton?

余生颓废 提交于 2019-12-19 09:58:24
问题 I have a windows service, which contains a singleton which in turn uses some loggers, message queue listeners and so on. Those classes implements IDisposable . Should I implement IDisposable in singleton itself or do something else to ensure that after service stop/crashing everything will be okay with native resources? The singleton is implemented like this: public class Temp { private static readonly Lazy<Temp> instance = new Lazy<Temp>(() => new Temp()); private Temp() { // create