The singleton is a design pattern to ensure that exactly one application-wide instance of a particular class exists (optionally with additional features such as thread-safe intialization, or some form of initialization order). Singletons are arguably the most well-known, the most used, the most ...
0
votes
2answers
36 views
Is this code thread-safe - Singleton Implementation using Concurrent Dictionary
class Connection
{
private string param1;
private string param2;
private static readonly ConcurrentDictionary<Tuple<string, string>, Connection>
connections = new ...
4
votes
3answers
120 views
Is this correct implementation of singleton using enum?
public class Item3 {
public static void main(String[] args) {
Singleton s=Singleton.Single.INSTANCE.getInstance();
Singleton s2=Singleton.Single.INSTANCE.getInstance();
...
3
votes
1answer
99 views
Prime Numbers Store
Problem definition:
Lets say we need to create a store for selling prime numbers.
Users enter the store and ask to buy a number.
If the number asked is a prime number,
1.1. then it's either ...
0
votes
2answers
95 views
Singleton interface in Java
I've created a singleton by means of an interface. I don't want to make a thing with getInstance() etc because I don't care about inheritance, and it's redundant and non-declarative.
Are there ...
0
votes
0answers
35 views
Abstract class which uses a abstract factory -> New implementation with Singleton
I have a abstract class which is extended by many many other classes:
What I have done:
public abstract class AbstractActionHandler {
protected WorkItem currentWI;
protected String status;
...
0
votes
0answers
58 views
Mixin both instance and class methods in Ruby
I have a Ruby class into which I want to include both class and instance methods. Following the pattern described in "Ruby Pattern: Extend through Include", I'm currently using the following:
class ...
5
votes
1answer
338 views
WPF Single Instance Best Practices
This is the code I implemented so far to create a single instance WPF application:
#region Using Directives
using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
...
0
votes
2answers
97 views
PHP my way of threating static classes
A few days ago I've faced an annoying problem.
Let's imagine we have 3 classes: Base, System, Handler.
The classes Systen and Handler both inherit from the class Base.
I want to access those classes ...
2
votes
2answers
128 views
Objective-C Singleton Implementation
is this the right way to do a singleton in Objective-C ? (coming from Android background, with a little understanding of threading)
#import "ItemsManager.h"
#define kNumMaxSelectableItems 15
...
1
vote
1answer
216 views
Why is android.app.Application not singleton by default [closed]
In many Android applications which I have found online or written myself, there is an application class like this:
public class MyApp extends Application {
private static MyApp instance;
...
4
votes
2answers
206 views
Objective-C retain / release snippet
Here are some snippets I coded and I would like some feedback on my way of handling this:
I have a utility class, as a singleton, that provides me with a method named randomColor which returns a ...
1
vote
2answers
39 views
A Better Way To Write This Date Object?
In my program, I will only ever need one instance of this so I used a singleton Object. However, is there a better way to write this so I don't have to use so much wording when retrieving the ...
4
votes
1answer
171 views
Pubsub implementation using a Singleton
Is using a singleton to represent a Publish-subscribe message system a bad design choice?
Background
Im thinking about writing a pubsub implementation (using the singleton pattern) to allow for ...
4
votes
2answers
645 views
Threadsafe DBContext in singleton
I found out the hardway that access to DbContext in .NET is not threadsafe. I have a singleton for logging things using a dbcontext. The original version uses something like
public Logger{
private ...
4
votes
2answers
283 views
C++ shared_singleton
I actually feel bad posting "yet another singleton"... I wrote the following one many years ago and had recently found another application for it. We had many threads, each running the same function ...
5
votes
3answers
270 views
Storage class, dependency injection and singletons
In re-writing my PHP framework vervPHP, I've created the following class:
class storage {
private static $instance;
public $db = null; // Holds a database connection object
public $user = ...
0
votes
1answer
316 views
Review of C++ singleton
Originally posted on Stack Overflow.
So far, I've used this in a few places and it seems solid.
This is what I usually do to make a singleton:
// Assume x86 for now
#ifdef _MSC_VER
#include ...
1
vote
2answers
179 views
C++ templated singleton class - properly destroyed?
I have written a templated singleton class in C++ but I am afraid that it is not properly destroyed. Can you advise me on that ?
my singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H
template ...
-1
votes
2answers
2k views
Thread-safe singleton class using std::shared_ptr in C++(11)
What I am trying to accomplish is to create an efficient thread-safe singleton base class (as stated in the title).
So this is my singleton class, which is used via inheritance (see below).
#pragma ...
0
votes
1answer
184 views
What exactly is this code doing? [closed]
I am making an Android Application which connects my android device to an embedded bluetooth chip. I have looked a lot online and in textbooks and have found several methods to send/receive data. I ...
2
votes
1answer
389 views
Is this a good implementation of a thread-safe singleton using the observer pattern?
I need a singleton that can safely operate in a multi-thread environment. Threading and concurrency is new to me, so I'm not sure if this implementation holds. Take a look:
public class ...
1
vote
1answer
236 views
Can this JavaScript singleton be improved?
I needed a singleton in JavaScript in order to make a DataManager which is called multiple times, but I just want it to load its data only the first time it is called, then give pieces of the data out ...
2
votes
2answers
125 views
best way to use singleton
I know 3 ways for using singleton in our code and I want to know which one is the best way and you prefer more.
The first is to have a Singleton class and every classes that want to be singleton ...
4
votes
2answers
430 views
Three-tier application with singleton pattern
I am creating a 3-Tier Windows Forms Application.
Questions
Am I using the right architecture, or can you suggest a better approach?
Is there any way to make the base class be a Singleton?
Is ...
7
votes
1answer
611 views
JavaScript Boilerplate - Review comments required
I am in between to create JavaScript Boilerplate (collection of best practices around) for low/medium complex project and will host on GitHub in sometime once finalized it.
Have divided the ...
0
votes
1answer
48 views
adding APL logger to an application [closed]
I have an application (Legacy code)
that contains interface Icomponent with save() methods
and many classes that implement it.
I want to add log after every save().
I thought to
use the ...
6
votes
4answers
341 views
A generic singleton - what do you think?
What do you guys think about this for a generic singleton?
using System;
using System.Reflection;
/* Use like this
public class Highlander : Singleton<Highlander>
{
private Highlander()
...
5
votes
3answers
463 views
Static methods or singleton?
I have class that handles HTTP requests:
public final class RestHttpClient {
/*there is no fields*/
/**
* @param mhttp - HTTP request that need to send
* @return HttpResponse, ...
1
vote
3answers
246 views
JavaScript singleton with methods and properties
I am diving deeper into JavaScript and starting to learn more about prototypes and simulating classes etc. I am used to working with the Module pattern and would like to try and combine this pattern ...
4
votes
1answer
1k views
Singleton class extending a parent class to utilise shared functionality
I have a singleton class which extends from an abstract java class. Two singleton classes extend from ItemImageThreadManager, the reason for this is to use shared scheduling functionality. A thread is ...
10
votes
2answers
5k views
Create thread safe singleton class
#include <boost/thread/mutex.hpp>
class Singleton
{
public:
static Singleton& GetInstance()
{
boost::mutex::scoped_lock lock(m_mutex);
static Singleton instance;
return ...
0
votes
1answer
245 views
How can I improve this code without using static or singleton
I used to use static or singleton for my DAL class. However, I read some articles saying that singleton is evil and should be avoided. Therefore I try to rewrite my code like this:
public class ...
2
votes
1answer
323 views
Singleton wrapper
Feel free to critique this database wrapper which is written as a code example for employers or clients.
<?php
class Database {
static function getInstance()
{
...
3
votes
4answers
916 views
Python, Singleton Decorator
I've written a class that makes writing singletons easy. The class is meant to be used as a decorator (example below). I'd like to see your thoughts about the code, the documentation, and everything ...
2
votes
5answers
414 views
Request for Comments: Singleton pattern implemented in Java
I have several utility classes who should only ever have one instance in memory, such as LogHelper, CacheHelper, etc. One instance to rule them all.
I've never implemented Singleton in Java before, ...
19
votes
7answers
1k views
Is my code a 'safe' singleton?
I was wondering if my code will produce a true singleton. I am creating an Android app, and all activities should access my API through one instance of the SyncApi class.
public class Api {
...
63
votes
9answers
11k views
Implementing a Singleton pattern in C#?
Is this the best way to implement this pattern in C#?
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
public static Singleton Instance { get ...

