Swift 03 建造者模式 Builder Pattern

/*
把统一复杂的工序、细分成不同的简单工序
能够轻易增加、修改工序
*/

// 药品 复杂产品
class MedicineProduct {
    var p_stName : String = ""
    var p_mNumber : Int = 0
    var p_stPacking : String = ""
    var p_stExpiryDate : String = ""
    var p_stLocation : String = ""
}

// 生产员 抽象建造者
class Builder {
	func buildName() { }
	func getProduct() -> MedicineProduct { 
	    return MedicineProduct()
	}
}

// 阿司匹林 具体建造者
class AspirinProduct : Builder {
	override func buildName() {
		medicineProduct.p_stName = "Aspirin"
		medicineProduct.p_mNumber = 100
		medicineProduct.p_stPacking = "box"
        medicineProduct.p_stExpiryDate = "1 year"
        medicineProduct.p_stLocation = "ZhuHai"
	}
	override func getProduct() -> MedicineProduct {
	    return medicineProduct
	}
	    
    var medicineProduct = MedicineProduct()
}

// 阿司匹林 具体建造者
class ParacetamolProduct : Builder {
	override func buildName() {
		medicineProduct.p_stName = "Paracetamol"
		medicineProduct.p_mNumber = 80
		medicineProduct.p_stPacking = "package"
        medicineProduct.p_stExpiryDate = "2 years"
        medicineProduct.p_stLocation = "ShanZhun"
	}
	override func getProduct() -> MedicineProduct {
	    return medicineProduct
	}
	    
    var medicineProduct = MedicineProduct()
}

// 装货员 指挥者
class Managaer {
    var builder = Builder()
    func Construct(c_concreteBuilder : Builder) -> MedicineProduct {
        builder = c_concreteBuilder
        builder.buildName()
        return builder.getProduct()
    }
}

// 客戶端
let managaer = Managaer()
// 客戶只需要更改药物的建造者、即工厂类、就能完成不同工厂的复杂包装程序
let product = managaer.Construct(c_concreteBuilder: AspirinProduct())
print("Name: \(product.p_stName) Number: \(product.p_mNumber) Packing: \(product.p_stPacking) ExpiryDate: \(product.p_stExpiryDate) Location: \(product.p_stLocation)")

let product2 = managaer.Construct(c_concreteBuilder: ParacetamolProduct())
print("Name: \(product2.p_stName) Number: \(product2.p_mNumber) Packing: \(product2.p_stPacking) ExpiryDate: \(product2.p_stExpiryDate) Location: \(product2.p_stLocation)")

你可能感兴趣的:(设计模式,Design,pattern,design,pattern,swift,设计模式,建造者模式,Builder,Pattern)