Proxy(代理)

Protection Proxy

/*:

### Example
*/
protocol DoorOperator {
    func open(doors: String) -> String
}

class HAL9000 : DoorOperator {
    func open(doors: String) -> String {
        return ("HAL9000: Affirmative, Dave. I read you. Opened \(doors).")
    }
}

class CurrentComputer : DoorOperator {
    private var computer: HAL9000!

    func authenticate(password: String) -> Bool {

        guard password == "pass" else {
            return false;
        }

        computer = HAL9000()

        return true
    }

    func open(doors: String) -> String {

        guard computer != nil else {
            return "Access Denied. I'm afraid I can't do that."
        }

        return computer.open(doors: doors)
    }
}
/*:
### Usage
*/
let computer = CurrentComputer()
let podBay = "Pod Bay Doors"

computer.open(doors: podBay)

computer.authenticate(password: "pass")
computer.open(doors: podBay)

Virtual Proxy

/*:

### Example
*/

protocol HEVSuitMedicalAid {
    func administerMorphine() -> String
}

class HEVSuit : HEVSuitMedicalAid {
    func administerMorphine() -> String {
        return "Morphine administered."
    }
}

class HEVSuitHumanInterface : HEVSuitMedicalAid {
    lazy private var physicalSuit: HEVSuit = HEVSuit()

    func administerMorphine() -> String {
        return physicalSuit.administerMorphine()
    }
}
/*:
### Usage
*/
let humanInterface = HEVSuitHumanInterface()
humanInterface.administerMorphine()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 動機(jī) 有時(shí),我們需要對一個對象的訪問進(jìn)行控制。比如說如果我們有一些開銷較大的對象,但是只需要使用其中一部分方法,我...
    holysu閱讀 1,092評論 0 5
  • 原文收錄在我的 GitHub博客 (https://github.com/jawil/blog) ,喜歡的可以關(guān)注...
    微醺歲月閱讀 2,477評論 0 20
  • 摘要 本篇筆記針對Java設(shè)計(jì)模式中最難理解的代理者模式進(jìn)行講解,從靜態(tài)代理、動態(tài)代理,及Java相關(guān)代理類的應(yīng)用...
    木木彬閱讀 621評論 1 4
  • 假如回到17歲: 1.我會認(rèn)真對待不喜歡的老師,與不喜歡的人相處更能鍛煉自己 2.選擇是有代價(jià)的。我填報(bào)志愿一定不...
    謝小仙閱讀 228評論 0 0
  • 茵茵草地 你跑著跳著 歡脫地追著泡泡 我看在眼里 刻在心里 我的寶貝 你是我祈福最好的回應(yīng) 你爽朗的笑聲 治愈我所...
    曦城365閱讀 255評論 0 2

友情鏈接更多精彩內(nèi)容