singleton

Singleton with finalizer but not IDisposable

吃可爱长大的小学妹 提交于 2019-12-22 01:38:58
问题 This is what I understand about IDisposable and finalizers from "CLR via C#", "Effective C#" and other resources: IDisposable is for cleaning up managed and unmanaged resources deterministically. Classes that are responsible for unmanaged resources (e.g. file handles) should implement IDisposable and provide a finalizer to guarantee that they are cleaned up even if the client code does not call Dispose() on the instance. Classes that are responsible for managed resources only should never

Are Kotlin's singletons thread safe?

六眼飞鱼酱① 提交于 2019-12-22 01:26:06
问题 Are Kotlin singletons (more specifically, object declarations) thread-safe by construction? If not, what is the best practice to write thread safe singletons in Kotlin? I would guess they are, but I haven't been able to find any explicit statement about it in the docs. 回答1: Kotlin "object" is thread-safe by construction. As you can see in any decompile/dumping tool, declared object is just final class with static instance initialization + language syntax sugar to simplify instance access 来源:

Patterns: Singletons vs. Static vars and methods approach

你说的曾经没有我的故事 提交于 2019-12-22 00:22:24
问题 I am reading a lot about the Singleton Pattern. I am currently using it to store groups of global state in my first app. I am reaching a point where I wonder which approach to implement API client classes and similar with. Are Structs with static vars and static functions having the same issues? To illustrate what I mean, I've tried to write the same heavily simplified and exact same(?) scenario twice. 1. A singleton being worked with by a view controller: struct APIClientSingletonClass {

Different ways to initialize singletons

牧云@^-^@ 提交于 2019-12-22 00:08:42
问题 Working in C# and Java, I've seen basically one way everybody initializes singletons: static obj _inst = null; obj getInstance() { if (_inst == null) { _inst = new obj(); } return _inst; } Now, when I move to Objective-C for the iPhone, whenever I see code samples, I see basically the same thing: static obj _inst = nil; + (obj *) sharedObj { if (!_inst) { _inst = [[obj alloc] init]; } return _inst; } There's a class method +initialize that's called on every class in the Objective-C runtime

Can singleton class be static?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 22:39:49
问题 Can singleton class be static? 回答1: No. A singleton class is meant to be instantiated, because the term itself refers to an instance; if you make it a static class, you can't create a singleton object out of it. 回答2: (Cat, meet pigeons.) Yes, but only in practice, not in theory. A singleton is a class that can only be instantiated once. A static class cannot be instantiated, so it cannot be called a singleton. However, since we're talking about C#, static classes have constructors, so it is

Thread-safe Enum Singleton

不羁的心 提交于 2019-12-21 21:12:14
问题 Enums are good for creating singleton. I know enum methods are not thread-safe so I tried to make it thread-safe. Can anyone please confirm if this implementation is correct or not. Is it fine to use static and volatile so many places and can it be optimized? As inner class is private so I have to create functions in enum to access inner class functionality. Can it be optimized? import java.util.Date; public enum SingletonWithEnum { INSTANCE; private static class Singleton{ private static

Test singleton with jUnit

我的未来我决定 提交于 2019-12-21 21:04:32
问题 I have a factory class to retrieve the configuration for my application: public class ConfigurationFactory { private static ConfigurationFactory configurationFactory = new ConfigurationFactory(); private Configuration configuration = null; public static ConfigurationFactory getConfigurationFactory() { return configurationFactory; } And some getConfiguration methods depending on where I am getting the config from (file, database, default, etc...): public Configuration getConfiguration(String

What is the use of placing an instance variable in .h where it wouldn't have a getter and setter ( i.e. nobody can set nor get it)?

試著忘記壹切 提交于 2019-12-21 20:56:41
问题 I have read Where to put iVars in "modern" Objective-C? and some other questions, but I am still confused. I am reading from https://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app SQLite Tutorial: .h #import <Foundation/Foundation.h> #import <sqlite3.h> @interface FailedBankDatabase : NSObject { sqlite3 *_database; } + (FailedBankDatabase*)database; - (NSArray *)failedBankInfos; @end .m #import "FailedBankDatabase.h" #import "FailedBankInfo.h" @implementation

How to keep a variable in memory until the app quits

北城以北 提交于 2019-12-21 20:44:25
问题 I have a singleton object in iOS that when instantiated parses a CSV file and then holds the results. I would like to make this object universally accessible and I would like it to not be released from memory until the app quits. I am running ARC so I cannot do manual retains. Is there a way I can do this so it will work with ARC? Header File: #import <Foundation/Foundation.h> #import "CHCSV.h" #import "RCParserObject.h" @interface ParserStore : NSObject <CHCSVParserDelegate> { // CSV

Singleton v Single Instance DB Connection in PHP

元气小坏坏 提交于 2019-12-21 20:19:33
问题 I'm moving onto teaching myself OOP in PHP. I'm creating a couple of little web apps and have followed a lot of tutorials that either create the database (using PDO) via a Singleton, or via passing the global around. I've read that these are pretty much the same thing and are both to be avoided like the plague. So I've watched the Google Tech Talks on clean code, and read almost every SO article on dependency injection and the like. I have a couple of questions. The clean code videos suggest