Skip to content

Commit

Permalink
Strategy Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcos committed Dec 7, 2016
1 parent b3a2938 commit 73848c5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
108 changes: 108 additions & 0 deletions 04-Strategy_Pattern.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions 04-Strategy_Pattern.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 文章

Expand Down

0 comments on commit 73848c5

Please sign in to comment.