Skip to content

Instantly share code, notes, and snippets.

@PaulTaykalo
Created October 4, 2016 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PaulTaykalo/5b72a197be0e9c63236677a3f4a20e9b to your computer and use it in GitHub Desktop.
Save PaulTaykalo/5b72a197be0e9c63236677a3f4a20e9b to your computer and use it in GitHub Desktop.
Function Injection example
//: Playground - noun: a place where people can play
import Foundation
import UIKit
class ImageProcessor {
let postProcessOp:(UIImage) -> ()
init(postProcessOp:@escaping (UIImage) -> () = {_ in }) {
self.postProcessOp = postProcessOp
}
func processImage(original: UIImage) -> UIImage {
let image = original.imageFlippedForRightToLeftLayoutDirection()
postProcessOp(image)
return image
}
}
class Factory {
func validImageProcessor() -> ImageProcessor {
return ImageProcessor(postProcessOp: Cacher.saveCachedImage)
}
}
class Cacher {
static func saveCachedImage(image:UIImage) {
print("Feeling sick today - Won't cache anything")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment