Questions tagged [singleton-methods]

Methods related to using of the singleton design pattern.

76 questions
3
votes
1 answer

Add class methods through include

I have Ruby class into which I want to include both class and instance methods. Following the pattern described here, I'm currently using the following: class SomeObject include SomeObject::Ability def self.some_builder_method(params) #…
user2398029
  • 6,699
  • 8
  • 48
  • 80
2
votes
1 answer

Deserializing Singleton

//Singleton public class MainList implements Serializable { private static MainList instance = new MainList(); private MainList() {} public static MainList getInstance() { return…
2
votes
2 answers

Singleton Controller Java

I was reading about singletons the other day and thought to implement them in a project of mine, but I didn't like the way it seemed to flow logically, so I created what I'd call a controller class to manage the state of the singleton object. I want…
Adam R. Turner
  • 139
  • 1
  • 2
  • 14
2
votes
2 answers

Ruby: execute singleton method in different context

(edited to make question more specific) I would like to know if it's possible to execute a singleton method in the context of another object as in the following example: class A def initialize @foo = 'foo' end end def A.singleton(*args) …
coxy
  • 90
  • 5
2
votes
1 answer

iOS Singleton API manager with dispatch_async/asynchronous methods: what's the implication?

It is a typical design pattern to use a Singleton to represent an "API Manager" object like this:- class APIManager { let ENDPOINT_URL = "http://remote-server-fqdn.com" var storage : Storage! var currentlyLoggedIn = false class var…
Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
2
votes
2 answers

Java Fluent API - better method?

Say I have an object that I've created to further simplify reading an XML document using the DOM parser. In order to "step into" a node or element, I'd like to use a single line to go from the start of the document to my target data, buried…
DGolberg
  • 2,109
  • 4
  • 23
  • 31
2
votes
1 answer

Why can't I attach a singleton method to another object?

some_string = "i love lamp" another_string = "i love desk" def some_string.lovehate if match /love/ sub /love/, "hate" elsif match /hate/ sub /hate/, "love" end end puts some_string.lovehate # => "i hate lamp" puts…
87linux
  • 23
  • 3
2
votes
7 answers

Best way to call a method inside a singleton class

I have a singleton class public class Singleton { static Singleton instance; public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } Consider a public function…
Arundas K V
  • 801
  • 2
  • 14
  • 28
2
votes
2 answers

Singleton Implementation. Blocking the alloc and init methods for external usage

I have a Class that I wish to implement as Singleton. I wish that the only way an Instance of this class may be created / accessed is via: +(MYClass*)sharedInstance method. alloc and init are called within the method implementation (of course). Is…
Ohad Regev
  • 5,641
  • 12
  • 60
  • 84
2
votes
1 answer

Where does `singleton` methods reside in Ruby?

I was playing with singleton class in my IRB. And doing so tried the below snippets. class Foo ; end #=> nil foo = Foo.new #=> # foo.define_singleton_method(:bar , method(:puts)) #=> # Here above I just…
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
2
votes
1 answer

Difference between def myobject.mymeth and class << myobject; def mymeth

I am currently practicing/learning singleton methods and singleton classes in Ruby, and I have come across a doubt. Given: myobject = Object.new Is there any difference between doing this: def myobject.mymethod end and doing this: class <<…
Nobita
  • 23,519
  • 11
  • 58
  • 87
1
vote
1 answer

Singleton method error from bind when called with the same metaclass

I'm writing aspect-ish code more or less lifted from the chosen solution for this question that looks like the following: class Module def add_logging klass, *method_names method_names.each do |method_name| original_method =…
iftheshoefritz
  • 5,829
  • 2
  • 34
  • 41
1
vote
1 answer

Objective-C Singleton problem. Object recognized in one class but not another

My problem is that I can access methods and attributes from the sharedInstance Singleton in one class but I cannot in another. For example, the code below works and is recognized by X-code. Works fine. return [[[SINGLETON…
1
vote
0 answers

Should I go tightly coupled, with singleton, or loosely coupled, without a singleton? Azure Services, Xamarin Forms

I am making a Xamarin Forms Cross platform app. I have been trying to figure out how to go about getting my app to integrate with the Azure Backend the best way. I have ran into a problem. It has been drilled into my head to try to build programs as…
1
vote
1 answer

Defining methods on instances of Fixnum's

I can define a method on an instance like this: object = Object.new def object.foo puts "5" end Trying something similar with a Fixnum doesn't work: def 3.foo puts "3" end def 3.foo ^ (irb):7: syntax error, unexpected keyword_end,…
Nat
  • 890
  • 3
  • 11
  • 23