设计模式-(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条件时,生怕会因为自己的疏忽而使代码天崩地裂,哈哈,本文的目的就是来解决这种不安全感的, ...
随机推荐
- CS academy Growing Trees【模板】DP求树的直径
[题意概述] 给出一棵树,树上的边有两个值a和b,你可以在[0,limit]范围内选择一个整数delta,树上的边的权值为a+b*delta,现在问当delta为多少的时候树的直径最小.最小直径是多少 ...
- 集合框架学习之List接口
Java语言的java.util包中提供了一些集合类,这些集合类又被称为容器.用来完善数组的不足之处.集合类与数组的不同之处是,数组的长度是固定的,集合的长度是可变的:数组用来存放基本类型的数据,集合 ...
- 有上下界的网络流 loj115 loj116 loj 117
参考文章 无源汇有上下界的可行流 有源汇有上下界的最大流 有源汇有上下界的最小流 无源汇有上下界可行流 以 loj115 为例. 剥离出必要边与自由边. #include <iostream&g ...
- navicat不同数据库数据传输
复制fo的t_fo_account表结构和数据到base库 结果
- websocket个人理解总结
WebSocket 释义:聊天室.服务.套接字.协议 引用:https://www.ibm.com/developerworks/cn/web/1112_huangxa_websocket/index ...
- 售货员的难题(codevs 2596)
题目描述 Description 某乡有n个村庄(1<n<=15),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)是已知的,且A村到B村与B村到A村 ...
- POJ 3036 Honeycomb Walk
http://poj.org/problem?id=3036 在每一个格子可以转移到与这个各自相邻的六个格子 那么设置转移变量 只需要六个 int d[6][2] = {-1, 0, -1, 1, 0 ...
- BZOJ1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草
数轴上n<=1000个点,从p出发以任意顺序走到所有的点,求到达每个点的时间之和的最小值. 好题!看起来水水的实际易错! 显然的结论是经过一个区间点之后肯定落在左端点或右端点上,谁没事最后还往中 ...
- Android视图组成View
视图组成View 创建时间: 2013-9-13 10:51 更新时间: 2013-9-13 11:04
- Gearman 初窥【转载】
Gearman是一个分发任务的程序框架,可以用在各种场合,与Hadoop相 比,Gearman更偏向于任务分发功能.它的任务分布非常简单,简单得可以只需要用脚本即可完成.Gearman最初用于Live ...