singleton

Java: Lazy loading Singleton and reflection attack?

做~自己de王妃 提交于 2019-12-06 10:47:27
If I implement a Singleton either through holder idiom or double checked locking, but instead of calling 'getInstance()', use reflection to instantiate it, and then call 'getInstance()' on it, this would create two instances, breaking the pattern. So I add a static 'counter' member to the class, increment it in the class's private constructor, and throw an exception if it crosses '1'. But in that case, if I first instantiate through reflection, nobody else would be able to call 'getInstance()' without throwing an Exception. So how do I lazy load a Singleton yet prevent it from this attack? (I

How to implement AVAudioPlayer Inside Singleton Method?

让人想犯罪 __ 提交于 2019-12-06 10:44:47
This is my code : class SomeAudioManager: NSObject { class var sharedInstance: SomeAudioManager{ struct Static { static var onceToken: dispatch_once_t = 0 static var instance: SomeAudioManager? = nil } dispatch_once(&Static.onceToken) { Static.instance = SomeAudioManager() } return Static.instance! } func audioView(songname: NSString,format: NSString) { let audioPlayer:ava audioPlayer=try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(songname, ofType:format)!), fileTypeHint: AVFileTypeMPEGLayer3) audioPlayer!.delegate=self; self.audioPlayer!.play() }

Right way way to implement Singleton in Android for data Sharing between activities? [closed]

懵懂的女人 提交于 2019-12-06 09:58:54
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I have been struggling with this issue for a long long time, actually I've checked a lot of post in stackoverflow talking about the

Use of a singleton for creating a basic style helper class

筅森魡賤 提交于 2019-12-06 09:32:31
问题 I am a newbie to objective-c and I have an architectural or design pattern question. I am building an ios app and like most ios apps, it uses various colors, font, patterns etc. Perhaps I am currently writing code incorrectly but I find myself rewriting things like color settings. And as a result, changing colors becomes an exercise in finding all of the color settings in the code, rewriting them, etc. Seems a bit inefficient to me. For example, I use a dark red color in multiple places in my

Implementing the Singleton pattern for accessing an object anywhere

女生的网名这么多〃 提交于 2019-12-06 09:08:15
Currently, I have a class whose constructor takes a username, password, and Context. I would like to be able to access this object from anywhere, so I was thinking of implementing a singleton pattern. The current constructor uses the credentials passed in to authenticate future api calls through that class. If I were to implement the singleton pattern, my first thought would to be have the getInstace() method take a username, password, etc.., but it seems wrong to have to pass that info everytime i grab an instance. Because of this I was thinking of adding some sort of .authenticate(usr, pswrd

How to create a perfect Singleton class in c#?

匆匆过客 提交于 2019-12-06 08:20:50
问题 I want to store some data with Singleton class. As far as I've studied, singleton class can be instantiated only for once. But it doesn't work for me. Can someone correct my code: public class MvcApplication : System.Web.HttpApplication { Singleton clientsessionidinstance = Singleton.GetInstance(); public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(

@Dependent @javax.ejb.Singleton versus @ApplicationScoped @javax.ejb.Singleton?

风流意气都作罢 提交于 2019-12-06 08:19:24
In essence, what is the difference between these two classes: @ApplicationScoped @Singleton class A {} @Dependent @Singleton class B {} Contextual EJB instances I prefer not to use @Inject when looking for EJB:s, unless the EJB is a @Stateful and I want the CDI container to manage the stateful's life-cycle which could be very convenient. Otherwise, using @Inject to retrieve a contextual EJB instance is a bit dangerous. For example, a @Remote client-view cannot be retrieved using CDI unless we also write a producer . Furthermore, class A and class B can not declare any other scope than what

HTTPContext across threads

十年热恋 提交于 2019-12-06 08:00:54
问题 I need to instantiate a singleton object per web request, so that the data is processed once and is valid throughout the request, I was using HttpContext.Current.Items to share data during HTTP request, everything was fine until we needed the singleton object instance across multiple threads, the first thing that I came up with was to pass the HttpContext instance to the new thread: HttpContext context = HttpContext.Current; ThreadPool.QueueUserWorkItem(callback => { HttpContext.Current =

PHP Extending class makes children inherit same static property

吃可爱长大的小学妹 提交于 2019-12-06 07:53:16
I'd like to start by showing a test case: class A { public static $instance=null; public function __construct(){ self::$instance=$this; } public function className(){ return get_class(self::$instance); } } class B extends A { public function className(){ return get_class(self::$instance); } } // test code $b=new B(); echo $b->className; // B $a=new A(); echo $a->className; // A echo $b->className; // A <- error: not B any more! Notes I'm using a factory+singleton patterns above. Well, somewhat. I don't need any specs on "implementing patterns correctly". I need problem solving, not KISS

Singleton-Pattern ASP.NET C# for each user

风格不统一 提交于 2019-12-06 07:36:36
i'm building a web application with asp.net c# and i have a class that i want to use in multiple pages witouth instantiate it every time. I need to load the data in it and never lose them during the user session time. I thought about the singleton-pattern but it shared the instance of the class beetween browsers. How can i solve the problem? Singleton is not the answer. Look at Session State , ViewState and Cookies . UserData data = new UserData(somedata); Session["UserData"] = data; next page UserData data = (UserData) Session["UserData"]; If you have inproc session state you can use