Last active
January 9, 2020 16:53
-
-
Save DanielCardonaRojas/90d7dbe357db79e70049b3eea76e44a1 to your computer and use it in GitHub Desktop.
Builder pattern
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 | |
public typealias Adapter<T> = (inout T) -> Void | |
public class BuilderHandler<V> { | |
let adapter: Adapter<V> | |
public init(_ handler: @escaping Adapter<V>) { | |
self.adapter = handler | |
} | |
} | |
public protocol Builder { | |
typealias Block = Adapter<Self> | |
typealias AdapterBlock = BuilderHandler<Self> | |
} | |
extension NSObject: Builder {} | |
extension Builder { | |
public func with(_ configure: Block) -> Self { | |
var this = self | |
configure(&this) | |
return this | |
} | |
public func with(_ configure: AdapterBlock) -> Self { | |
var this = self | |
configure.adapter(&this) | |
return this | |
} | |
public func combining(_ handlers: AdapterBlock...) -> Self { | |
var this = self | |
for h in handlers { | |
h.adapter(&this) | |
} | |
return this | |
} | |
} | |
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
protocol Builder { | |
typealias Handler = (inout Self) -> Void | |
} | |
//extension NSObject: Builder {} | |
extension NSObject: Builder {} | |
extension Builder { | |
public func with(_ configure: Handler) -> Self { | |
var this = self | |
configure(&this) | |
return this | |
} | |
} | |
extension Builder where Self: UIView { | |
func usingAutoLayout() -> Self { | |
self.translatesAutoresizingMaskIntoConstraints = false | |
return self | |
} | |
func userInteractionDisabled() -> Self { | |
self.isUserInteractionEnabled = false | |
return self | |
} | |
func userInteractionEnabled() -> Self { | |
self.isUserInteractionEnabled = true | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment