Skip to content

Instantly share code, notes, and snippets.

@LH17
LH17 / Visitor.swift
Created January 1, 2019 15:28
Visitor Design Pattern
protocol Country {
func accept(visitor: CountryVisitor)
}
protocol CountryVisitor {
func visit(country: India)
func visit(country: Brazil)
func visit(country: China)
}
@LH17
LH17 / Template.swift
Created January 1, 2019 15:26
Template Design Pattern
protocol Office {
func officeSchedule()
}
protocol Employee {
func work()
func getPaid()
}
@LH17
LH17 / Strategy.swift
Created January 1, 2019 15:22
Strategy Design Pattern
protocol Strategy {
func convert(number: Int)
}
class Convert {
var strategy: Strategy
var number: Int
init(number: Int, strategy: Strategy) {
@LH17
LH17 / State.swift
Created January 1, 2019 15:16
State Design Pattern
protocol Human {
// MARK: - Getter
func getState() -> ManState
// MARK: - Setter
func set(state: ManState)
}
protocol ManState {
func stand()
@LH17
LH17 / Observer.swift
Created January 1, 2019 15:13
Observer Design Pattern
protocol Observable {
func add(customer: Observer)
func remove(customer : Observer)
func notify()
}
protocol Observer {
var id: Int { get set }
func update()
@LH17
LH17 / Memento.swift
Created January 1, 2019 15:07
Memento Design Pattern
import UIKit
typealias MementoType = [String: Any]
protocol Memento: class {
var key: String { get set }
var state: MementoType { get set }
func save()
@LH17
LH17 / Mediator.swift
Created January 1, 2019 15:03
Mediator Design Pattern
protocol Receiver {
var name: String { get }
func receive(message: String)
}
protocol Sender {
var teams: [Receiver] { get set }
func send(message: String, sender: Receiver)
}
@LH17
LH17 / Iterator.swift
Created January 1, 2019 14:57
Iterator Design Pattern
struct MyBestFilms: Sequence {
let films: [String]
func makeIterator() -> MyBestFilmsIterator {
return MyBestFilmsIterator(films)
}
}
struct MyBestFilmsIterator: IteratorProtocol {
@LH17
LH17 / CommandDesignPattern.swift
Created January 1, 2019 14:43
Command Design Pattern
protocol Command {
func execute()
}
class ConcreteCommand: Command {
var colorReceiver: ColorReceiver
init(colorReceiver: ColorReceiver) {
self.colorReceiver = colorReceiver
@LH17
LH17 / ChainOfResponsibility.swift
Last active January 1, 2019 14:35
Chain Of Responsibility Design Pattern
enum Level: Int {
case state = 1
case national = 2
case international = 3
}
class Sports {
var level: Level