外觀模式

【風(fēng)趣的解釋】

Facade Mode

美眉們越來越聰明了,知道男人寧愿花點(diǎn)錢也不愿意陪她們逛街。是男人,你懂得!美眉們都學(xué)會網(wǎng)購了,沒事閑著就在淘寶上逛 ,免去了整天拖著男人在商場里溜達(dá),付個款簡單的跟“1”一樣。在支付寶輸個密碼、點(diǎn)個確定就OK。這個支付界面就是一個外觀模式很好的例子,只需用戶執(zhí)行簡單的操作,就能自動完成很復(fù)雜的功能(處理買家、賣家、快遞、銀行之間的業(yè)務(wù))。

【正式的解釋】

外觀模式

外觀模式為子系統(tǒng)中的一組接口提供一個統(tǒng)一的高層接口,使得子系統(tǒng)更易于使用。

【Python版】

#-*- coding:utf-8 -*-

#支付系統(tǒng)
class PaySystm(object):
    def pay(self):
        print "Payments completed."
#賣家系統(tǒng)
class SellerSystem(object):
    def sendOut(self):
        print "Seller sended out."
#快遞系統(tǒng)
class ExpressSystem(object):
    def express(self):
        print "Expressors delivered."

#高層接口,簡單易用,只能支付一次哦
class Facade(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            paySys = PaySystm()
            paySys.pay()

            sellerSys = SellerSystem()
            sellerSys.sendOut()

            expSys = ExpressSystem()
            expSys.express()

            cls.__instance = super(cls.__class__, cls).__new__(
                                 cls, *args, **kwargs)
        return cls.__instance

if __name__ == "__main__":
    facade = Facade()

"""

Payments completed.
Seller sended out.
Expressors delivered.
"""

【JS版】

//支付系統(tǒng)
function PaySystem(){

}
PaySystem.prototype.pay = function(){
    console.log('Payments completed.');
};

//賣家系統(tǒng)
function SellerSystem(){

}
SellerSystem.prototype.sendOut = function(){
    console.log('Seller sended out.');
};

//快遞系統(tǒng)
function ExpressSystem(){

}
ExpressSystem.prototype.express = function(){
    console.log('Expressors delivered.');
}

//高層接口,簡單易用,只能支付一次哦
function Facade(){
    if(typeof Facade.instance == 'object'){
        return Facade.instance; 
    }

    var paySys,
        sellerSys,
        expSys;
    paySys = new PaySystem();
    paySys.pay();

    sellerSys = new SellerSystem();
    sellerSys.sendOut();

    expSys = new ExpressSystem();
    expSys.express();

    Facade.instance = this;
}

var facade = new Facade();

/*console out

Payments completed.
Seller sended out.
Expressors delivered.
 */
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容