js38---門面模式

來源:互聯網
上載者:User

標籤:需要   --   ...   logs   yun   run   Firefox   new   false   

(function(){    //門面    function addEvebtFacade(el,type,fn){        if(window.addEventListener){            //使用與Firefox瀏覽器            alert(1);            el.addEventListener(type,fn,false);        }else if(window.attachEvent){            //適用於IE的            alert(2);            el.attachEvent("on"+type,fn);        }else{            alert(3);            el["on"+type] = fn;        }    }    document.write("<a id=‘but1‘ href=‘#‘>click</a>");    var el = document.getElementById("but1");    addEvebtFacade(el,"click",function(){        alert("ok")    })})()
/** * 用2個DAO來體現門面模式 */(function(){    //人員類    var PersonDao = new Interface("PersonDao",["getInfo","learn",            "marry"]);    var Person = function(){        this.name = "YUNFENGCHENG";        this.address = "BEIJING";        this.getInfo = function(){            return "名字: "+this.name +" 地址: "+this.address;        }        this.learn = function(){            document.write("學習");        }        this.marry = function(){};        //驗證實現的介面        Interface.ensureImplements(this,PersonDao);    }    //DOG DAO    var DogDao = new Interface("DogDao",["call","run","getInfo"]);    var Dog = function(){        this.name = "DAHUANG";        this.getInfo = function(){            return "狗狗的名字: "+this.name;        }        this.run = function(){};        this.call = function(){};        Interface.ensureImplements(this,DogDao);    }    //需求是現在需要給養的夠辦了相應寵物領養證件  需要人和狗狗的資訊可以    //1.不用門面    //用戶端程式    function action(person,dog){        //當做養狗證的號碼        var r = "GG"+new Date().getDate()+Math.floor(Math.random()*11);        var str = "辦證成功 :編號 "+r        +"<br>主人資訊: "+person.getInfo()        +"<br>狗狗的資訊: "+dog.getInfo();        document.write(str);    }    action(new Person(),new Dog());    document.write("<br>..........................");    //使用門面模式    //負載的事交給門面來做    function facade(person,dog){        //當做養狗證的號碼        var r = "GG"+new Date().getDate()+Math.floor(Math.random()*11);        this.str = "辦證成功 :編號 "+r        +"<br>主人資訊: "+person.getInfo()        +"<br>狗狗的資訊: "+dog.getInfo();    }    facade.prototype.action = function(){        return this.str;    }    //用戶端程式    function action2(person,dog){        document.write(new facade(person,dog).action());    }    action2(new Person(),new Dog())    //用了門面模式用戶端代碼就變的如此的簡單了})()

 

js38---門面模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.