singleton

C++ Singleton undefined reference to

流过昼夜 提交于 2019-12-30 06:45:11
问题 I am new to C++ and trying to understand the Singleton Pattern in C++. myclass.h #ifndef MYCLASS_H #define MYCLASS_H class Myclass { public: static Myclass* getInstance(); private: Myclass(){} Myclass(Myclass const&){} Myclass& operator=(Myclass const&){} static Myclass* m_instance; }; #endif // MYCLASS_H myclass.cpp #include "myclass.h" Myclass* Myclass::getInstance() { if (!m_instance) { m_instance = new Myclass; } return m_instance; } The compiler can't compile. I get the following error,

Dependency injection when the class created also needs runtime values?

空扰寡人 提交于 2019-12-30 03:27:07
问题 Assume you divide up your systems in Value objects and Services objects (as suggested in "Growing Object-Oriented Software, Guided by Tests". Misko Hevery calls these "newables" and "injectables". What happens when one of your value objects suddenly needs to access a service to implement it's methods? Let's say you have a nice simple Value object. It's immutable, holds a few bits of information and that's about it. Let's say we use it something like this: CreditCard card = new CreditCard(

Singleton - Why use classes?

梦想与她 提交于 2019-12-30 02:48:46
问题 Just the other day I have seen code that uses the so called singleton pattern. Meaning something along the lines of class MySingleton{ public: void foo() { ... } static MySingleton&get_instance(){ static MySingleton singleton; return singleton } private: MySingleton(){ ... } ~MySingleton(){ ... } int bar; }; I do see why one would want to do that: Make the instance globally accessible. Make sure that there is never more than one instance of that class. However I do not see why this way of

Cannot access private member in singleton class destructor

孤街浪徒 提交于 2019-12-29 08:52:08
问题 I'm trying to implement this singleton class. But I encountered this error: 'Singleton::~Singleton': cannot access private member declared in class 'Singleton' This is flagged in the header file, the last line which contains the closing brace. Can somebody help me explain what is causing this problem? Below is my source code. Singleton.h: class Singleton { public: static Singleton* Instance() { if( !pInstance ) { if( destroyed ) { // throw exception } else { Create(); } } return pInstance; }

Cannot access private member in singleton class destructor

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 08:52:05
问题 I'm trying to implement this singleton class. But I encountered this error: 'Singleton::~Singleton': cannot access private member declared in class 'Singleton' This is flagged in the header file, the last line which contains the closing brace. Can somebody help me explain what is causing this problem? Below is my source code. Singleton.h: class Singleton { public: static Singleton* Instance() { if( !pInstance ) { if( destroyed ) { // throw exception } else { Create(); } } return pInstance; }

How to create a class which can only have a single instance in C#

笑着哭i 提交于 2019-12-29 08:01:52
问题 I wonder if there is a mechanism or pattern to allow only one instance of a class in C#. I have heard of the Singleton class, but i don't know how to use it well. 回答1: Using singleton, that is a class which only allows a single instance of itself to be created. public sealed class Singleton { public static readonly Singleton instance = new Singleton(); private Singleton() {} } The operation of this pattern is simple and could be reduced to the following: Hide the constructor of the Singleton

A simple implementation of a Singleton

帅比萌擦擦* 提交于 2019-12-29 07:36:25
问题 Isn't this a simpler as well as safe (and hence better) way to implement a singleton instead of doing double-checked locking mambo-jambo? Any drawbacks of this approach? public class Singleton { private static Singleton _instance; private Singleton() { Console.WriteLine("Instance created"); } public static Singleton Instance { get { if (_instance == null) { Interlocked.CompareExchange(ref _instance, new Singleton(), null); } return _instance; } } public void DoStuff() { } } EDIT: the test for

Multiple Delegates in iOS

半腔热情 提交于 2019-12-29 04:12:15
问题 I am making an object that goes to download stuff for all of my view controllers. The object is singleton instance and has a callback method with received data once the download is completed. It also has a delegate property so that it knows which object to call back to after the download is done. There are multiple controllers that use this shared instance, and my question is how to call back to the correct view controller that requested the download. My approach is to use delegation, but the

Why is enum the best implementation for a singleton?

有些话、适合烂在心里 提交于 2019-12-29 03:22:37
问题 I read Effective Java and there it's stated that a singleton is best implemented using enum . This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a

How to access objects within an object by mixing in a trait with reflection?

混江龙づ霸主 提交于 2019-12-29 01:40:30
问题 How can I get all of the objects within an object with reflection? Consider this code: object MonthDay extends MyEnum { //Some important holidays object NewYear extends MonthDay( 1, 1) object UnityDay extends MonthDay(11, 9) object SaintNicholas extends MonthDay(12, 6) object Christmas extends MonthDay(12, 24) } class MonthDay(month: Int, day: Int) trait MyEnum { val values: List[MonthDay] = this.getClass.getField("MODULE$")... val next: MonthDay = ... val previous: MonthDay = ... } //Of