Is there a way to set up a global variable inside of a module? When I tried to do it the most obvious way as appears below, the Python interpreter said the variable __DBNAME__ did not exist.
...
__DBNAME__ = None
def initDB(name):
if not __DBNAME__:
__DBNAME__ = name
else:
raise RuntimeError("Database name has a...
We have 3 different libraries, each developed by a different developer, and each was (presumably) well designed. But since some of the libraries are using RAII and some don't, and some of the libraries are loaded dynamically, and the others aren't - it doesn't work.
Each of the developers is saying that what he is doing is right, and ma...
So my clients iphone app has balloned from 5 or so classes to over 25 in the last few weeks. With such a large (for the iphone anyway) class structure I've been utilizing the Singleton class for accessing global variables. The problem is that whenever I need to access a variable outside of the class I'm working on, I have a choice of e...
Can anyone provide an example of a singleton pattern and explain why they are necessary?
...
The Java EE 6 Tutorial says:
To improve performance, you might choose a stateless session bean if it has any of these traits:
The bean’s state has no data for a specific client.
In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an ema...
Hi guys,
I am wondering if it is possible to have a static class instantiate another class for the purpose of holding a reference to it globally. I have a data store which is composed of an in-memory object and would like to access it from different locations. The data needs to persist changes to the application so it needs to be instan...
Here's the one I'm using:
<?php
final class Database {
private static $oDb;
public static function init() {
if(self::$oDb == NULL)
{
self::$oDb = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error());
mysql_select_db('mysql_db_name', self::$oDb) or die (mysql_err...
Hi,
I wonder, what are the pros and the cons of these two approaches to access variables:
1) CodeIgniter -style (does this have a name?)
public function doSomething()
{
$database = $this->database; // or just $this->database
}
2) The Singleton pattern
public function doSomething()
{
$database = Framework_Database::getInstance();
...
Hi.
I declare a singleton on a MFC extension DLL, like this:
//header file: SingleTon.h
class AFX_EXT_CLASS CMySingleton
{
public:
static CMySingleton* Instance()
{
if(!singleton)
singleton = new CMySingleton();
return singleton;
}
int a;
// Other non-static member functions
private:
CMySingleton() {}; ...
I'm writing a .net compact framework app in c#. It was working fine in the development environment but when built in release mode and run by itself, it was throwing MethodNotFound exceptions. I sprinkled a bunch of debug logging code to find out where it was breaking and narrowed it down to a big Init() function. This in turn calls metho...
Hi ,
I was refering to a solution from wikipedia for Singleton Pattern:
public class Singleton
{
// Private constructor prevents instantiation from other classes
private Singleton() {}
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INST...
I am writing a unit test by mocking out external services
com.example.Service service;
service = RealServiceClient.getService().getServiceId("1");
How can I simulate the above RealService class?
The thing is RealServiceClient.getService() returns RealService.
PS: I am new to Java.
...
The standard way of doing singletons in Python is
class Singleton(object):
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
return cls._instance
However, this doesn't work on App Engine, since there are may ...
I wanna create a single instance of my class.How can i create a single instance of a class.
...
I have decided to use the singleton pattern for my application. It makes the most sense to me. However, just when I feel like I have made some progress I run into another wall.
I have a load function. The load function does the following.
Check if class has been previously loaded.
- If so - return $class::get_instance();
- Otherwise -...
So I'm finding that I'm using a singleton User class to access global variables throughout an iphone app. In some cases I use the instance in almost all the methods within a class. So in the first few lines of each method I have statement such as:
User *user = [User sharedManager];
This is obviously not a very DRY implementation. My...
I have a few classes in a project that should be created only once.
What is the best way to do that?,
They can be created as static object.
Can be created as singleton
Can be created as global.
What is the best design pattern to implement this?
I am thinking of creating all classes as singleton, but that would create lot of singlet...
Possible Duplicate:
On Design Patterns: When to use the Singleton?
This question is not about whether or not singletons are "considered harmful" in general. I just want to know, from your experience, what are some SPECIFIC SITUATIONS in which singletons seem to work well.
EDIT:
Please, if you want to discuss the appropriateness...
Ok, I'm trying to avoid global variables, so I read up on singleton classes.
This is a try to set and read a mutable array, but the result is null.
//Content.h
@interface Content : NSObject {
NSMutableArray *contentArray;
}
+ (Content *) sharedInstance;
- (NSMutableArray *) getArray;
- (void) addArray:(NSMutableArray *)mutableArr...
I have prepared some authomatic tests with the Visual Studio Team Edition testing framework. I want one of the tests to connect to the database following the normal way it is done in the program:
string r_providerName = ConfigurationManager.ConnectionStrings["main_db"].ProviderName;
But I am receiving an exception in this line. I supp...