iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 15
0
自我挑戰組

Pro Design Patterns in Swift5系列 第 15

Day: 15 Chapter 11 Builder模式

  • 分享至 

  • xImage
  •  

自從Day9, GCD(Grand Central Dispatch)卡關後, 前幾天沒用心寫, 我對不起廣大的粉絲
在此利用中秋連假展開時間鉗形攻勢, Hold住文章品質!!

enum Burgers {
    case STANDARD; case BIGBURGER; case SUPERVEGGIE;
}

class BurgerBuilder {
    private var veggie = false;
    private var pickles = false;
    private var mayo = true;
    private var ketchup = true;
    private var lettuce = true;
    private var cooked = Burger.Cooked.NORMAL;
    private var patties = 2;
    private var bacon = true;
    
    private init() {
        // do nothing
    }
    
    func setVeggie(choice: Bool) {
        self.veggie = choice;
        if (choice) {
            self.bacon = false;
        }
    }
    
    func setPickles(choice: Bool) { self.pickles = choice; }
    func setMayo(choice: Bool) { self.mayo = choice; }
    func setKetchup(choice: Bool) { self.ketchup = choice; }
    func setLettuce(choice: Bool) { self.lettuce = choice; }
    func setCooked(choice: Burger.Cooked) { self.cooked = choice; }
    func addPatty(choice: Bool) { self.patties = choice ? 3 : 2; }
    func setBacon(choice: Bool) { self.bacon = choice; }
    
    func buildObject(name: String) -> Burger {
        return Burger(name: name, veggie: veggie, patties: patties,
            pickles: pickles, mayo: mayo, ketchup: ketchup,
            lettuce: lettuce, cook: cooked, bacon: bacon);
    }
    
    class func getBuilder(burgerType:Burgers) -> BurgerBuilder {
        var builder:BurgerBuilder;
        switch (burgerType) {
        case .BIGBURGER: builder = BigBurgerBuilder();
        case .SUPERVEGGIE: builder = SuperVeggieBurgerBuilder();
        case .STANDARD: builder = BurgerBuilder();
        }
        return builder;
    }
}

class BigBurgerBuilder : BurgerBuilder {
    
    private override init() {
        super.init();
        self.patties = 4;
        self.bacon = false;
    }
    
    override func addPatty(choice: Bool) {
        fatalError("Cannot add patty to Big Burger");
    }
}

class SuperVeggieBurgerBuilder : BurgerBuilder {
    
    private override init() {
        super.init();
        self.veggie = true;
        self.bacon = false;
    }
    
    override func setVeggie(choice: Bool) {
        // do nothing - always veggie
    }
    
    override func setBacon(choice: Bool) {
        fatalError("Cannot add bacon to this burger");
    }
}

上一篇
Day: 14 Chapter 10 TODO
下一篇
Day16:
系列文
Pro Design Patterns in Swift517
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言