Skip to content

Commit

Permalink
Support Swift 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcos committed Oct 18, 2018
1 parent 9ca17bc commit 0c897e3
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 58 deletions.
30 changes: 15 additions & 15 deletions 01-Simple_Factory_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

// 协议
protocol Operator {
var num: (Double, Double) { get set }
var nums: (Double, Double) { get set }

func getResult() -> Double?
}

// 遵守协议
struct Addition: Operator {
var num = (0.0, 0.0)
var nums = (0.0, 0.0)

func getResult() -> Double? {
return num.0 + num.1
return nums.0 + nums.1
}
}

class Subtraction: Operator {
var num = (0.0, 0.0)
var nums = (0.0, 0.0)

func getResult() -> Double? {
return num.0 - num.1
return nums.0 - nums.1
}
}

struct Multiplication: Operator {
var num = (0.0, 0.0)
var nums = (0.0, 0.0)

func getResult() -> Double? {
return num.0 * num.1
return nums.0 * nums.1
}
}

struct Division: Operator {
var num = (0.0, 0.0)
var nums = (0.0, 0.0)

func getResult() -> Double? {
var result: Double?
if num.1 != 0 {
result = num.0 / num.1
guard nums.1 != 0 else {
return nil
}
return result

return nums.0 / nums.1
}
}

Expand All @@ -69,9 +69,9 @@ struct OperatorFactory {
}

var testDivision = OperatorFactory.calculateForOperator(.division)
testDivision.num = (1, 0)
testDivision.nums = (1, 0)
print(testDivision.getResult() ?? "Error")

var testAddition = OperatorFactory.calculateForOperator(.addition)
testAddition.num = (1, 1)
testAddition.nums = (1, 1)
print(testAddition.getResult() ?? "Error")
29 changes: 14 additions & 15 deletions 02-Factory_Method_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

// 协议
protocol Operator {
var num: (Double, Double) { get set }
var nums: (Double, Double) { get set }

func getResult() -> Double?
// 工厂
Expand All @@ -18,10 +18,10 @@ struct Addition: Operator {
return Addition()
}

var num = (0.0, 0.0)
var nums = (0.0, 0.0)

func getResult() -> Double? {
return num.0 + num.1
return nums.0 + nums.1
}
}

Expand All @@ -30,10 +30,10 @@ struct Subtraction: Operator {
return Subtraction()
}

var num = (0.0, 0.0)
var nums = (0.0, 0.0)

func getResult() -> Double? {
return num.0 - num.1
return nums.0 - nums.1
}
}

Expand All @@ -42,10 +42,10 @@ struct Multiplication: Operator {
return Multiplication()
}

var num = (0.0, 0.0)
var nums = (0.0, 0.0)

func getResult() -> Double? {
return num.0 * num.1
return nums.0 * nums.1
}
}

Expand All @@ -54,21 +54,20 @@ struct Division: Operator {
return Division()
}

var num = (0.0, 0.0)
var nums = (0.0, 0.0)

func getResult() -> Double? {
var result: Double?
if num.1 != 0 {
result = num.0 / num.1
guard nums.1 != 0 else {
return nil
}
return result
return nums.0 / nums.1
}
}

var testAddition = Addition.createOperation()
testAddition.num = (2, 3)
testAddition.nums = (2, 3)
print(testAddition.getResult() ?? "Error")

var testDivision = Division.createOperation()
testDivision.num = (2, 0)
testDivision.nums = (2, 0)
print(testDivision.getResult() ?? "Error")
2 changes: 1 addition & 1 deletion 03-Abstract_Factory_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 04-Strategy_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 05-Decorator_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 06-Proxy_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
4 changes: 2 additions & 2 deletions 07-Prototype_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down Expand Up @@ -73,7 +73,7 @@ resumeB.setWorkExperience("2016.9-2016.12", "School")
let resumeC = resumeA.clone()
resumeC.setWorkExperience("2014-2018", "University")

//深拷贝
// 深拷贝
resumeA.display()
resumeB.display()
resumeC.display()
2 changes: 1 addition & 1 deletion 08-Template_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 09-Facade_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 10-Builder_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 11-Observer_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 12-Delegate_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
3 changes: 1 addition & 2 deletions 13-State_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down Expand Up @@ -95,4 +95,3 @@ for i in stride(from: 9, through: 22, by: 1) {
work.hour = i
work.writeProgram()
}

2 changes: 1 addition & 1 deletion 15-Memento_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 16-Composite_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 17-Iterator_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 18-Singleton_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 19-Bridge_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 20-Command_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 22-Mediator_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
2 changes: 1 addition & 1 deletion 23-Flyweight_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down
10 changes: 5 additions & 5 deletions 24-Interpreter_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand All @@ -11,15 +11,15 @@ struct PlayContext {
// 表达式
class Expression {
func interpret(_ context: inout PlayContext) {
if context.text.characters.count != 0 {
if context.text.count != 0 {
let rangeA = Range(uncheckedBounds: (context.text.index(after: context.text.startIndex), context.text.index(context.text.startIndex, offsetBy: 2)))

let playKey = context.text.substring(with: rangeA)
let playKey = String(context.text[rangeA])
let index = context.text.index(context.text.startIndex, offsetBy: 3)
context.text = context.text.substring(from: index)
context.text = String(context.text[index...])

let rangeB = Range(uncheckedBounds: (context.text.characters.startIndex, context.text.characters.index(of: " ")!))
let playValue = context.text.substring(with: rangeB)
let playValue = context.text[rangeB]

context.text = context.text.substring(from: context.text.characters.index(of: " ")!)
execute(playKey, value: Double(playValue)!)
Expand Down
2 changes: 1 addition & 1 deletion 25-Visitor_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns

import UIKit

Expand Down

0 comments on commit 0c897e3

Please sign in to comment.