iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 5
0
自我挑戰組

Pro Design Patterns in Swift5系列 第 5

Day5: Chapter4: Object Template 模式(上)

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20200920/20130138mh0aABactk.jpg

Photo by Vidar Nordli-Mathisen on Unsplash


程式列 4-1

var products = [
    ("Kayak", "A boat for one person", 275.0, 10),
    ("Lifejacket", "Protective and fashionable", 48.95, 14),
    ("Soccer Ball", "FIFA-approved size and weight", 19.5, 32),
]

func calculateTax(product:(String, String, Double, Int)) -> Double {
    return product.2 * 0.2;
}

func calculateStockValue(tuples:[(String, String, Double, Int)]) -> Double {
    return tuples.reduce(0, {
        (total, product) -> Double in
        return total + (product.2 * Double(product.3))
    })
}

print("Sales tax for Kayak: $\(calculateTax(product: products[0]))")
print("Total value of stock: $\(calculateStockValue(tuples: products))")

程式列 4-2

var products = [
    ("Kayak", 275.0, 10),
    ("Lifejacket", 48.95, 14),
    ("Soccer Ball", 19.5, 32),
]

func calculateTax(product:(String, Double, Int)) -> Double {
    return product.1 * 0.2;
}

func calculateStockValue(tuples:[(String, Double, Int)]) -> Double {
    return tuples.reduce(0, {
        (total, product) -> Double in
        return total + (product.1 * Double(product.2))
    })
}

print("Sales tax for Kayak: $\(calculateTax(product: products[0]))")
print("Total value of stock: $\(calculateStockValue(tuples: products))")

認識緊密耦合的問題

本書優點, 藉由緊密耦合的案例, 來引導這問題。


程式列 4-4

var products = [
    Product(name: "Kayak", description: "A boat for one person", price: 275.0, stock: 10),
    Product(name: "Lifejacket", description: "Protective and fashionable", price: 48.95, stock: 14),
    Product(name: "Soccer Ball", description: "FIFA-approved size and weight", price: 19.5, stock: 32),
]

func calculateTax(product:Product) -> Double {
    return product.price * 0.2;
}

func calculateStockValue(tuples:[Product]) -> Double {
    return tuples.reduce(0, {
        (total, product) -> Double in
        return total + (product.price * Double(product.stock))
    })
}

print("Sales tax for Kayak: $\(calculateTax(product: products[0]))")
print("Total value of stock: $\(calculateStockValue(tuples: products))")

原本product.1, 已修改為product.price, 閱讀性舒暢。

使用樣板兩個步驟
1.定義樣板
2.使用樣板建構物件


封裝讓資料值與操作值的邏輯可以結合在單一元件
程式列 4-7

class Product {
    var name: String;
    var price: Double;
    var stock: Int;
    
    init(name: String, price: Double, stock: Int) {
        self.name = name;
        self.price = price;
        self.stock = stock;
    }
    
    func calculateTax(rate: Double) -> Double {
        return self.price * rate;
    }
    
    var stockValue: Double {
        get {
            return self.price * Double(self.stock);
        }
    }
}

程式列 4-8

var products = [
    Product(name: "Kayak", price: 275.0, stock: 10),
    Product(name: "Lifejacket", price: 48.95, stock: 14),
    Product(name: "Soccer Ball", price: 19.5, stock: 32),
]

func calculateStockValue(tuples:[Product]) -> Double {
    return tuples.reduce(0, {
        (total, product) -> Double in
        return total + product.stockValue;
    })
}

print("Sales tax for Kayak: $\(products[0].calculateTax(rate: 0.2))")
print("Total value of stock: $\(calculateStockValue(tuples: products))")

程式列 4-10

class Product {
    var name: String;
    var price: Double;
    private var stockBackingValue: Int = 0;
    var stock: Int {
        get {
            return stockBackingValue;
        }
        set {
            stockBackingValue = max(0, newValue);
        }
    };
    
    init(name: String, price: Double, stock: Int) {
        self.name = name;
        self.price = price;
        self.stock = stock;
    }
    
    func calculateTax(rate: Double) -> Double {
        return min(10, self.price * rate)
    }
    
    var stockValue: Double {
        get {
            return self.price * Double(self.stock);
        }
    }
}

Tip: 如過對"結構是值物件, 類別是參考物件"這句話不懂可參考這文章


咖啡店要關門了, 只好下一篇介紹Object Template模式套用SportsStore

謝謝閱讀, Thank you


上一篇
Day4: 建構 Sports Store 應用程式
下一篇
Day6: Chapter4: Object Template 模式(下)
系列文
Pro Design Patterns in Swift517
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言