Questions tagged [singleton-methods]

Methods related to using of the singleton design pattern.

76 questions
1
vote
1 answer

Defining singleton methods

The following code is from the book Exceptional Ruby: starts_with_a = Object.new def starts_with_a. ===(e) /^A/ =~ e.name end If I comment out the first line where a new object is assigned to starts_with_a, then I get this error: `
':…
BrainLikeADullPencil
  • 11,313
  • 24
  • 78
  • 134
0
votes
2 answers

Coin Flip Statistics Program - undefined method

I'm trying to write a coin flip program where I can analyze the percentage of Heads flipped. I've gotten coin flip to work, just not the actually analyzing yet. The problem is when I created a Coin class in order to further break down the object…
Tony
  • 981
  • 5
  • 16
  • 30
0
votes
2 answers

Preventing a function from running multiple times in JavaScript

I'm working with some code that does this: var _init = false; this.init = function(){ if(_init) return; else _init = true; // Do a bunch of stuff here } It seems to me that there is a tiny race condition there that…
Trott
  • 66,479
  • 23
  • 173
  • 212
0
votes
1 answer

Is there any way to access the Parent object whom called a singleton method?

Given the following functional snippet I'm having trouble reducing the database queries: class User < ApplicationRecord belongs_to :account def self.do_something self.find_each do |user| puts "#{self.new.account.name}:#{user.name} did…
0
votes
2 answers

Can clone object have same scope in singleton pattern?

In my case, I created a singleton object check below- class Foo { private static $obj = null; public static function create_obj () { if (self::$obj === null) { self::$obj = new self; } return…
Devraj verma
  • 407
  • 3
  • 14
0
votes
2 answers

Turn Employee Data List Into Singleton Class App

I have made an list employee data app in C# with functions like AddEmployee, DeleteEmployee, SearchEmployee. I would like to make the same program, but using Singleton design pattern. I know the structure of Singleton, but I messed up with making…
Kaloyan
  • 41
  • 5
0
votes
2 answers

Ruby: Singleton method of an instance variable inside a class

I am taking ruby-kickstart (Josh Cheek) challenges and even though I managed to pass all the test there is one thing I cannot understand. In the exercise you are being asked to override the << method for an instance variable. Specifically here is…
0
votes
2 answers

Java - Infinity Loop Singleton class method call

I've a question about singleton class. When I call method next(), I expect that mProcessIndex will be increased but in reality, it won't so it causes stackoverflow error. So, the question is what is correct way to modify mProcessIndex value? Code…
0
votes
0 answers

Is it okay to have extra static methods in a singleton class?

I have written a singleton class (in objective-C) with methods: //shared instance +(void) sharedInstance; //method to get all feeds. -(NSArray*)getAllFeedItems; So to use this I have to call [[MyClass sharedInstance] getAllFeedItems]; Now instead…
0
votes
6 answers

ruby hash within hash and a singleton method- cant access instance variable

#!/usr/bin/env ruby # this is the data I have @data = { :student => { :id => '123477', :first_name => 'Lazlo', :last_name =>'Fortunatus', :email=>'Lazlo@fortunatus.org' }, :contact_info => { :telephone=>'1 415 222-2222', …
hidden
  • 3,216
  • 8
  • 47
  • 69
0
votes
1 answer

Is it bad to wrap singleton instance methods in static methods?

Is there a con to implementing a singleton that wraps instance methods with static ones? For example: public void DoStuff() { instance._DoStuff(); } private void _DoStuff() { ... } And of course instance would be static. But it…
sk84z
  • 193
  • 1
  • 3
  • 10
0
votes
1 answer

how to maintain connection(created in singleton class)in the next page

I am trying to develop a windows chat app in wp8.1 in my first page i created a server connection in singleton class.I created another window for sending messages .using singleton how can i maintain the server connection in the next page also? my…
0
votes
3 answers

static method vs singleton methods in multithreaded environment

I have a code sample with static methods and singleton class //code with static methods public class DataManager{ public static Object getDataObject(){ HashMap map = new HashMap(); //Some processing in collections //retrieval of…
0
votes
1 answer

When is a singleton class object garbage collected?

If a single instance of class is created using singleton pattern, if it is not referenced for a long time, if a GC finds an unrooted tree whose leaf is the Singleton instance, will it be garbage collected?
Free Coder
  • 459
  • 4
  • 19
0
votes
1 answer

Obj C: Unable to access singleton from debugger

So this is very odd. I just created a new singleton in my app, using the same pattern I have used for the numerous other singletons. This one, however, doesn't play nice with the debugger. Here's the code for getting the singleton: - (id)init { …
Darren Black
  • 1,030
  • 1
  • 9
  • 28