Created
February 9, 2016 09:37
-
-
Save 53ningen/49b38ab9d445caf34297 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
/// DEFINE protocol A, class AImpl | |
protocol A { func getA() -> String } | |
private class AImpl: A{ func getA() -> String { return "a" } } | |
protocol AComponent { | |
static var a: A { get } | |
static func createA() -> A | |
} | |
/// DEFINE AComponent, DefaultAComponent | |
protocol DefaultAComponent: AComponent {} | |
extension DefaultAComponent { | |
static func createA() -> A { return AImpl() } | |
} | |
/// DEFINE protocol AB, class SimpleAB, class ABImpl | |
protocol AB { func getAB() -> String } | |
private class SimpleAB: AB { func getAB() -> String { return "AB" } } | |
private class ABImpl: AB { | |
let a: A | |
init(a: A) { self.a = a } | |
func getAB() -> String { return a.getA() + "b" } | |
} | |
/// DEFINE protocol ABComponent, DefaultABComponent, SimpleABComponent | |
protocol ABComponent { | |
static var ab: AB { get } | |
static func createAB() -> AB | |
} | |
protocol DefaultABComponent: ABComponent {} | |
extension DefaultABComponent where Self: AComponent { | |
static func createAB() -> AB { return ABImpl(a: a) } | |
} | |
protocol SimpleABComponent: ABComponent {} | |
extension SimpleABComponent { | |
static func createAB() -> AB { return SimpleAB() } | |
} | |
/// DEFINE context | |
typealias ContextType = protocol<AComponent, ABComponent> | |
class Context { | |
private let type: ContextType.Type | |
init(type: ContextType.Type) { self.type = type } | |
var a: A { return type.a } | |
var ab: AB { return type.ab } | |
} | |
class DebugContext: DefaultAComponent, DefaultABComponent { | |
static let a: A = DebugContext.createA() | |
static let ab: AB = DebugContext.createAB() | |
} | |
class ReleaseContext: DefaultAComponent, SimpleABComponent { | |
static let a: A = ReleaseContext.createA() | |
static let ab: AB = ReleaseContext.createAB() | |
} | |
let debugContext = Context(type: DebugContext.self) | |
debugContext.ab.getAB() | |
let releaseContext = Context(type: ReleaseContext.self) | |
releaseContext.ab.getAB() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment