一,概念:

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

二,使用场景

  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. 可以从CSS框架中借鉴到什么

    http://isux.tencent.com/css-framework.html http://isux.tencent.com/css-framework.html 现在很多人会使用 CSS 框 ...

  2. python004 Python3 解释器

    Python3 解释器Linux/Unix的系统上,一般默认的 python 版本为 2.x,我们可以将 python3.x 安装在 /usr/local/python3 目录中.安装完成后,我们可以 ...

  3. [HNOI2012] 永无乡 题解

    题意: n个点,有加边操作,询问与某一点处于相同的联通块的点中权值第k大的点 思路: 对所有点建立一棵权值线段树,加边就配合并查集进行线段树合并 反思: 动态开点,权值线段树要用sum[g[x=fin ...

  4. [luoguP1198][JSOI2008] 最大数(线段树 || 单调栈)

    题目传送门 1.线段树 线段树可以搞. 不过慢的要死1300+ms #include <cstdio> #include <iostream> using namespace ...

  5. Android: java.lang.ClassCastException: android.widget.imageView cannot be cast to android.widget.textView异常解决

    有时在修改xml文件时,全报这种错误,这个应该是缓存没得到及时更新导致的,可以通过以下方法解决: Eclipse tends to mess up your resources every now a ...

  6. 兴奋剂检查(vijos 1426)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  7. [JSP]自定义EL函数以及使用

    有时候在JSP页面需要进行一连串的字符串的处理,需要进行自定义EL函数. 先看EL函数的tld文件: standard.jar下面: 自定义EL函数: 1.编写EL函数(全是public static ...

  8. 【Tomcat】如何优化tomcat配置(从内存、并发、缓存4个方面)优化

    一.Tomcat内存优化 ** Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 java_OPTS 参数. JAVA_O ...

  9. POJ 2391 floyd二分+拆点+最大流

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20904   Accepted: 4 ...

  10. [Bzoj2286][Sdoi2011]消耗战(虚树模板题附讲解)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4896  Solved: 1824[Submit][Statu ...