设计模式-(17)策略模式 (swift版)
一,概念:
策略模式定义了一系列的算法,并将每一个算法封装起来,而且使他们可以相互替换,让算法独立于使用它的客户而独立变化。
二,使用场景
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版)的更多相关文章
- 设计模式:策略模式(Strategy)
定 义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...
- PHP设计模式之策略模式
前提: 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查 找.排序等,一种常用的方法是硬编码(Hard Cod ...
- JavaScript设计模式之策略模式(学习笔记)
在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...
- 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)
原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...
- JavaScript设计模式之策略模式
所谓"条条道路通罗马",在现实中,为达到某种目的往往不是只有一种方法.比如挣钱养家:可以做点小生意,可以打分工,甚至还可以是偷.抢.赌等等各种手段.在程序语言设计中,也会遇到这种类 ...
- 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查
原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...
- [design-patterns]设计模式之一策略模式
设计模式 从今天开始开启设计模式专栏,我会系统的分析和总结每一个设计模式以及应用场景.那么首先,什么是设计模式呢,作为一个软件开发人员,程序人人都会写,但是写出一款逻辑清晰,扩展性强,可维护的程序就不 ...
- 设计模式入门,策略模式,c++代码实现
// test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...
- 设计模式之策略模式和状态模式(strategy pattern & state pattern)
本文来讲解一下两个结构比较相似的行为设计模式:策略模式和状态模式.两者单独的理解和学习都是比较直观简单的,但是实际使用的时候却并不好实践,算是易学难用的设计模式吧.这也是把两者放在一起介绍的原因,经过 ...
- python设计模式之策略模式
每次看到项目中存在大量的if else代码时,都会心生一丝不安全感. 特别是产品给的需求需要添加或者更改一种if条件时,生怕会因为自己的疏忽而使代码天崩地裂,哈哈,本文的目的就是来解决这种不安全感的, ...
随机推荐
- 洛谷 P1434 [SHOI2002]滑雪
这道题适合记忆化练手 毕竟总有些大佬虐题. 这个题有几个剪枝 1.记忆化 这个不用多说了吧 剪枝就是 如果 当前点到下面一个点的目前下降的高度+1 小于 下面那个点 能下降的高度 那么反过来,这个点不 ...
- Java学习之跳转语句
Java语言中提供3中跳转语句,分别是break语句.continue语句和return语句. break语句 可以用在switch语句中.在switch语句中,break语句用于中止下面的case语 ...
- NYOJ 203 三国志
三国志 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下, ...
- Leetcode 307.区域检索-数组可修改
区域检索-数组可修改 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. update(i, val) 函数可以通过将下标为 i 的 ...
- C 题 KMP中next[]问题
题目大意: 找到能够进行字符串匹配的前缀 这题只要一直求next,直到next为0停止,记得答案是总长减去next的长度 #include <iostream> #include < ...
- [USACO12MAR]拖拉机
题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...
- linux下程序JDBC连接不到mysql数据库
今天在linux下部署一个 JavaEE项目的时候总是连接不到Mysql数据库,检查之后发现连接池的配置确定是对的,进入linux服务器之后以mysql -uname -ppassword连接总是报A ...
- nginx学习网站收录
1.菜鸟教程 2. Nginx中文 3. Nginx官网 参考:http://www.cnblogs.com/knowledgesea/p/5175711.html
- Python基础教程笔记——第4章:字典
字典 字典是Python唯一内建的数学映射类型,字典中的值没有特殊的顺序,键可以是数字,字符串,甚至是元组 字典的创建: 字典由键值对构成,字典中键是唯一的,而值不唯一.>>> a_ ...
- mongodb 报错问题
系统不支持:Mongo 错误位置 FILE: C:\wamp64\www\frame\a_tp32\ThinkPHP\Library\Think\Db\Driver\Mongo.class.php L ...