外观模式

作者: 周文洪 | 来源:发表于2013-08-21 09:43 被阅读232次

【风趣的解释】

Facade Mode

美眉们越来越聪明了,知道男人宁愿花点钱也不愿意陪她们逛街。是男人,你懂得!美眉们都学会网购了,没事闲着就在淘宝上逛 ,免去了整天拖着男人在商场里溜达,付个款简单的跟“1”一样。在支付宝输个密码、点个确定就OK。这个支付界面就是一个外观模式很好的例子,只需用户执行简单的操作,就能自动完成很复杂的功能(处理买家、卖家、快递、银行之间的业务)。

【正式的解释】

外观模式

外观模式为子系统中的一组接口提供一个统一的高层接口,使得子系统更易于使用。

【Python版】

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

#支付系统
class PaySystm(object):
    def pay(self):
        print "Payments completed."
#卖家系统
class SellerSystem(object):
    def sendOut(self):
        print "Seller sended out."
#快递系统
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版】

//支付系统
function PaySystem(){

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

//卖家系统
function SellerSystem(){

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

//快递系统
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.
 */

相关文章

网友评论

    本文标题:外观模式

    本文链接:https://www.haomeiwen.com/subject/ohvftttx.html