Skip to content

Instantly share code, notes, and snippets.

// Protocol
protocol Fruit {
func getPrice() -> String
func getCount() -> Int
}
// Implements Fruit Protocol
class Orange: Fruit {
func getPrice() -> String {
// Abstract Factory
protocol FurnitureFactory {
static func createTable() -> Table
static func createChair() -> Chair
}
// Abstract product Table
protocol Table {
func count() -> Int
// protocol for creating a product
protocol ShoeShop {
func produceShoe()
}
// class that conforms to ShoeShop protocol
class Nike: ShoeShop {
func produceShoe() {
print("Shoe Produced")
// Singleton class Vehicle
class Vehicle {
static let sharedInstance = Vehicle()
// private initialser
private init() {}
func getName() -> String {
return "Car"
}
@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
@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 / 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 / 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 / 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 / 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()