設計模式-(17)策略模式 (swift版)

來源:互聯網
上載者:User

標籤:print   enum   interface   balance   context   super   圖片   rate   ext   

一,概念:

  策略模式定義了一系列的演算法,並將每一個演算法封裝起來,而且使他們可以相互替換,讓演算法獨立於使用它的客戶而獨立變化。

 

二,使用情境

  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版)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.