初学者的通病
class ViewController: UIViewController {
//这里没有写输入框 就简单写一下了
var A:Int = 5
var B:String = "+"
var C:Int = 10
var D:Int?
override func viewDidLoad() {
super.viewDidLoad()
if B == "+" {
D = A + C
}
if B == "-" {
D = A - C
}
if B == "*" {
D = A * C
}
if B == "/" {
D = A / C
}
print("结果是:",D!)
// 结果是:15
}
}
稍微规范一下代码
class ViewController: UIViewController {
var numberA:Int = 5
var strOperate:String = "+"
var numberB:Int = 10
var numberResult:Int?
override func viewDidLoad() {
super.viewDidLoad()
switch strOperate {
case "+":
numberResult = numberA + numberB
case "-":
numberResult = numberA - numberB
case "*":
numberResult = numberA * numberB
case "/":
if numberB != 0 {
numberResult = numberA / numberB
} else {
numberResult = 0
}
default:
break
}
print("结果是:", numberResult!)
// 结果是:15
}
}
对业务逻辑进行封装
计算逻辑
class Operation: NSObject {
class func getResult(numberA:Int, numberB:Int, operate:String) -> Int {
var result = 0
switch operate {
case "+":
result = numberA + numberB
case "-":
result = numberA - numberB
case "*":
result = numberA * numberB
case "/":
if numberB != 0 {
result = numberA / numberB
} else {
result = 0
}
default:
break
}
return result
}
}
客户端逻辑
class ViewController: UIViewController {
var numberA:Int = 5
var strOperate:String = "+"
var numberB:Int = 10
var numberResult:Int?
override func viewDidLoad() {
super.viewDidLoad()
numberResult = Operation.getResult(numberA, numberB: numberB, operate: strOperate)
print("结果是:", numberResult!)
// 结果是:15
}
}
最后
解耦及创建工厂类
运算逻辑父类
class Operation: NSObject {
private var _numberA:Double = 0
private var _numberB:Double = 0
var numberA:Double {
set{
_numberA = newValue
}
get{
return _numberA
}
}
var numberB:Double {
set{
_numberB = newValue
}
get{
return _numberB
}
}
func getResult() -> Double {
var result:Double = 0
return result
}
}
运算符子类逻辑(继承运算逻辑父类)
class OperationAdd: Operation {
// 加法运算类
override func getResult()->Double{
var result:Double = 0
result = numberA + numberB
return result
}
}
class OperationSub: Operation {
// 减法运算类
override func getResult() -> Double {
var result:Double = 0
result = numberA - numberB
return result
}
}
class OperationMul: Operation {
// 乘法运算类
override func getResult() -> Double {
var result:Double = 0
result = numberA * numberB
return result
}
}
class OperationDiv: Operation {
// 除法运算类
override func getResult() -> Double {
var result:Double = 0
if numberB != 0 {
result = numberA / numberB
} else {
result = 0
}
return result
}
}
工厂类
class OperationFactory {
static func createOperate(operate:String)->Operation {
var oper:Operation?
switch operate {
case "+":
oper = OperationAdd()
case "-":
oper = OperationSub()
case "*":
oper = OperationMul()
case "/":
oper = OperationDiv()
default:
break
}
return oper!
}
}
客户端代码
class ViewController: UIViewController {
var oper:Operation?
override func viewDidLoad() {
super.viewDidLoad()
oper = OperationFactory.createOperate("+")
oper?.numberA = 20
oper?.numberB = 10
let result:Double = oper!.getResult()
print("结果是:", result)
// 结果是:30
}
}
总结
大家可以看到, 通过四种形态的代码展示了同样的操作,从初学者写出的"简单"代码到将代码规范化,到面向对象思想,最后到考虑到耦合度及创建工厂模式;如果你已经理解第四种形态,那么无论是在某一天我们需要修改加法运算,或者继续添加复杂运算逻辑,再或者修改用户界面, 只需要去相应的类中做出对应的修改即可, 当然你要非得说第一种又简单代码又少这话也没毛病,不过咱学的是设计模式你说是不?
我在写出这篇帖子的时候 只有第三步的水平,不知道大家学习设计模式的时候都在什么水平?目前本人水平有限,此贴仅供自己练手和给觉得自己还有不足的同学参考
@旺仔牛奶
2016年3月28日