设计模式一书中引用了商场促销的例子来讲解策略模式.现在我们就来看看代码
1.0版本
class ViewController: UIViewController {
var total:Double = 0.0 // 总计
var price:Double = 50 // 单价
var num:Double = 3 // 数量
override func viewDidLoad() {
super.viewDidLoad()
let totalPrices = price * num
total = total + totalPrices
print("总价为:", total)
// 总价为:150
}
}
加入打折操作
class ViewController: UIViewController {
var total:Double = 0.0 // 总计
var price:Double = 50 // 单价
var num:Double = 3 // 数量
var payMod = "正常收费" // 收费模式
override func viewDidLoad() {
super.viewDidLoad()
var totalPrices:Double = 0
switch payMod {
case "正常收费":
totalPrices = price * num
case "8折":
totalPrices = price * num * 0.8
case "75折":
totalPrices = price * num * 0.75
case "5折":
totalPrices = price * num * 0.5
default:
break
}
total = total + totalPrices
print("单价:",price,"个数:",num, "收费模式:",payMod,"合计:", total)
// 单价:50 个数:3 收费模式:正常收费 总价为:150
}
}
再来看看使用简单工厂模式的写法
class CashSuper: NSObject {
//现金父类
func acceptCash(money:Double)->Double{
return money
}
}
class CashNormal: CashSuper {
// 正常收费子类
override func acceptCash(money: Double) -> Double {
return money
}
}
class CashRebate: CashSuper {
// 打折收费子类
private var moneyRebate:Double = 1.0
// 初始化方法
init(moneyRebate:Double) {
self.moneyRebate = moneyRebate
}
override func acceptCash(money: Double) -> Double {
return money * moneyRebate
}
}
class CashReturn: CashSuper {
// 返利收费子类
private var moneyCondition:Double = 0.0
private var moneyReturn:Double = 0.0
// 初始化方法
init(moneyCondition:Double, moneyReturn:Double) {
self.moneyCondition = moneyCondition
self.moneyReturn = moneyReturn
}
override func acceptCash(money: Double) -> Double {
var result = money
if money >= moneyCondition {
result = money - floor(money / moneyCondition) * moneyReturn
}
return result
}
class CashFactory {
//工厂类
static func createCashAccept(type:String)->CashSuper {
var cs: CashSuper?
switch type {
case "正常收费":
cs = CashNormal()
case "满300返100":
cs = CashReturn(moneyCondition: 300, moneyReturn: 100)
case "打8折":
cs = CashRebate(moneyRebate: 0.8)
default:
break
}
return cs!
}
}
class ViewController: UIViewController {
// 客户端代码
var total:Double = 0.0 // 总计
var price:Double = 50 // 单价
var num:Double = 3 // 数量
var payMod = "正常收费"
override func viewDidLoad() {
super.viewDidLoad()
let csuper = CashFactory.createCashAccept(payMod)
var totalPrices:Double = 0
// 传入商品总价
totalPrices = csuper.acceptCash(price * num)
total = total + totalPrices
print("单价:",price,"个数:",num, "收费模式:",payMod,"合计:", total)
// 单价:50 个数:3 收费模式:正常收费 总价为:150
}
}
如果我们使用策略模式写的话会是什么样子?
策略模式只需要在增加一个context类
class CashContext: NSObject {
private var cs:CashSuper
init(csuper:CashSuper) {
self.cs = csuper
}
func getResult(money:Double)->Double{
return cs.acceptCash(money)
}
}
客户端代码
class ViewController: UIViewController {
var total:Double = 0.0 // 总计
var price:Double = 50 // 单价
var num:Double = 3 // 数量
var payMod = "正常收费"
override func viewDidLoad() {
super.viewDidLoad()
var cc: CashContext?
switch payMod {
case "正常收费":
cc = CashContext(csuper: CashNormal())
case "满300返100":
cc = CashContext(csuper: CashReturn(moneyCondition: 300, moneyReturn: 100))
case "打8折":
cc = CashContext(csuper: CashRebate(moneyRebate: 0.8))
default:
break
}
var totalPrices:Double = 0
totalPrices = cc!.getResult(price * num)
total = total + totalPrices
print("单价:",price,"个数:",num, "收费模式:",payMod,"合计:", total)
// 单价:50 个数:3 收费模式:正常收费 总价为:150
}
}
使用策略模式的话,进行判断的语句又回到了客户端代码中,这肯定不是我们想要的结果那么将策略模式和简单工厂模式结合一下
再来看看策略模式和简单工厂模式的结合版
需要修改cashContext类
class CashContext: NSObject {
private var cs:CashSuper?
init(type:String) {
switch type {
case "正常收费":
cs = CashNormal()
case "满300返100":
cs = CashReturn(moneyCondition: 300, moneyReturn: 100)
case "打8折":
cs = CashRebate(moneyRebate: 0.8)
default:
break
}
}
func getResult(money:Double)->Double{
return cs!.acceptCash(money)
}
}
客户端代码修改
class ViewController: UIViewController {
var total:Double = 0.0 // 总计
var price:Double = 50 // 单价
var num:Double = 3 // 数量
var payMod = "正常收费"
override func viewDidLoad() {
super.viewDidLoad()
var csuper: CashContext?
csuper = CashContext(type: payMod)
var totalPrices:Double = 0
totalPrices = csuper!.getResult(price * num)
total = total + totalPrices
print("单价:",price,"个数:",num, "收费模式:",payMod,"合计:", total)
// 单价:50 个数:3 收费模式:正常收费 总价为:150
}
}
策略模式就是封装算法的,但在实践中,我们发现可以用它来封装几乎任何类型的规则, 只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性
但是在基本的策略模式中, 选择所用的具体实现职责还是由客户端对象在承担,并转给策略模式的Context对象.这本身并没有接触客户端需要选择判断的压力,而策略模式与简单工厂模式节后和,选择具体时间的职责也可以由Context来承担,这就最大化减轻了客户端的职责.
但是书中还提到这不是最优的解决办法.让我们继续之后的学习吧~
@旺仔牛奶
2016年3月29日
网友评论