标签: singleton-methods

帮助理解返回单例的类方法

有人可以帮我理解下面的方法是做什么的吗?

+ (Game *) shared
{
    static Game *sharedSingleton;

    @synchronized(self)
    {
        if (!sharedSingleton)
        {
            sharedSingleton = [[Game alloc] init];
        }
    }

    return sharedSingleton;
}
Run Code Online (Sandbox Code Playgroud)

singleton cocoa-touch objective-c singleton-methods ios

12
推荐指数
2
解决办法
6751
查看次数

是否可以使用块定义Ruby单例方法?

所以,我想为一个对象定义一个单例方法,但我想用一个闭包来做.

例如,

def define_say(obj, msg)
  def obj.say
    puts msg
  end
end

o = Object.new
define_say o, "hello world!"
o.say
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为通过"def"定义单例方法不是闭包,所以我得到一个例外,"msg"是一个未定义的变量或方法.

我想做的就是在Module类中使用"define_method"方法,但据我所知,这只能用于在类上定义方法...但我想要一个Singleton方法. ..

所以,我想写这样的东西:

def define_say(obj, msg)
  obj.define_singleton_method(:say) {
    puts msg
  }
end
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何实现这一点,而无需创建存储Proc的方法,然后在单例方法中使用Proc?(基本上,我想要一个干净,非hacky这样做的方式)

ruby closures singleton-methods

11
推荐指数
2
解决办法
5335
查看次数

如何找到私有单例方法

我已经定义了Vehicle这样的模块

module Vehicle
  class <<self
    def build
    end

    private

    def background
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

致电Vehicle.singleton_methods退货[:build].

如何检查定义的所有私有单例方法Vehicle

ruby singleton-methods eigenclass

9
推荐指数
1
解决办法
2232
查看次数

如何在GoLang中创建单例数据库类

编辑 解决:如何在GoLang中创建单例DBManager类.

我提到了如何创建单例的几个代码示例,但我希望在这些方法中使用方法并在单例引用上调用它们.我的守则如下

package dbprovider

import (
    "github.com/jinzhu/gorm"
    _"github.com/jinzhu/gorm/dialects/sqlite"
    "rest/article"
    "log"
)

type DBOperations interface {
    AddArticle(article *article.Article)
}

type DBManager struct {
    db            *gorm.DB
    isInitialized bool
}

var dbManagerInstance = new()

func GetDBManager() DBManager {
    return dbManagerInstance
}

func new() DBManager {
    localDbRef, err := gorm.Open("sqlite3", "../articles.db")
    if (err != nil) {
        panic("Error initializing db")
    } else {
        log.Print("DB Initialized successfully")
    }
    return DBManager{db:localDbRef, isInitialized:true}
}

func (dbManager DBManager)  AddArticle(article article.Article) (err error) {
    if (dbManager.isInitialized) {
        tx …
Run Code Online (Sandbox Code Playgroud)

singleton-methods go go-gorm

9
推荐指数
1
解决办法
5097
查看次数

单例类方法的并发调用

我有一个单身人士课程:

public class Singleton {
    private static Singleton istance = null;

    private Singleton() {}

    public synchronized static Singleton getSingleton() {
        if (istance == null)
            istance = new Singleton();
        return istance;
    }

    public void work(){
            for(int i=0; i<10000; i++){
                Log.d("-----------", ""+i);
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

多个线程正在调用work()函数:

public class Main {

public static void main(String[] args) {

    new Thread (new Runnable(){
        public void run(){
            Singleton s = Singleton.getSingleton();
            s.work();}
    }).start();

    System.out.println("main thread");

    new Thread(new Runnable() { 
         public void run() {
             Singleton s = …
Run Code Online (Sandbox Code Playgroud)

java concurrency singleton multithreading singleton-methods

6
推荐指数
2
解决办法
1万
查看次数

将方法别名为单个对象

我正在尝试定义单例别名方法.如:

name = 'Bob'
# I want something similar to this to work
name.define_singleton_method(:add_cuteness, :+)
name = name.add_cuteness 'by'
Run Code Online (Sandbox Code Playgroud)

我确信我可以将方法对象作为第二个参数传递.

我不想这样做

name.define_singleton_method(:add_cuteness) { |val| self + val }
Run Code Online (Sandbox Code Playgroud)

我想别名String#+方法不使用它.强调别名,但将实际方法对象作为第二个参数发送也很酷.

ruby metaprogramming singleton-methods pointer-aliasing

5
推荐指数
1
解决办法
289
查看次数

ThreadStatic的C#Singleton模式设计

我想弄清楚单身模式设计.我想从我的单例类为每个线程创建单独的实例.所以我在下面提供了两个设计.

这是工作

class Program
{
    static void Main(string[] args)
    {
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Console.ReadLine();
    }
}
public sealed class SingletonClass
{
    [ThreadStatic]
    private static SingletonClass _instance;

    public static SingletonClass Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new SingletonClass();
            }
            return _instance;
        }
    }
    private SingletonClass()
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

它不起作用(抛出NullReferenceException并且没有创建实例.)

class Program
{
    static void Main(string[] args)
    {
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Console.ReadLine();
    }
}
public sealed class SingletonClass
{
    [ThreadStatic]
    private …
Run Code Online (Sandbox Code Playgroud)

c# singleton multithreading singleton-methods threadstatic

5
推荐指数
1
解决办法
599
查看次数

所有单身方法都是公开的吗?

单身方法是否必须公开?如果不是,私有/受保护的单例方法何时有用?

ruby singleton-methods

4
推荐指数
1
解决办法
270
查看次数

单例设计模式中私有构造函数的需要是什么?

当我浏览下面的代码时,我找不到它在示例中使用私有构造函数的原因?

public sealed class Singleton
    {
        private static Singleton instance = null;
        private Singleton()
        {
        }

        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }

                return instance;
            }
        }
    } 
Run Code Online (Sandbox Code Playgroud)

...

  //Why shouldnt I use something like below.
  public class Singleton
  {
       private static Singleton instance = null;            

       static Singleton()
       {
       }

       public static Singleton Instance
       {
            get
            {
                if (instance == null)
                {
                     instance = new Singleton();
                }

                return instance;
            } …
Run Code Online (Sandbox Code Playgroud)

c# singleton design-patterns singleton-methods

4
推荐指数
2
解决办法
5368
查看次数

Ruby中的模块方法

这是我的代码:

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   # => "$$$$$$$$$$$$$$$$$$$$"
Dollar::line # => "@@@@@@@@@@@@@@@@@@@@"
line         # => "@@@@@@@@@@@@@@@@@@@@"
Run Code Online (Sandbox Code Playgroud)

谁能解释我是如何得到这个结果的?我不明白这里的方法查找流程.

ruby inheritance module singleton-methods

3
推荐指数
1
解决办法
103
查看次数