singleton

Singleton across modules

旧时模样 提交于 2019-12-21 20:15:01
问题 I'm trying to implement a Singleton and I am running into difficulty when I import the module. My set up is the following. I am using Python 2.7. MODULE 1 class SingletonClass(object): def __new__(self, *args, **kwargs): if not self._instance: self._instance = super(SingletonClass, self).__new__( self, *args, **kwargs) return self._instance print SingletonClass() #OUTPUT: 0x00000000030F1630 print SingletonClass() #OUTPUT: 0x00000000030F1630 (Good, what I want) MODULE 2 import SingletonClass

Don't static members make classes kind of (global) objects themselves?

不羁岁月 提交于 2019-12-21 20:02:55
问题 Every time I come across an implementation of the singleton pattern or any static classes (i.e. classes with (almost) only static members) I wonder whether this isn't actually a hack and therefore heavy abuse of the principle of classes and instances just to design single objects instead of designing classes and creating a single instance. To me, it looks like static members of classes in general try to add some sort of characteristics to classes which they actually aren't supposed to have

using shared instance of ADBannerView across app with UITableViews

不想你离开。 提交于 2019-12-21 17:48:05
问题 I have an app with multiple UITableViews and am in the process of implementing iADs. Per the Apple documentation (http://developer.apple.com/library/ios/#technotes/tn2286/_index.html#//apple_ref/doc/uid/DTS40011212) I have created a shared banner that belongs to my app delegate and the application delegate is also the banner's delegate. This works well and the ads show nicely across the various view controllers AFTER the banner is loaded and the user switches screens. The problem is that

Problems passing data to a DetailViewController from TableView in iOS5

不羁岁月 提交于 2019-12-21 17:32:32
问题 I would appreciate any help in solving my problem that has had me pulling my hair out for the last day! Disclaimer 1 I'm an Objective-C noob - so the answer to this may be very simple (I'm coming from years of PHP). Please be nice. Disclaimer 2 Yes, I have extensively 'Googled' and looked at Apple Documentation - but have failed to come to an understanding of my problem. Summary I am trying to pass a value selected in a table view to another controller from a singleton. I am finding that the

How do I make other classes derive from a singleton class?

岁酱吖の 提交于 2019-12-21 17:20:11
问题 I am sorry if this is a duplicate or too elementary, but how do I make a singleton class that can be subclassed? 回答1: Steve Yegge has an amusing article about singletons that mentions subclassing in this quote: Then there's the subclassing thing. It's almost impossible to subclass a Singleton, and if you manage it, then you shouldn't have been using a Singleton in the first place. You don't even want to go there. I've walked roads that I dare not recount. Just pretend you can't do it, and you

Objective-C - Optimizing this singleton pattern?

元气小坏坏 提交于 2019-12-21 16:22:07
问题 I've found that singleton pattern on the net. It seems to me it has many things that can be optimized. -In sharedMySingleton method, no need to call a retain ? I'm not sure... -If not, why is there a retain in allocWithZone ? -what is the use of @synchronized . The NSAssert make think that the block can be called many times, so if yes, there should be some more code to release previous memory, or exit the block clearly without just NSAsserting, and if no, why is there this NSAssert ? -the

Php: singleton VS full static class? When use what?

眉间皱痕 提交于 2019-12-21 16:18:00
问题 I understand that singleton enforces a class to be created once. But why should an instance exists if I dont access it directly? Why is that pattern for, isn't it easier just use full static class with static methods and datas? 回答1: Some time ago I was asked what is the benefit of using singleton over of using static class, here is my response: Static class leads to invisible dependencies - that is a class that use the static class, but that class is not part of the class' interface.

When is a static nested class (and static members therein) loaded into memory?

你。 提交于 2019-12-21 07:58:12
问题 Here, I was trying to implement a singleton class for my Database connectivity using the inner static helper class : package com.myapp.modellayer; public class DatabaseConnection { private DatabaseConnection() { //JDBC code... } private static class ConnectionHelper { // Instantiating the outer class private static final DatabaseConnection INSTANCE = new DatabaseConnection(); } public static DatabaseConnection getInstance() { return ConnectionHelper.INSTANCE; } } However, my doubt is when

Singleton Inheritance

 ̄綄美尐妖づ 提交于 2019-12-21 07:55:58
问题 How do I inherit from a singleton class into other classes that need the same functionality? Would something like this make any sense? 回答1: Jon Skeet wrote about this a while back. It is possible to achieve some of the benefits of inheritance with the Singleton, although using nested inner classes does leave a little to be desired. It doesn't have infinite extensibility, it's only a technique for having a Singleton choose its own implementation at runtime. Realistically, inheriting from a

Thread-safe initialization of static variables

南笙酒味 提交于 2019-12-21 07:10:08
问题 I've been using this pattern to initialize static data in my classes. It looks thread safe to me, but I know how subtle threading problems can be. Here's the code: public class MyClass // bad code, do not use { static string _myResource = ""; static volatile bool _init = false; public MyClass() { if (_init == true) return; lock (_myResource) { if (_init == true) return; Thread.Sleep(3000); // some operation that takes a long time _myResource = "Hello World"; _init = true; } } public string