singleton

ruby - create singleton with parameters?

落花浮王杯 提交于 2020-07-17 06:22:03
问题 I've seen how to define a class as being a singleton (how to create a singleton in ruby): require 'singleton' class Example include Singleton end But what if I want to give it some parameters for that single instance, meaning, the Example should always have certain properties initialized. For example, say I had a class whose sole purpose is to log to a file (this is just an example) but it requires a name of a file to log to before it can work. class MyLogger def initialize(file_name) @file

ruby - create singleton with parameters?

99封情书 提交于 2020-07-17 06:21:11
问题 I've seen how to define a class as being a singleton (how to create a singleton in ruby): require 'singleton' class Example include Singleton end But what if I want to give it some parameters for that single instance, meaning, the Example should always have certain properties initialized. For example, say I had a class whose sole purpose is to log to a file (this is just an example) but it requires a name of a file to log to before it can work. class MyLogger def initialize(file_name) @file

ruby - create singleton with parameters?

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-17 06:21:04
问题 I've seen how to define a class as being a singleton (how to create a singleton in ruby): require 'singleton' class Example include Singleton end But what if I want to give it some parameters for that single instance, meaning, the Example should always have certain properties initialized. For example, say I had a class whose sole purpose is to log to a file (this is just an example) but it requires a name of a file to log to before it can work. class MyLogger def initialize(file_name) @file

Multiple QApplication instances

不羁岁月 提交于 2020-06-11 05:03:20
问题 I'd like to know what are the implications (problems) of having multiple QApplication / QCoreApplication instances in the same process, and how to solve some issues regarding it. The scenario is as follows: I'd like to make a wrapper on a open source third-party application in order to convert it into an embeddable widget as an optional plugin (the application consists basically on a single QMainWindow -based interface). Such project heavily relies on a QCoreApplication derived class but

How to serialize/deserialize Kotlin sealed class?

混江龙づ霸主 提交于 2020-05-26 12:09:31
问题 I have a following sealed class: sealed class ViewModel { data class Loaded(val value : String) : ViewModel() object Loading : ViewModel() } How can I serialize/deserialize instances of the ViewModel class, let's say to/from JSON format? I've tried to use Genson serializer/deserializer library - it can handle Kotlin data classes, it's also possible to support polymorphic types (eg. using some metadata to specify concrete types). However, the library fails on Kotlin object types, as these are

How to pass argument in a singleton

情到浓时终转凉″ 提交于 2020-05-23 03:29:11
问题 I've been wondering how to pass argument to a singleton contructor. I already know how to do a singleton, but I've been unlucky to find a way to do it. Here is my code (part of it). Questionnary* Questionnary::getInstance(){ static Questionnary *questionnary = NULL; if(questionnary == NULL){ cout << "Object created"; questionnary = new Questionnary(); } else if(questionnary != NULL){ cout << "Object exist"; } return questionnary; } Questionnary::Questionnary(){ cout << "I am an object"; } /

Creating Extensible Applications using service-loaders according to a singleton design pattern

我的梦境 提交于 2020-05-16 03:11:34
问题 I'm working on project on IntelliJ IDEA and I want to add support to Extensible Applications in my java application. The way to do it is, creating a jar file in this jar file there should be a META-INF/services directory, inside this directory, I need to add a file whose name contains the same name as the fully-qualified name of the interface which provides the service, and in that file it should have the fully-qualified name of the implementations of that interface. This is the encrypt and

Why singleton breaks open/closed principle?

北城余情 提交于 2020-05-13 03:38:41
问题 Could anyone tell me why does singleton break open/closed principle? Is it because there could be problems with inheriting from that class? 回答1: For a class to be "open" it must be possible to inherit from it. Inheritance is an "is-a" relationship. If you inherit from a singleton-class then instances of the child-class are also instances of the parent class due to the "is-a" relationship, meaning you can suddenly have multiple instances of the singleton class. If the singleton class inhibits

Why singleton breaks open/closed principle?

时光毁灭记忆、已成空白 提交于 2020-05-13 03:37:32
问题 Could anyone tell me why does singleton break open/closed principle? Is it because there could be problems with inheriting from that class? 回答1: For a class to be "open" it must be possible to inherit from it. Inheritance is an "is-a" relationship. If you inherit from a singleton-class then instances of the child-class are also instances of the parent class due to the "is-a" relationship, meaning you can suddenly have multiple instances of the singleton class. If the singleton class inhibits

Trouble creating a simple singleton class in Jersey 2 using built-in Jersey dependency injection

ぐ巨炮叔叔 提交于 2020-05-09 21:40:53
问题 I am having trouble getting a very basic implementation of a singleton class off the ground with Jersey 2 (2.7) and only Jersey's built-in HK2 dependency injection. I am running this on Tomcat. My goal is to create a singleton instance of a support class that will be used by various web service methods. I don't have a strong preference between constructor injection, method injection, and annotating a class member (as I do below). Here is my to-be-singleton class: package singletest; import