Created
September 20, 2020 08:55
-
-
Save MostafaMazrouh/614c4e948f612794d99db96a49ac1fe3 to your computer and use it in GitHub Desktop.
Dependency Inversion vs Dependency Injection
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
class A { | |
var b: B | |
init(b: B) { | |
self.b = b | |
} | |
func start() { | |
b.doSomething() | |
} | |
} | |
class B { | |
func doSomething() { | |
} | |
} |
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
protocol BInterface { | |
func doSomething() | |
} | |
class A { | |
var b: BInterface | |
init(b: BInterface) { | |
self.b = b | |
} | |
func start() { | |
b.doSomething() | |
} | |
} | |
class B: BInterface { | |
func doSomething() { | |
} | |
} | |
class C: BInterface { | |
func doSomething() { | |
} | |
} |
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
class A { | |
var b: B | |
init() { | |
b = B() | |
} | |
func start() { | |
b.doSomething() | |
} | |
} | |
class B { | |
func doSomething() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment