singleton

Singleton pattern interview

谁都会走 提交于 2019-12-20 10:43:50
问题 I am recently asked about java related question in an interview with following code, since I am very new to java and barely code in Java so I really have no idea what the following code does. The question was Select the option that describes the worst thing with the following code: public class Bolton { private static Bolton INST = null; public static Bolton getInstance() { if ( INST == null ) { INST = new Bolton(); } return INST; } private Bolton() { } } Here are the options for this

Why use static method in PHP's laravel model class?

烂漫一生 提交于 2019-12-20 10:39:58
问题 In PHP laravel, we have codes like $user = User::find(1); var_dump($user->name); I am not concerning how to use the find method, I am concerning why laravel use a static method? Shouldn't the use of static method make the method hard to test? Will it be better if they designed using singleton? e.g. $user = User::getInstance()->find(1); var_dump($user->name); 回答1: In fact, your example is very similar to what Laravel does behind the scene. When you do User::find() , you are actually asking for

Singleton class vs. class with static member

左心房为你撑大大i 提交于 2019-12-20 09:45:08
问题 Despite the many threads on that topic, I am still unclear as to when to choose which approach. I am hoping that by discussing a specific example, I will finally "get it." Note: My language here is Cocoa though the general issue is not language-specific. I have a class TaskQueue that I want to use to: access from anywhere in my code in order to add or remove scheduled tasks process automatically the scheduled tasks at regular intervals When TaskQueue is first used, I want TaskQueue to

Kotlin Singleton Application Class

旧街凉风 提交于 2019-12-20 09:14:51
问题 So in android i want to make my application class a singleton. Making it like this: object MyApplication: Application(){} won't work. Following erros is thrown at runtime: java.lang.IllegalAccessException: private com....is not accessible from class android.app.Instrumentation. Doing this is also not possible: class MyApp: Application() { private val instance_: MyApp init{ instance_ = this } override fun onCreate() { super.onCreate() if (BuildConfig.DEBUG) { Timber.plant(Timber.DebugTree());

java Singleton - prevent multiple creation through reflection

扶醉桌前 提交于 2019-12-20 08:49:31
问题 I have a singleton like this. public class BookingFactory { private final static BookingFactory instance; static { instance = new BookingFactory(); } public static BookingFactory getInstance() { return instance; } private BookingFactory() { System.out.println("Object is created."); } } public class Test { BookingFactory instance = BookingFactory.getInstance(); instance = BookingFactory.getInstance(); Class<?> clazz = Class.forName("com.test.BookingFactory"); Constructor pvtConstructor = clazz

“Singleton” factories, ok or bad?

时光怂恿深爱的人放手 提交于 2019-12-20 08:48:39
问题 I've a lot of (abstract) factories and they're usually implemented as singletons. Usually for the convenience of not having to pass them through layers who really have no business with using or knowing these factories. Most of the times I only need to make a decision at startup of which factory implementation the rest of the code program, maybe through some configuration it looks e.g. like abstract class ColumnCalculationFactory { private static ColumnCalculationFactory factory; public static

Singleton in go

可紊 提交于 2019-12-20 08:28:00
问题 How does one implement the Singleton design pattern in the go programming language? 回答1: Setting aside the argument of whether or not implementing the singleton pattern is a good idea, here's a possible implementation: package singleton type single struct { O interface{}; } var instantiated *single = nil func New() *single { if instantiated == nil { instantiated = new(single); } return instantiated; } single and instantiated are private, but New() is public. Thus, you can't directly

Singleton and unit testing

荒凉一梦 提交于 2019-12-20 08:27:21
问题 The Effective Java has the following statement on unit testing singletons Making a class a singleton can make it difficult to test its clients, as it’s impossible to substitute a mock implementation for a singleton unless it implements an interface that serves as its type. Can anyone explain the why this is so ? 回答1: You could use reflection to reset your singleton object to prevent tests from affecting each other. @Before public void resetSingleton() throws SecurityException,

ios/objective c/singleton: Storing userid in session variable

邮差的信 提交于 2019-12-20 07:55:44
问题 I defined an NSInteger in a singleton (for userid) and access it in another class. However, while I have managed to get rid of error messages so it builds, the app crashes when it hits line. Here is the initial definition; Session.h @interface Session : NSObject @property (assign,nonatomic) NSInteger *userid; + (IDSession *)sharedInstance; Session.m @implementation Session + (Session *)sharedInstance { static Session *session; if (!session){ session = [[Session alloc] init]; NSInteger *userid

ios singleton class crashes my app

帅比萌擦擦* 提交于 2019-12-20 07:14:09
问题 I have a problem with an singleton pattern. I have read the following tutorials about singleton classes and have created my own. http://www.galloway.me.uk/utorials/singleton-classes/ http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/ The first time i build & run the app it works like it should. No problems at all! But when i rebuild the app the singleton class does not work properly anymore. The first init works like it should but when i call it again after a