一,概念:

  策略模式定义了一系列的算法,并将每一个算法封装起来,而且使他们可以相互替换,让算法独立于使用它的客户而独立变化。

二,使用场景

  1.针对同一类型问题的多种处理方式,仅仅是具体行为有差别时; 
  2.需要安全地封装多种同一类型的操作时; 
  3.出现同一抽象类有多个子类,而又需要使用 if-else 或者 switch-case 来选择具体子类时。

三,类图

   

  环境(Context)角色:持有一个Strategy的引用。

  抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。

  具体策略(ConcreteStrategy)角色:包装了相关的算法或行为。

四,代码实例

protocol CardInterface {
var money: Float{get set}
var discountShopping: Float{get}
var discountFood: Float{get}
var discountRecreation: Float{get} func shopping(standardCost: Float) -> Bool
func food(standardCost: Float) -> Bool
func recreation(standardCost: Float) -> Bool
} class BaseCard: CardInterface {
var money: Float
var discountShopping: Float
var discountFood: Float
var discountRecreation: Float init(money: Float, dShopping: Float, dFood: Float, dRecreation: Float) {
self.money = money
discountShopping = dShopping
discountFood = dFood
discountRecreation = dRecreation
} func shopping(standardCost: Float) -> Bool {
if money >= standardCost * discountShopping {
money -= standardCost * discountShopping
print("success: price(\(standardCost)), cost (\(standardCost * discountShopping)) in fact,left (\(money)),type shopping")
return true
}
print("Lack of balance")
return false
} func food(standardCost: Float) -> Bool {
if money >= standardCost * discountFood {
money -= standardCost * discountFood
print("success: price(\(standardCost)), cost (\(standardCost * discountFood)) in fact,left (\(money)),type food")
return true
}
print("Lack of balance")
return false
} func recreation(standardCost: Float) -> Bool {
if money >= standardCost * discountRecreation {
money -= standardCost * discountRecreation
print("success: price(\(standardCost)), cost (\(standardCost * discountRecreation)) in fact,left (\(money)),type recreation")
return true
}
print("Lack of balance")
return false
}
} class NomalCard: BaseCard {
init(money: Float) {
super.init(money: money, dShopping: 0.88, dFood: 0.9, dRecreation: 0.8)
}
} class VipCard: BaseCard {
init(money: Float) {
super.init(money: money, dShopping: 0.8, dFood: 0.8, dRecreation: 0.7)
}
} class SuperVipCard: BaseCard {
init(money: Float) {
super.init(money: money, dShopping: 0.7, dFood: 0.75, dRecreation: 0.5)
}
}
enum CardType: String {
case Nomal
case VIP
case SuperVIP
} class Customer {
var card: CardInterface?
var cardType: CardType init(cType: CardType) {
cardType = cType
addCard()
} fileprivate func addCard() {
switch cardType {
case .Nomal:
card = NomalCard(money: 100)
case .VIP:
card = VipCard(money: 100)
case .SuperVIP:
card = SuperVipCard(money: 100)
default: break }
} }
class ViewController: UIViewController {

    override func viewDidLoad() {
super.viewDidLoad()
let xiaoMing = Customer(cType: .SuperVIP)
var rel = xiaoMing.card?.recreation(standardCost: 88)
print(rel ?? false)
rel = xiaoMing.card?.recreation(standardCost: 100)
print(rel ?? false)
rel = xiaoMing.card?.recreation(standardCost: 100)
print(rel ?? false)
}
}

设计模式-(17)策略模式 (swift版)的更多相关文章

  1. 设计模式:策略模式(Strategy)

    定   义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...

  2. PHP设计模式之策略模式

    前提: 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查 找.排序等,一种常用的方法是硬编码(Hard Cod ...

  3. JavaScript设计模式之策略模式(学习笔记)

    在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...

  4. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  5. JavaScript设计模式之策略模式

    所谓"条条道路通罗马",在现实中,为达到某种目的往往不是只有一种方法.比如挣钱养家:可以做点小生意,可以打分工,甚至还可以是偷.抢.赌等等各种手段.在程序语言设计中,也会遇到这种类 ...

  6. 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查

    原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...

  7. [design-patterns]设计模式之一策略模式

    设计模式 从今天开始开启设计模式专栏,我会系统的分析和总结每一个设计模式以及应用场景.那么首先,什么是设计模式呢,作为一个软件开发人员,程序人人都会写,但是写出一款逻辑清晰,扩展性强,可维护的程序就不 ...

  8. 设计模式入门,策略模式,c++代码实现

    // test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...

  9. 设计模式之策略模式和状态模式(strategy pattern & state pattern)

    本文来讲解一下两个结构比较相似的行为设计模式:策略模式和状态模式.两者单独的理解和学习都是比较直观简单的,但是实际使用的时候却并不好实践,算是易学难用的设计模式吧.这也是把两者放在一起介绍的原因,经过 ...

  10. python设计模式之策略模式

    每次看到项目中存在大量的if else代码时,都会心生一丝不安全感. 特别是产品给的需求需要添加或者更改一种if条件时,生怕会因为自己的疏忽而使代码天崩地裂,哈哈,本文的目的就是来解决这种不安全感的, ...

随机推荐

  1. Boolean.valueOf("true")的用法

    Boolean.valueOf(a);a为true时返回true不管大小写,a为其他值时都返回false:

  2. 表连接sql执行计划学习

    循环嵌套连接(Nested Loop Join) 合并连接(Merge Join) 哈西匹配(Hash Join) 文章:浅谈SQL Server中的三种物理连接操作 循环嵌套,如果内循环列上有索引, ...

  3. HDU-2509-Be the Winner,博弈题~~水过~~

    Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ht ...

  4. python学习笔记--python数据类型

    一.整形和浮点型 整形也就是整数类型(int)的,在python3中都是int类型,没有什么long类型的,比如说存年龄.工资.成绩等等这样的数据就可以用int类型,有正整数.负整数和0,浮点型的也就 ...

  5. /etc/fstab readyonly 解决办法

    阿里云主机切换地区强制升级后,“新的磁盘盘符识别为vdb1,但是在/etc/fstab中记录的挂载信息还是旧的xvdb1,导致磁盘挂载失败” 机子启动出错了. 按提示 输入 root的密码,进入以Re ...

  6. 获取当前日期的T-SQL语句

    CONVERT(nvarchar(10),count_time,121): CONVERT为日期转换函数,一般就是在时间类型 (datetime,smalldatetime)与字符串类型(nchar, ...

  7. 【BZOJ1717&POJ3261】Milk Patterns(后缀数组,二分)

    题意:求字符串的可重叠的k次最长重复子串 n<=20000 a[i]<=1000000 思路:后缀数组+二分答案x,根据height分组,每组之间的height>=x 因为可以重叠, ...

  8. 指针,数组,字符串的区别(高质量程序设计指南C++/C语言第7章)

    指针: 指针是变量,和平时的那些变量没有本质的差异,不同的只是它的值和类型,.,即解释方式 二进制层面:指针的值是内存单元的地址,而变量是引用内存单元值的别名 语言层面:指针的值就是变量的地址. 对象 ...

  9. 使用imageMagick 制作圆角矩形和图片加水印

    制作圆角矩形好图片水印都是图片合成的操作 composite -gravity southeast mask175.png  src.jpg  dest.jpg -gravity southeast ...

  10. ATcoder 1983 BBQ Hard

    E - BBQ Hard Time limit : 2sec / Memory limit : 256MB Score : 1400 points Problem Statement Snuke is ...