Skip to content

Instantly share code, notes, and snippets.

@dydus0x14
Last active April 26, 2016 07:43
Show Gist options
  • Save dydus0x14/976ed540001e3fb15f9a3b5ec203c124 to your computer and use it in GitHub Desktop.
Save dydus0x14/976ed540001e3fb15f9a3b5ec203c124 to your computer and use it in GitHub Desktop.
enum ControlFactoryError: ErrorType {
case TypeNotFound
}
protocol ControlFactory {
func create<T>(t: T.Type) throws -> UIView
}
class RealControlFactory: ControlFactory {
typealias FactoryItem = ()->(UIView)
var options = [String: FactoryItem]()
func create<T>(t: T.Type) throws -> UIView {
guard let factory = options["\(t)"] else {
throw ControlFactoryError.TypeNotFound
}
return factory()
}
func option<T>(t: T.Type, factory: ()->(UIView)) -> Self {
options["\(t)"] = factory
return self
}
}
// Example
let rf = RealControlFactory()
.option(Int.self, factory: { return UIView() })
.option(String.self, factory: { return UIView() })
let f: ControlFactory = rf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment