zoukankan      html  css  js  c++  java
  • js38---门面模式

    (function(){
        //门面
        function addEvebtFacade(el,type,fn){
            if(window.addEventListener){
                //使用与火狐浏览器
                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())
        //用了门面模式客户端代码就变的如此的简单了
    })()
  • 相关阅读:
    东方通Linux应用部署手册
    TonWeb6.1Linux安装文档
    达梦数据库适配问题
    达梦数据库8安装手册
    中标麒麟7虚拟机安装手册
    线性构成图标绘制样例
    布尔运算知识讲解
    UI设计中的软件知识
    无法用排他锁锁定该数据库,以执行该操作。 (Microsoft SQL Server,错误: 5030)
    【1】如何学习操作系统
  • 原文地址:https://www.cnblogs.com/yaowen/p/6892227.html
Copyright © 2011-2022 走看看