有人可以帮我理解下面的方法是做什么的吗?
+ (Game *) shared
{
static Game *sharedSingleton;
@synchronized(self)
{
if (!sharedSingleton)
{
sharedSingleton = [[Game alloc] init];
}
}
return sharedSingleton;
}
Run Code Online (Sandbox Code Playgroud) 所以,我想为一个对象定义一个单例方法,但我想用一个闭包来做.
例如,
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这样做的方式)
我已经定义了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?
编辑 解决:如何在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) 我有一个单身人士课程:
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) 我正在尝试定义单例别名方法.如:
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#+方法不使用它.强调别名,但将实际方法对象作为第二个参数发送也很酷.
我想弄清楚单身模式设计.我想从我的单例类为每个线程创建单独的实例.所以我在下面提供了两个设计.
这是工作
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) 当我浏览下面的代码时,我找不到它在示例中使用私有构造函数的原因?
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) 这是我的代码:
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 ×5
singleton ×4
c# ×2
closures ×1
cocoa-touch ×1
concurrency ×1
eigenclass ×1
go ×1
go-gorm ×1
inheritance ×1
ios ×1
java ×1
module ×1
objective-c ×1
threadstatic ×1