Created
May 12, 2023 01:52
-
-
Save dokun1/5f63649d39b257e3dfa2cd61dc4b1674 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
struct Bakery { | |
func bakeBread() { | |
print("make some Bread") | |
} | |
} | |
struct Kitchen { | |
func cookPizza() { | |
} | |
func makePizza() { | |
bakery.bakeBread() | |
self.cookPizza() | |
} | |
var bakery: Bakery | |
init(bakery: Bakery) { | |
self.bakery = bakery | |
} | |
} | |
let theBestBakery = Bakery() | |
let theBestKitchen = Kitchen(bakery: theBestBakery) | |
theBestKitchen.makePizza() | |
protocol NetworkAccessible { | |
func fetch() | |
} | |
struct RealNetwork: NetworkAccessible { | |
func fetch() { | |
print("I am a real network request!") | |
} | |
} | |
struct FakeNetwork: NetworkAccessible { | |
func fetch() { | |
print("I am a fake network request!") | |
} | |
} | |
struct WebObject { | |
var network: NetworkAccessible | |
init(network: NetworkAccessible) { | |
self.network = network | |
} | |
func getData() { | |
network.fetch() | |
} | |
} | |
let realNetwork = RealNetwork() | |
let fakeNetwork = FakeNetwork() | |
let realWebObject = WebObject(network: realNetwork) | |
let fakeWebObject = WebObject(network: fakeNetwork) | |
realWebObject.getData() | |
fakeWebObject.getData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment