singleton

Library as Singleton in Codeigniter?

廉价感情. 提交于 2019-12-10 15:42:37
问题 I've created a custom library using singleton pattern. Why by this way? because I need to be able to call my functions without $this-> reference, for example, I could execute the code below: function foo() { MyLibrary::instance()->foo(); } then I could call my functions in controllers like this: function foo(); instead of $this->mylibrary->foo(); I am in trouble because CodeIgniter try to instantiate my library when the best way to do is "read" the static instance. Why I need to do this? My

How and when to dispose/garbage collect a singleton instance

别来无恙 提交于 2019-12-10 15:36:31
问题 I am using a Singleton instance created out of a nested class. This instance holds some static collections which are cleared when the Singleton is disposed, but the problem is I get a reference to non-null disposed Singleton which is not properly garbage collected. I would like to know WHEN and HOW to completely dispose and garbage collect my Singleton instance so that when the instance is queried again after dispose (and setting to null) a new Instance is created. I am using the following

Java singleton inner class

怎甘沉沦 提交于 2019-12-10 15:24:44
问题 I know the concept of singleton in Java. I'm having problems with creating singleton as inner class in Java. Problem occurs at holder's public class NormalClass { private class Singleton { private static Singleton instance = null; private Singleton() { } private static class SingletonHolder { private static Singleton sessionData = new Singleton(); } public static Singleton getInstance() { return NormalClass.Singleton.SingletonHolder.sessionData; } } public void method1() { Singleton

c# singleton code reuse

北城余情 提交于 2019-12-10 15:23:49
问题 I have a number of classes doing different things but using the same Singleton pattern from http://www.yoda.arachsys.com/csharp/singleton.html public sealed class Singleton { static Singleton instance=null; static readonly object padlock = new object(); Singleton() { } public static Singleton Instance { get { lock (padlock) { if (instance==null) { instance = new Singleton(); } return instance; } } } } Does anyone have a neat way I can reuse as much of the common Singleton code as possible

static AlarmManager in Android

拥有回忆 提交于 2019-12-10 15:12:48
问题 I'm developing a simple tasks app in Android and I need to create notifications via an AlarmManager. My problem is that I have certain alarms that should be deleted -and thus their notifications- but they aren't, so I decided -following posts such as Delete alarm from AlarmManager using cancel() - Android to make the AlarmManager a static variable so the same instance can be reached from the whole app. The way I'm doing this is having the following method in my main class: public static

Dependency Injection in View Controller

自古美人都是妖i 提交于 2019-12-10 14:59:24
问题 I am trying to use dependency injection instead of following singletons. This is how I am trying to achieve. When I run the application I am having an error on "No "decodeObject" candidates produce the expected contextual result type "ModelManager" on that. Any idea how can I implement dependency injection in a right way? My Model class: class ModelManager { var results: MyCustomClass init(results: MyCustomClass) { self.results = results } func update(customDataInfo: MyCustomClass!) { self

Singleton class or a class with static methods in ASP.NET MVC application [duplicate]

扶醉桌前 提交于 2019-12-10 14:23:43
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: ASP .NET Singleton I know the general differences between singleton class and a class with static properties/methods but I would like to know how it influences the concurrency (many users logged in an application) in a ASP.NET MVC web application? For instance, we are storing settings in our singleton (or static properties) class. Is there a chance that two users suddenly start seeing/sharing the same settings?

Force Singleton Pattern on a Class implementing an Interface

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 13:54:59
问题 I better explain the question with an example. I have an Interface Model which can be used to access data. There can be different implementations of Model which can represent the data in various format say XMl , txt format etc. Model is not concerned with the formats. Lets say one such implementation is myxmlModel . Now i want to force myxmlModel and every other implementation of Model to follow Singleton Pattern .The usual way is to make myxmlModels constructor private and provide a static

How to make Angular2 Service singleton?

心已入冬 提交于 2019-12-10 12:59:17
问题 I'm trying to implement an auth guard in my app. ie; Only authenticated users can access certain routes of my app. I'm following the tut given here. Once the user is logged in I change a boolean value in my AuthService to true to indicate that the use has logged in. Which needs to be retained through out the life of app. Given below the source code: auth-guard.service.ts import { Injectable } from '@angular/core'; import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot }

Setting Time.zone during a request: Thread Safe?

我怕爱的太早我们不能终老 提交于 2019-12-10 12:48:11
问题 I'd like to let users of my app specify a time zone, and then set their time zone at the beginning of each request. In Rails, AFAICT, this is done by setting the singleton object: Time.zone = "America/Los_Angeles" My understanding of best practices is that, generally speaking, a singleton should be set ONCE. For example in the application configuration. From an answer to a similar question, someone suggests setting it in the ApplicationController class ApplicationController < ActionController