Skip to content

Instantly share code, notes, and snippets.

@LH17
LH17 / Visitor.swift
Created Jan 1, 2019
Visitor Design Pattern
View Visitor.swift
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 Jan 1, 2019
Template Design Pattern
View Template.swift
protocol Office {
func officeSchedule()
}
protocol Employee {
func work()
func getPaid()
}
@LH17
LH17 / Strategy.swift
Created Jan 1, 2019
Strategy Design Pattern
View Strategy.swift
protocol Strategy {
func convert(number: Int)
}
class Convert {
var strategy: Strategy
var number: Int
init(number: Int, strategy: Strategy) {
@LH17
LH17 / State.swift
Created Jan 1, 2019
State Design Pattern
View State.swift
protocol Human {
// MARK: - Getter
func getState() -> ManState
// MARK: - Setter
func set(state: ManState)
}
protocol ManState {
func stand()
@LH17
LH17 / Observer.swift
Created Jan 1, 2019
Observer Design Pattern
View Observer.swift
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 Jan 1, 2019
Memento Design Pattern
View Memento.swift
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 Jan 1, 2019
Mediator Design Pattern
View Mediator.swift
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 Jan 1, 2019
Iterator Design Pattern
View Iterator.swift
struct MyBestFilms: Sequence {
let films: [String]
func makeIterator() -> MyBestFilmsIterator {
return MyBestFilmsIterator(films)
}
}
struct MyBestFilmsIterator: IteratorProtocol {
View CommandDesignPattern.swift
protocol Command {
func execute()
}
class ConcreteCommand: Command {
var colorReceiver: ColorReceiver
init(colorReceiver: ColorReceiver) {
self.colorReceiver = colorReceiver
@LH17
LH17 / ChainOfResponsibility.swift
Last active Jan 1, 2019
Chain Of Responsibility Design Pattern
View ChainOfResponsibility.swift
enum Level: Int {
case state = 1
case national = 2
case international = 3
}
class Sports {
var level: Level