Skip to content

Instantly share code, notes, and snippets.

@LH17
LH17 / Visitor.swift
Created January 1, 2019 15:28
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 January 1, 2019 15:26
Template Design Pattern
View Template.swift
protocol Office {
func officeSchedule()
}
protocol Employee {
func work()
func getPaid()
}
@LH17
LH17 / Strategy.swift
Created January 1, 2019 15:22
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 January 1, 2019 15:16
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 January 1, 2019 15:13
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 January 1, 2019 15:07
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 January 1, 2019 15:03
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 January 1, 2019 14:57
Iterator Design Pattern
View Iterator.swift
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
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 January 1, 2019 14:35
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