singleton

How to make a Swift Class Singleton instance thread safe?

与世无争的帅哥 提交于 2019-12-10 03:56:29
问题 I have a singleton class as so: class Database { static let instance:Database = Database() private var db: Connection? private init(){ do { db = try Connection("\(path)/SalesPresenterDatabase.sqlite3") }catch{print(error)} } } Now I access this class using Database.instance.xxxxxx to perform a function within the class. However when I access the instance from another thread it throws bizarre results as if its trying to create another instance. Should I be referencing the instance in the same

How to test a Singleton class?

时光怂恿深爱的人放手 提交于 2019-12-10 02:27:53
问题 I am using RSpec and want to test the constructor of a Singleton class more than one time. How can I do this? Best regards 回答1: have a look at http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby: require 'singleton' class <<Singleton def included_with_reset(klass) included_without_reset(klass) class <<klass def reset_instance Singleton.send :__init__, self self end end end alias_method :included_without_reset, :included alias_method :included, :included_with_reset end 回答2: Singleton

How to create object/singleton of generic type in Scala?

余生长醉 提交于 2019-12-09 19:10:08
问题 In the code shown below, how can I convert EmptyTree to object (Singleton) ? trait Tree[T] { def contains(num: T): Boolean def inc( num: T ): Tree[T] } class EmptyTree[T <% Ordered[T] ] extends Tree[T] { def contains(num:T):Boolean = false def inc(num:T):Tree[T] = { new DataTree(num, new EmptyTree, new EmptyTree) } override def toString = "." } class DataTree[T <% Ordered[T] ](val x:T, val left:Tree[T], val right:Tree[T]) extends Tree[T] { def contains(num:T):Boolean = { if( num < x ) left

Java : Singleton class instances in a Web based Application

岁酱吖の 提交于 2019-12-09 17:23:26
问题 I have this Singleton class inside a Web Application . public class MyDAO { private static MyDAO instance; private MyDAO() { } public static MyDAO getInstance() { if (instance == null) { instance = new MyDAO(); } return instance; } I will access it this way public void get_Data() { MyDAO dao = MyDAO.getInstance(); } How many Objects of MyDAO class will be created if there are 3 Users accessing the Application ?? Will there be one instance of MyDAO per User ?? 回答1: You must synchronize the

Fully deallocating an Objective-C singleton

。_饼干妹妹 提交于 2019-12-09 17:14:30
问题 I'm new to writing singletons and I have to use one for a current iOS project. One of the requirements is that it can be killed. I know this goes against the design of a singleton, but is this something that should/could be done? 回答1: Of course it could be done, but if you're looking for an object that can be created, and then released when not needed... that sounds like a regular object. :) Generally singletons control their own lifecycles. You're going to get one-sided discussion here

Using Service as singleton in Android

丶灬走出姿态 提交于 2019-12-09 15:55:27
问题 Is it a bad practice to create a Service that works as a singleton? I mean a Service that is never stopped and that contains some private data that some other engines and Activities would use, so the Service could have something like: public class CustomService extends Service { private List<Profile> mProfiles; private static CustomService instance; public static CustomService getInstance() { if(instance == null) { instance = new CustomService(); } return instance; } public List<Profile>

Double checked locking pattern: Broken or not?

别说谁变了你拦得住时间么 提交于 2019-12-09 15:55:15
问题 Why is the pattern considered broken? It looks fine to me? Any ideas? public static Singleton getInst() { if (instace == null) createInst(); return instace; } private static synchronized createInst() { if (instace == null) { instace = new Singleton(); } } 回答1: It looks okay at first glance, but this technique has many subtle problems and should usually be avoided. For example, consider the following sequence of events: Thread A notices that the value is not initialized, so it obtains the lock

instancetype vs class name for singleton?

放肆的年华 提交于 2019-12-09 14:31:01
问题 From what I understand, instancetype declares to the compiler that the return type of the method is the same as the class receiving the message. Traditionally I've always declared my singleton initializers with the class name explicitly set as the return type: @interface MyClass : NSObject + (MyClass *)sharedInstance; @end Now I'm wondering if I should use instancetype instead, like so: @interface MyClass : NSObject + (instancetype)sharedInstance; @end In the end the result is the same, I'm

What is the difference between all-static-methods and applying a singleton pattern?

∥☆過路亽.° 提交于 2019-12-09 11:26:59
问题 I am making a database to store information about the users of my website (I am using stuts2 and hence Java EE technology). For the database I'll be making a DBManager. Should I apply singleton pattern here or rather make all it's methods static? I will be using this DBManager for basic things like adding, deleting and updating User profiles. Along with it, I'll use for all other querying purposes, for instance to find out whether a username already exists and to get all users for

Guice eager/lazy singleton instantiations

好久不见. 提交于 2019-12-09 09:18:55
问题 I'm having some troubles understanding how Guice's singleton instantiations works. I've read the available documentation (here - http://code.google.com/p/google-guice/wiki/Scopes ), but I still can't figure out some things: 1) I've integrated Guice with Tomcat, and I've set up some bindings in a ServletModule: bind(MyServlet.class).asEagerSingleton(); serve("myUrl").with(MyServlet.class); serve("myOtherUrl").with(MyOtherServlet.class); (where MyOtherServlet class has a @Singleton annotation