Last active
April 26, 2016 07:43
-
-
Save dydus0x14/976ed540001e3fb15f9a3b5ec203c124 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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