创建一个通用的单例 [英] Creating a generic singleton

查看:110
本文介绍了创建一个通用的单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个头部爆竹(对我来说)。基本上我想要从同一个类继承2个不同的单例。在任何一个我想使用本身派生的某个类。所以我有工具 AUtil:工具 BUtil:工具。并且 Singleton 用作 ASingleton 使用 AUtility 胃和 B 分别。我在各个方面都失败了。最后一次尝试是一个工厂模式,它将Swift 1.2简单地转换为Segfault:

$ p $协议可初始化{init()}
类A:初始化{
var x =A
需要init(){}
}
class B:可初始化{
var x =B
需要init(){}
}

class C {
let t:可初始化
init(t:可初始化){
self。 t = t
println(t)
}
func factory(){
println(t.dynamicType())
}
}

正如我所说的,我也尝试了以下模式:

  private let _SingletonSharedInstance = StaticClass()
$ b class class StaticClass {$ b $ class var sharedInstance:StaticClass {
return _SingletonSharedInstance
}
}

let s = StaticClass.sharedInstance

(这不是你看到的通用,但我所有的尝试都失败了,所以我展示了我的出发点。)



无论如何,我似乎是在厄运和死亡之间迷失了方向。

 协议可初始化:class {init()} 

private var instances = [String:Initializable ]()

func singletonInstance< T:Initializable>(_ ty:T.Type = T.self) - > T {
let name = NSStringFromClass(ty)
if let o =(instances [name] as?T){
return o
}
let o = ty ()
instances [name] = o
return o
}



<

  class Foo:Initializable {required init(){}} 
class Bar:Initializable {required init(){}}

let foo1 = singletonInstance()as Foo // or`singletonInstance(Foo.self)`
let foo2 = singletonInstance( )作为Foo
assert(foo1 === foo2)

让bar1 = singletonInstance()作为Bar
让bar2 = singletonInstance()作为Bar
assert(bar1 === bar2)

(我已经测试了上面的代码,并在Swift 1.2中使用它。)


This is a bit of a head banger (for me). Basically I want to have 2 different singletons that inherit from the same class. In either I want to use a certain class which itself is derived. So I have Utility and both AUtil:Utility and BUtil:Utility. And Singleton that is used as ASingleton using AUtility in its stomach and B respectively. I failed on all frontiers. The last attempt was a factory pattern which simply got Swift 1.2 to Segfault:

protocol Initializable { init() }
class A:Initializable {
  var x = "A"
  required init() {}
}
class B:Initializable {
  var x = "B"
  required init() {}
}

class C {
  let t:Initializable
  init(t:Initializable) {
    self.t = t
    println(t)
  }
  func factory() {
    println(t.dynamicType())
  }
}

As said I also tried to make the following pattern generic:

private let _SingletonSharedInstance = StaticClass()

class StaticClass {
  class var sharedInstance : StaticClass {
    return _SingletonSharedInstance
  }
}

let s = StaticClass.sharedInstance

(This one isn't generic as you see. But all my attempts failed and so I show my starting point.)

Anyway I seem to be lost between doom and death.

解决方案

Do you mean something like this?

protocol Initializable: class { init() }

private var instances = [String: Initializable]()

func singletonInstance<T: Initializable>(_ ty: T.Type = T.self) -> T {
    let name = NSStringFromClass(ty)
    if let o = (instances[name] as? T) {
        return o
    }
    let o = ty()
    instances[name] = o
    return o
}

An use-side of it, for instance.

class Foo: Initializable { required init() {} }
class Bar: Initializable { required init() {} }

let foo1 = singletonInstance() as Foo // or `singletonInstance(Foo.self)`
let foo2 = singletonInstance() as Foo
assert(foo1 === foo2)

let bar1 = singletonInstance() as Bar
let bar2 = singletonInstance() as Bar
assert(bar1 === bar2)

(I've tested the code above and got it to work in Swift 1.2.)

这篇关于创建一个通用的单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆