Skip to content

Instantly share code, notes, and snippets.

@53ningen
Last active March 15, 2016 16:06
Show Gist options
  • Save 53ningen/a6df95dbe8e7f503d1fe to your computer and use it in GitHub Desktop.
Save 53ningen/a6df95dbe8e7f503d1fe to your computer and use it in GitHub Desktop.
Playgroundで動きます (Xcode7.2)
public protocol A {
func getA() -> String
}
private class AImpl: A {
private func getA() -> String {
return "a"
}
}
public protocol AB {
func getAB() -> String
}
private class ABUsingA: AB {
private let a: A
private init(a: A) {
self.a = a
}
private func getAB() -> String {
return a.getA() + "b"
}
}
private class SimpleAB: AB {
private func getAB() -> String {
return "AB"
}
}
public protocol AComponent {
static var a: A { get }
}
public protocol DefaultAComponent: AComponent {}
extension DefaultAComponent {
public static var a: A {
return AImpl()
}
}
public protocol ABComponent {
static var ab: AB { get }
}
public protocol DefaultABComponent: ABComponent {}
extension DefaultABComponent where Self: AComponent {
public static var ab: AB {
return ABUsingA(a: a)
}
}
public protocol SimpleABComponent: ABComponent {
static var ab: AB { get }
}
extension SimpleABComponent {
public static var ab: AB {
return SimpleAB()
}
}
public typealias Components = protocol<AComponent, ABComponent>
public enum DefaultComponents: DefaultAComponent, DefaultABComponent {}
let defaultComponents: Components.Type = DefaultComponents.self
defaultComponents.a.getA()
defaultComponents.ab.getAB()
public enum SimpleComponents: DefaultAComponent, SimpleABComponent {}
let simpleComponents: Components.Type = SimpleComponents.self
simpleComponents.a.getA()
simpleComponents.ab.getAB()
@53ningen
Copy link
Author

public typealias User = String

public protocol UserApiClient {
    func getUsers() -> [User]
}

private struct UserApiClientImpl: UserApiClient {
    func getUsers() -> [User] { return ["cocoa", "chino"] }
}

public protocol UsesUserApiClient {
    var userApiClient: UserApiClient { get }
}

public protocol MixInUserApiClient: UsesUserApiClient {
    var userApiClient: UserApiClient { get }
}
extension MixInUserApiClient {
    var userApiClient: UserApiClient { return UserApiClientImpl() }
}

public protocol UserRepository {
    func getUsers() -> [User]
}

private protocol UserRepositoryImpl: UserRepository {}
extension UserRepositoryImpl where Self: UsesUserApiClient {
    func getUsers() -> [User] { return ["master"] + self.userApiClient.getUsers() }
}

public protocol UsesUserRepository {
    var userRepository: UserRepository { get }
}

public protocol MixInUserRepository {
    var userRepository: UserRepository { get }
}
private struct _UserRepository: MixInUserApiClient, UserRepositoryImpl {}
extension MixInUserApiClient {

    var userRepository: UserRepository { return _UserRepository() }
}


public typealias Components = protocol<UsesUserRepository>
struct DefaultComponents: Components, MixInUserApiClient, MixInUserRepository {}

let c: Components = DefaultComponents()
c.userRepository.getUsers()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment