设计模式-(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条件时,生怕会因为自己的疏忽而使代码天崩地裂,哈哈,本文的目的就是来解决这种不安全感的, ...
随机推荐
- POJ 3659 Cell phone Network (树的最小点覆盖, 树形DP)
题意: 给定一棵树,每个点可以覆盖自己和相邻的点, 求最少要多少个点覆盖图 #include <cstdio> #include <cstring> #include < ...
- react native 标签出错.
这种错误为标签错误,没办法,你只能往标签上找了,但不一定是<Text></Text>,我是在<TextInput></TextInput>上出错的,多了 ...
- iOS第三方语音-微信语音
网址链接:http://pr.weixin.qq.com/ 里面包含了微信语音和图像,集成很简单,下载方demo后会有个文档,按照流程来(因为它只提供了真机的.a文件,所以只能用真机哦,不然会报错) ...
- 什么样的经历,才能领悟成为架构师? >>>
什么样的经历,才能领悟成为架构师? >>> 本文主要分析 SpringBoot 的启动过程. SpringBoot的版本为:2.1.0 release,最新版本. 一.时序图 还是老 ...
- [NOIP2003] 提高组 洛谷P1038 神经网络
题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神经网络的研究一直是当今 ...
- MongoDB基本管理命令操作
1. 查看所有数据库: show dbs 或: show databases 注意: 该命令不会显示新创建的空数据库,若想显示需要向空数据库插入一些数据. MongoDB中默认的数据库为test,若果 ...
- 关于HTML文件、JS文件、CSS文件
把JS和CSS脚本写在html里和写在独立文件里有什么区别? 1. 都写在html里是性能最优的方案. 2. 都写在html里是可维护性最差的方案. 3. 分开写在js.css.html是可维护性最有 ...
- Android 学习路线图(转载自https://blog.csdn.net/lixuce1234/article/details/77947405)
程序设计 一.java (a)基本语法(如继承.异常.引用.泛型等) Java核心技术 卷I(适合入门) 进阶 Effective Java中文版(如何写好的Java代码) Java解惑 (介绍烂Ja ...
- Spring Cloud(8):Sleuth和Zipkin的使用
场景: 某大型电商网站基于微服务架构,服务模块有几十个. 某天,测试人员报告该网站响应速度过慢.排除了网络问题之后,发现很难进一步去排除故障. 那么:如何对微服务的链路进行监控呢? Sleuth: 一 ...
- curl -L 跟随跳转
curl -L 跟随跳转 加上-v 就可以看见详细信息: 学习了:https://www.cnblogs.com/davicelee/archive/2011/11/19/cURL.html http ...