singleton

Why am I getting unresolved externals?

前提是你 提交于 2019-12-13 02:56:43
问题 I am writing an immutable binary search tree in c++. My terminating nodes are represented by a singleton empty node. My compiler (visual c++) seems to be having trouble resolving the protected static member that holds my singleton. I get the following error: error LNK2001: unresolved external symbol "protected: static class boost::shared_ptr > node::m_empty" (?m_empty@?$node@HH@@1V?$shared_ptr@V?$node@HH@@@boost@@A) I am assuming this means it cant resolve the static m_empty member for the

Implementing Singleton as metaclass, but for abstract classes

雨燕双飞 提交于 2019-12-13 02:35:13
问题 I have an abstract class and I would like to implement Singleton pattern for all classes that inherit from my abstract class. I know that my code won't work because there will be metaclass attribute conflict. Any ideas how to solve this? from abc import ABCMeta, abstractmethod, abstractproperty class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls]

How do I ensure there is only one instance of a class at any one time in Objective-C?

送分小仙女□ 提交于 2019-12-13 02:33:17
问题 This is slightly different from the standard singleton pattern, in that if all external references to an object have released, then the singleton will be released, too. Then, later, when a new object is requested, a new singleton is created. So, something like this: MyThing *thing1 = [MyThing new]; MyThing *thing2 = [MyThing new]; // thing1 & thing2 are the same object. thing1 = nil; thing2 = nil; thing1 = [MyThing new]; thing2 = [MyThing new]; // thing1 and thing2 are a the same objet, but

How to use the Singleton pattern via inheritance?

徘徊边缘 提交于 2019-12-12 22:22:34
问题 I am trying to use the Singleton design pattern via my abstract Charcter class so all sub classes can acces the object instance. Here is my singleton class: class GatewayAccess { private static GatewayAccess ph; // Constructor is 'protected' protected GatewayAccess() { } public static GatewayAccess Instance() { // Uses lazy initialization. // Note: this is not thread safe. if (ph == null) { ph = new GatewayAccess(); Console.WriteLine("This is the instance"); } return ph; } } I can use this in

How to make a Volley Request activity independent in android?

血红的双手。 提交于 2019-12-12 22:18:48
问题 I read the documentation about making an network request activity independent by making a singleton class and passing the application context to it. I implemented it similarly, however I still find that on rotation the app waits for the call again to complete before displaying any data. So what am I doing wrong and how to set it up properly so that the call lasts the lifetime of the application so that it doesn't call every time on orientation change as per the documentation. I know it can be

Objective-C: When to know that you are abusing the SIngleton method of Global Variables

99封情书 提交于 2019-12-12 21:26:21
问题 So my clients iphone app has balloned from 5 or so classes to over 25 in the last few weeks. With such a large (for the iphone anyway) class structure I've been utilizing the Singleton class for accessing global variables. The problem is that whenever I need to access a variable outside of the class I'm working on, I have a choice of either modifying the code to pass a reference to the variable or just throw it in the singleton. Of course throwing it in the singleton is a lot less code and

How to make a singleton for retrofit 2?

若如初见. 提交于 2019-12-12 19:22:05
问题 If there exists multiple retrofit call, how can i make a singleton of a retrofit, so that there won't be repeated codes within the class, thereby get rid of unnecessary codes. 回答1: Here's an example, but! Although this might be shiny and easy to use, singletons are evil. Try to avoid using them if possible. One way around it is by using dependency injection instead. Anyway. public class Api { private static Api instance = null; public static final String BASE_URL = "your_base_url"; // Keep

std::system Exception when instantiating a singleton object

别说谁变了你拦得住时间么 提交于 2019-12-12 19:11:26
问题 I'm learning how to implement a thread safe singleton pattern in c++11 and later. #include <iostream> #include <memory> #include <mutex> class Singleton { public: static Singleton& get_instance(); void print(); private: static std::unique_ptr<Singleton> m_instance; static std::once_flag m_onceFlag; Singleton(){}; Singleton(const Singleton& src); Singleton& operator=(const Singleton& rhs); }; std::unique_ptr<Singleton> Singleton::m_instance = nullptr; std::once_flag Singleton::m_onceFlag;

MVVM share object between the all the views

偶尔善良 提交于 2019-12-12 19:02:20
问题 I have MVVM Project and I want to share one object( singleton ) from the model between several viewmodel what is the good practice to do that? Thank you for the help 回答1: If the object is needed and does not provide value without it force the interface within the object via Constructor Injection; do not push a concrete type via injection always make use of an interface. Since you are not making use of an IoC container such as Unity, you will need to create your singleton instance at the

How to make a Rust singleton's destructor run?

て烟熏妆下的殇ゞ 提交于 2019-12-12 18:56:05
问题 These are the ways I know of to create singletons in Rust: #[macro_use] extern crate lazy_static; use std::sync::{Mutex, Once, ONCE_INIT}; #[derive(Debug)] struct A(usize); impl Drop for A { fn drop(&mut self) { // This is never executed automatically. println!( "Dropping {:?} - Important stuff such as release file-handles etc.", *self ); } } // ------------------ METHOD 0 ------------------- static PLAIN_OBJ: A = A(0); // ------------------ METHOD 1 ------------------- lazy_static! { static