The singleton is a design pattern to ensure that exactly one application-wide instance of a particular class exists.

learn more… | top users | synonyms

-5
votes
1answer
71 views

Sum up all my web requests and make them static [on hold]

Context: I'm currently developing an iOS app in C#. I have different view controllers and sometimes also another view controller needs to make the same web request. I don't want to hold a reference ...
3
votes
1answer
65 views

Ensure only a single instance of static template class member

I am interacting with a C library, whose documentation states that certain functions are not thread safe. Since its functions deal heterogeneously with multiple types, I have written a template ...
1
vote
0answers
43 views

Singleton request validator and sanitizor class

I have tried to code a PHP singleton class for validating multiple/single fields (sanitizing is optional). When validating single field, on success it will return 1 (...
5
votes
2answers
95 views

Thread-safe Phoenix Singleton class template with Boost

I've implemented the phoenix-singleton as a class template with boost inspired by Modern C++ Design. The code compiles fine with MSVC 2013 - and it seems to work, but I'm new to multi-threaded ...
8
votes
1answer
194 views

Can we now `implement Singleton` using Java 8?

After my success with extending enum - I thought I would try to make Singleton creation as simple as possible since Java-8 makes ...
1
vote
2answers
94 views

Proper way to wait end of a service and singleton design

My code works fine and I would like to know if I chose the best strategy to implement my code, or if It can written better (more fast, more clear, best use of design pattern, etc). In my android ...
-1
votes
2answers
109 views

Using singleton and/or factory patterns in business logic layer [closed]

I understand the purpose of singleton and factory patterns, however I have doubts implementing it. My problem is that whenever I need to call a method from BLL, then I have create an instance of BLL ...
2
votes
1answer
48 views

Lazy singleton with BackboneJS

I am trying to have a Backbone.Model which is a singleton and I wish to instantiate it lazily. I only need to support modern browsers so please do not worry about the use of ES5 getter. Is this the ...
9
votes
5answers
1k views

Singleton Database class

Currently my team is building an application involving a database. We have to write a lot of data (approx 2,000,000 records) to the database and therefore we decided to open a connection once and ...
8
votes
3answers
373 views

Singleton Database wrapper

Before everybody yells at me about why I wrote a PDO wrapper class, it's to avoid writing try/catch, ...
3
votes
2answers
313 views

Correct way to implement a Singleton field

I have a helper class that does HTTP operations on Android. Every method uses a client field. I think it's a good idea to make this client a singleton since every method in the class uses it. The ...
4
votes
2answers
396 views

Singleton necessary in DAO

I am using a singleton in my DAO. Would I be better with static methods? Also, should I be worried about synchronization of the singleton and my data statures? ...
4
votes
0answers
88 views

CRTP object counter to track accidental singleton instantiation

I have used CRTP to implement an object counter for singleton classes. This way I can keep track of accidental attempts to instantiate more than one object. It is a header only class: ...
4
votes
2answers
122 views

Class refactoring including changing instance methods to class methods and adding singletons

My main goal with this refactor was to change instance methods that should have been class methods in the first place into class methods. My secondary goal was to add singletons to these classes ...
4
votes
2answers
76 views

Static variable representing program state accessed with multiple functions

I have this method that performs certain functions based on the state that the algorithm is currently at: ...
2
votes
2answers
128 views

PHP Trait Singleton

An implementation of the singleton pattern in PHP using a trait (added in 5.4). Is there anything missing, any ways to create a second copy of the class? ...
6
votes
1answer
167 views

Searching for songs on an Android application

In my application I need to search for some songs on my external storage card every now and then. On startup I read everything, however when it comes to new activities I thought I could just pass the ...
4
votes
2answers
114 views

Android global data

I have a huge list of song objects in my program and I need those objects in almost all activities. Well, at least a part of it up to everything. So I created a class which looks pretty much like ...
5
votes
3answers
128 views

Is this good design for custom email template?

I read effective Java by Joshua Bloch. And it said use enums over int constant. So I was thinking instead of using hard coded strings I could also use enums. The forgotpassword.txt and ...
1
vote
1answer
91 views

Singleton-like pattern for a project [closed]

I'm changing the inner implementation of a project. In order to do so, I've created an interface that will be implemented in 2 different ways. One of this ways is by class ...
7
votes
4answers
1k views

Sound manager for Android

I created a singleton class for managing sound effects on Android. This class will only be instanced and loaded once at the beginning, and each activity will use the loaded songs. I don't know either ...
3
votes
1answer
143 views

Singleton With Sub-Singletons

I've found myself wanting an easy way to implement chat into various apps, so I developed a set of classes for the firebase.com backend that make it easy for me to quickly set up the nuts and bolts of ...
6
votes
2answers
351 views

Singleton design pattern

I am a beginner in PHP-OOP and design patterns. I have got this basic piece of code in Singleton Pattern and I changed it in order to understand its behavior. I can see that it works as expected. ...
6
votes
1answer
190 views

A parametrized Config singleton

Here is an attempt to code a specific kind of Singleton - the one geared for our configuration needs. It needs to be initialized with a configuration location, do not allow copies or other instances, ...
4
votes
1answer
468 views

ResourceManager Singleton

As I understand creating lots of resource managers in C# may be a bad idea so I thought that the best thing to do would be to create a singleton for this. However, I'm not 100% if this is a good ...
6
votes
2answers
301 views

Simple Java Singleton

Below is a Singleton that I designed as a piece of sample code for a website I'm working on for fun. The goal is to use the Singleton to hold an Earth object since ...
3
votes
2answers
1k views

Using a singleton class to get and set program wide settings

The following code works and does what I want, but I'd like to confirm I'm using this OOP concept correctly. I'm using the following class to get and set some configuration parameters for my program. ...
3
votes
1answer
77 views

How do I avoid explicit type switching when operation based on state?

I've encountered a problem that I have to solve using dynamic_cast to invoke different functions, depending on the type of class State family. I was re-factoring a ...
0
votes
1answer
1k views

PHP starting OOP MySQLi singleton

this is my first post on the whole StackExchange network, so I might make some mistakes. And I'm also spanish spearker, so I'll probably have mistakes in my writting too.. Context: I'm starting with ...
6
votes
4answers
253 views

Singleton SpinLock: Making Random Thread-Safe

Is this a valid and safe use of .NET's System.Threading.SpinLock? Why am I doing this? Random's public methods are not ...
0
votes
1answer
117 views

Creating a singleton container class for intrer-class messaging

My question is for validation of my code (any loop holes / bugs) and guidance the best methodology to implement for my requirement. I am developing a Python application that will have many classes. ...
2
votes
2answers
445 views

I need code review on using singleton to access local database

As I get to some performance issues in my app and find that I use database access in a bad way. So I decided to move to singleton pattern. I need someone to review this code and confirm me that I made ...
2
votes
1answer
163 views

Singleton has logic and state, and logic has state

------edit--------- note: this is for a MUD client, so the commands are issued to the MUD game server -----end edit-------- For an overview, here's the structure of the project: ...
1
vote
1answer
1k views

PDO class for multiple databases

I have PDO class below: ...
3
votes
1answer
338 views

A parent class, a singleton subclass, and a subclass uses a builder [closed]

There are three classes and an interface. The parent class is called MobileSignal. It contains info about mobile signal. E.g., CID, LAC, device ID, MCC, user ...
6
votes
3answers
9k views
2
votes
4answers
435 views

Global data singleton

I need a global data in my application. My application uses several threads to access and add items to my global variable. The global variable is a ...
7
votes
1answer
254 views

Prime Numbers Store

Let's 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 available for sale 1.2. ...
0
votes
2answers
1k 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 ...
2
votes
1answer
458 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: ...
1
vote
1answer
136 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: ...
11
votes
1answer
6k views

Single-instance WPF application

This is the code I implemented so far to create a single instance WPF application: ...
0
votes
1answer
238 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
509 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) ...
0
votes
1answer
878 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: ...
4
votes
2answers
247 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 ...
4
votes
1answer
732 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
4k 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 ...
5
votes
2answers
908 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 ...