Questions tagged [singleton-methods]

Methods related to using of the singleton design pattern.

76 questions
12
votes
4 answers

Help understanding class method returning singleton

Can someone please help me understand what the following method is doing? + (Game *) shared { static Game *sharedSingleton; @synchronized(self) { if (!sharedSingleton) { sharedSingleton = [[Game alloc]…
5lb Bass
  • 539
  • 8
  • 15
12
votes
2 answers

Is it possible to define a Ruby singleton method using a block?

So, I want to define a singleton method for an object, but I want to do it using a closure. For example, def define_say(obj, msg) def obj.say puts msg end end o = Object.new define_say o, "hello world!" o.say This doesn't work because…
Mike Stone
  • 44,224
  • 30
  • 113
  • 140
10
votes
1 answer

How to find private singleton methods

I have defined a module Vehicle like such module Vehicle class <
Mike Bradford
  • 240
  • 2
  • 10
8
votes
6 answers

concurrent calls of singleton class methods

I have a singleton class: public class Singleton { private static Singleton istance = null; private Singleton() {} public synchronized static Singleton getSingleton() { if (istance == null) istance = new…
Luky
  • 5,346
  • 4
  • 17
  • 15
5
votes
2 answers

C# Singleton Pattern Designs for ThreadStatic

I want to figure out about singleton pattern designs. I want to create seperated instances for per thread from my singleton class. So I provided two designs below. It is Working class Program { static void Main(string[] args) { …
lucky
  • 12,734
  • 4
  • 24
  • 46
4
votes
1 answer

Access to singleton with static functions

Consider I have a singleton class Foo. The question here is not about the way to implement the singleton idiom, then I don't explicit it. I have somthing like that: class Foo { private: Foo(); ~Foo(); public: void …
Caduchon
  • 4,574
  • 4
  • 26
  • 67
4
votes
7 answers

what is the need of private constructor in singleton design pattern?

when i go through the below code, i couldnt find the reason why it using private constructor in the sample? public sealed class Singleton { private static Singleton instance = null; private Singleton() { } …
Tom Cruise
  • 1,395
  • 11
  • 30
  • 58
4
votes
2 answers

How to multi-thread "Cross-tier" a singleton with a correlation id without locking?

So we are using a common state context singleton with a correlation ID for centralized Logging. The purpose is to track the id throughout our entire process and correlate the different tiers. The state context is accessed by multiple dlls and…
Demodave
  • 6,242
  • 6
  • 43
  • 58
4
votes
4 answers

Are all singleton methods public?

Is a singleton method necessarily public? If not, when would a private/protected singleton method be useful?
sawa
  • 165,429
  • 45
  • 277
  • 381
4
votes
1 answer

Are there any exceptions to singleton methods in Ruby?

In Ruby everything is an object. But when I try a singleton method on a number, I get type error. Are there any exceptions to the notion of everything is an object? a_str = "Ruby" a_num = 100 def a_str.bark puts "miaow" end a_str.bark #=> miaow…
Bala
  • 11,068
  • 19
  • 67
  • 120
4
votes
2 answers

How does the `Object#define_singleton_method(symbol, method)` works in ruby?

From the Doc of define_singleton_method I got two syntax to define the singleton method as below : define_singleton_method(symbol) { block } -> proc : With the above syntax I tried the below code and the syntax I…
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
3
votes
3 answers

Ruby singleton class

I am unsure about the difference between this. def String.hello puts "hello there" end and x = Person.new def x.hello puts "hello there" end From my understanding the second code block will create an object of class Person.…
slindsey3000
  • 4,053
  • 5
  • 36
  • 56
3
votes
3 answers

Module methods in Ruby

Here is my code: module Star def Star.line puts '*' * 20 end end module Dollar def Star.line puts '$' * 20 end end module At def line puts '@' * 20 end end include At Dollar::line # => "@@@@@@@@@@@@@@@@@@@@" Star::line …
Nishant Upadhyay
  • 639
  • 7
  • 20
3
votes
2 answers

Ruby class << abcd syntax

I know there have been other questions about the syntax class << self. Still, I did not find those answers clear enough. I have a background in Java/C#, C, so Ruby is kinda strange to me. I read that class << self refers to the singleton class. I…
BrunoMCBraga
  • 652
  • 2
  • 7
  • 23
3
votes
1 answer

Singleton Thread Safety

My question regards thread safety with class functions. This is some test code i have knocked together to try and get a better understanding. public sealed class Singleton { public static Singleton Instance { get { return _lazyInit.Value; }…
TBD
  • 771
  • 1
  • 11
  • 27
1
2 3 4 5 6