Skip to content

Instantly share code, notes, and snippets.

@Kdan
Created July 5, 2020 11:20
Show Gist options
  • Select an option

  • Save Kdan/0880685f140e7501fa993f99fabba314 to your computer and use it in GitHub Desktop.

Select an option

Save Kdan/0880685f140e7501fa993f99fabba314 to your computer and use it in GitHub Desktop.
class A {
/// A `Bool` indicating if we have the answer to the universe.
private(set) var hasAnswer = false
/// The instance of B we are using to check if we have the answer.
private let b: B
init(b: B) {
self.b = b
}
/// Checks if we have the answer to the universe and updates the `hasAnswer` property.
func checkAnswer() {
hasAnswer = b.validateAnswer()
}
}
class ConcreteB: B {
/// The instance of `C` used to get the number.
private let c: C
init(c: C) {
self.c = c
}
/// Validates if `C` has the answer.
func validateAnswer() -> Bool {
c.number == 42
}
}
class ConcreteC: C {
/// The shared singleton instance of `ConcreteC`.
static let shared = ConcreteC()
/// The answer to the universe.
var number: Int { 42 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment