From 73848c5104cee218cd37e420728ca7fc40d5b1e2 Mon Sep 17 00:00:00 2001 From: kingcos <2821836721v@gmail.com> Date: Thu, 8 Dec 2016 00:25:23 +0800 Subject: [PATCH] Strategy Pattern --- 04-Strategy_Pattern.playground/Contents.swift | 108 ++++++++++++++++++ .../contents.xcplayground | 4 + README.md | 1 + README_CN.md | 1 + 4 files changed, 114 insertions(+) create mode 100644 04-Strategy_Pattern.playground/Contents.swift create mode 100644 04-Strategy_Pattern.playground/contents.xcplayground diff --git a/04-Strategy_Pattern.playground/Contents.swift b/04-Strategy_Pattern.playground/Contents.swift new file mode 100644 index 0000000..e2918df --- /dev/null +++ b/04-Strategy_Pattern.playground/Contents.swift @@ -0,0 +1,108 @@ +//: Playground - noun: a place where people can play +// Powered by https://maimieng.com from https://github.com/kingcos/Swift-3-Design-Patterns + +import UIKit + +// 协议 +protocol CashSuper { + func acceptCash(_ money: Double) -> Double +} + +// 普通 +struct CashNormal: CashSuper { + func acceptCash(_ money: Double) -> Double { + return money + } +} + +// 打折 +struct CashRebate: CashSuper { + let monenyRebate: Double + + init(_ moneyRebate: Double) { + self.monenyRebate = moneyRebate + } + + func acceptCash(_ money: Double) -> Double { + return money * monenyRebate + } +} + +// 满减 +struct CashReturn: CashSuper { + let moneyCondition: Double + let moneyReturn: Double + + init(_ moneyCondition: Double, _ moneyReturn: Double) { + self.moneyCondition = moneyCondition + self.moneyReturn = moneyReturn + } + + func acceptCash(_ money: Double) -> Double { + var result = money + + if result >= moneyCondition { + result = money - Double(Int(money / moneyCondition)) * moneyReturn + } + return result + } +} + +// 优惠方式 +enum DiscountWays { + case byDefault, twentyPersentOff, threeHundredSubOneHundred +} + +struct CashContext { + private let cs: CashSuper + + init(_ cs: CashSuper) { + self.cs = cs + } + + func getResult(_ money: Double) -> Double { + return cs.acceptCash(money) + } +} + +struct CashContextWithSimpleFactoryPattern { + private let cs: CashSuper + + init(_ type: DiscountWays) { + switch type { + case .twentyPersentOff: + cs = CashRebate(0.8) + case .threeHundredSubOneHundred: + cs = CashReturn(300, 100) + default: + cs = CashNormal() + } + } + + func getResult(_ money: Double) -> Double { + return cs.acceptCash(money) + } +} + +// 策略模式 +var type = DiscountWays.twentyPersentOff +var cc: CashContext +switch type { +case .twentyPersentOff: + cc = CashContext(CashRebate(0.8)) +case .threeHundredSubOneHundred: + cc = CashContext(CashReturn(300, 100)) +default: + cc = CashContext(CashNormal()) +} +cc.getResult(200) + +// 策略模式与简单工厂模式结合 +var cs = CashContextWithSimpleFactoryPattern(.byDefault) +cs.getResult(100) + +cs = CashContextWithSimpleFactoryPattern(.twentyPersentOff) +cs.getResult(200.5) + +cs = CashContextWithSimpleFactoryPattern(.threeHundredSubOneHundred) +cs.getResult(650) diff --git a/04-Strategy_Pattern.playground/contents.xcplayground b/04-Strategy_Pattern.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/04-Strategy_Pattern.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/README.md b/README.md index 5c617ef..eb0cd2f 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ If my code did help you, would you please give me a Star 🌟? It will courage m 1. [Simple Factory Pattern](01-Simple_Factory_Pattern.playground/Contents.swift) 2. [Factory Method Pattern](02-Factory_Method_Pattern.playground/Contents.swift) 3. [Abstract Factory Pattern](03-Abstract_Factory_Pattern.playground/Contents.swift) +4. [Strategy Pattern](04-Strategy_Pattern.playground/Contents.swift) ## Reference diff --git a/README_CN.md b/README_CN.md index fedfc0d..ebf4d36 100644 --- a/README_CN.md +++ b/README_CN.md @@ -22,6 +22,7 @@ 1. [简单工厂模式(Simple Factory Pattern)](01-Simple_Factory_Pattern.playground/Contents.swift) 2. [工厂方法模式(Factory Method Pattern)](02-Factory_Method_Pattern.playground/Contents.swift) 3. [抽象工厂模式(Abstract Factory Pattern)](03-Abstract_Factory_Pattern.playground/Contents.swift) +4. [策略模式(Strategy Pattern)](04-Strategy_Pattern.playground/Contents.swift) ## 文章