Skip to content

Instantly share code, notes, and snippets.

@IhwanID
Last active August 17, 2021 12:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save IhwanID/ab7b36f9f544ae049a08b690007d73a1 to your computer and use it in GitHub Desktop.
Intro to Dependency Diagrams and Composition
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
}
}
}
@IhwanID
Copy link
Author

IhwanID commented Aug 17, 2021

import UIKit

typealias FeedLoader = (([String]) -> Void) -> Void

class FeedViewController: UIViewController{
    var loadFeed: FeedLoader!
    
    convenience init(loadFeed: @escaping FeedLoader){
        self.init()
        self.loadFeed = loadFeed
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadFeed{ item in
            
        }
    }
}

@IhwanID
Copy link
Author

IhwanID commented Aug 17, 2021

import UIKit


protocol FeedLoader{
    func loadFeed(completion: @escaping ([String]) -> Void)
}

class FeedViewController: UIViewController{
    var loadFeed: FeedLoader!
    
    convenience init(loadFeed: FeedLoader){
        self.init()
        self.loadFeed = loadFeed
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadFeed.loadFeed{ item in
            
        }
    }
}

@IhwanID
Copy link
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) {
        
    }
}

@IhwanID
Copy link
Author

IhwanID commented Aug 17, 2021

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