-
-
Save soxjke/1c0706a031a24f2fd19470f97500aab1 to your computer and use it in GitHub Desktop.
Function Injection example
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
| //: Playground - noun: a place where people can play | |
| import Foundation | |
| import UIKit | |
| typealias ImagePostProcessingOp = (UIImage) -> Void | |
| class ImageProcessor { | |
| let postProcessOp : ImagePostProcessingOp | |
| init(postProcessOp : ImagePostProcessingOp = {_ in }) { | |
| self.postProcessOp = postProcessOp | |
| } | |
| func processImage(original : UIImage) -> UIImage { | |
| let image = original.imageFlippedForRightToLeftLayoutDirection() | |
| postProcessOp(image) | |
| return image | |
| } | |
| } | |
| class Cacher { | |
| let path : String | |
| init(path : String) { | |
| self.path = path | |
| } | |
| func saveCachedImage(image : UIImage) { | |
| print("Saving image to \(path)") | |
| } | |
| } | |
| class Factory { | |
| func validImageProcessor() -> ImageProcessor { | |
| return ImageProcessor(postProcessOp: defaultCacher().saveCachedImage) | |
| } | |
| func defaultCacher() -> Cacher { | |
| return Cacher(path: NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first!) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment