Last active
August 17, 2021 12:29
-
-
Save IhwanID/ab7b36f9f544ae049a08b690007d73a1 to your computer and use it in GitHub Desktop.
Intro to Dependency Diagrams and Composition
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
import UIKit | |
class FeedViewController: UIViewController{ | |
var loadFeed: ((([String]) -> Void) -> Void)! | |
convenience init(loadFeed: @escaping (([String]) -> Void) -> Void){ | |
self.init() | |
self.loadFeed = loadFeed | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
loadFeed{ item in | |
} | |
} | |
} |
Author
IhwanID
commented
Aug 17, 2021
import UIKit
protocol FeedLoader{
func loadFeed(completion: @escaping ([String]) -> Void)
}
class FeedViewController: UIViewController{
var remote: RemoteFeedLoader!
var local: LocalFeedLoader!
convenience init(remote: RemoteFeedLoader, local: LocalFeedLoader){
self.init()
self.remote = remote
self.local = local
}
override func viewDidLoad() {
super.viewDidLoad()
if true {
remote.loadFeed { items in
}
}else{
local.loadFeed { items in
}
}
}
}
class RemoteFeedLoader: FeedLoader{
func loadFeed(completion: @escaping ([String]) -> Void) {
}
}
class LocalFeedLoader: FeedLoader{
func loadFeed(completion: @escaping ([String]) -> Void) {
}
}
import UIKit
protocol FeedLoader{
func loadFeed(completion: @escaping ([String]) -> Void)
}
class FeedViewController: UIViewController{
var loader: FeedLoader!
convenience init(loader: FeedLoader){
self.init()
self.loader = loader
}
override func viewDidLoad() {
super.viewDidLoad()
loader.loadFeed { items in
}
}
}
class RemoteFeedLoader: FeedLoader{
func loadFeed(completion: @escaping ([String]) -> Void) {
}
}
class LocalFeedLoader: FeedLoader{
func loadFeed(completion: @escaping ([String]) -> Void) {
}
}
class RemoteWithLocalFallbackFeedLoader: FeedLoader{
let remote: RemoteFeedLoader
let local: LocalFeedLoader
init(remote: RemoteFeedLoader, local: LocalFeedLoader) {
self.remote = remote
self.local = local
}
func loadFeed(completion: @escaping ([String]) -> Void) {
let load = Reachability.networkAvailable ? remote.loadFeed : local.loadFeed
load(completion)
}
}
struct Reachability{
static let networkAvailable = false
}
let vc = FeedViewController(loader: RemoteFeedLoader())
let vc1 = FeedViewController(loader: LocalFeedLoader())
let vc2 = FeedViewController()
vc2.loader = RemoteWithLocalFallbackFeedLoader(remote: RemoteFeedLoader(), local: LocalFeedLoader())
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment