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
// Singleton class Vehicle | |
class Vehicle { | |
static let sharedInstance = Vehicle() | |
// private initialser | |
private init() {} | |
func getName() -> String { | |
return "Car" | |
} |
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
// protocol for creating a product | |
protocol ShoeShop { | |
func produceShoe() | |
} | |
// class that conforms to ShoeShop protocol | |
class Nike: ShoeShop { | |
func produceShoe() { | |
print("Shoe Produced") |
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
// Abstract Factory | |
protocol FurnitureFactory { | |
static func createTable() -> Table | |
static func createChair() -> Chair | |
} | |
// Abstract product Table | |
protocol Table { | |
func count() -> Int |
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
// Protocol | |
protocol Fruit { | |
func getPrice() -> String | |
func getCount() -> Int | |
} | |
// Implements Fruit Protocol | |
class Orange: Fruit { | |
func getPrice() -> String { |
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
// Protocol | |
protocol Fruit { | |
func set(price: String?) | |
func clone() -> Fruit | |
} | |
// Class Apple which implements the protocol | |
class Apple: Fruit { | |
var count: Int |
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
precedencegroup UnionOperatorPrecedence { | |
associativity: left | |
higherThan: IntersectionOperatorPrecedence | |
} |
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
precedencegroup IntersectionOperatorPrecedence { | |
associativity: left | |
} | |
infix operator ∩: IntersectionOperatorPrecedence | |
func ∩<T: Equatable> (left: [T], right: [T]) -> [T] { | |
var intersection: [T] = [] | |
for value in left { | |
if right.contains(value) { |
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
precedencegroup UnionOperatorPrecedence { | |
associativity: left | |
} | |
infix operator ∪: UnionOperatorPrecedence | |
func ∪ <T:Equatable>(lhs: [T], rhs:[T])->[T] { | |
var result = lhs | |
for element in rhs { | |
if !lhs.contains(element) { |
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
class StateViewController: UIViewController {} | |
extension StateViewController: ViewStateProtocol { | |
@objc func handleTap(_ sender: UIView) { | |
// for showing the loader | |
addView(withState: .loading) | |
// for showing the error message | |
addView(withState: .error) |
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
class StateManager { | |
static let sharedInstance = StateManager() | |
var viewStore: [String: UIView] = [:] | |
// Associates a view for the given state | |
public func addView(_ view: UIView, forState state: String, superview: UIView) { | |
viewStore[state] = view | |
superview.addSubview(view) | |
} |