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())
        //用了门面模式客户端代码就变的如此的简单了
    })()
  • 相关阅读:
    P6057 [加油武汉]七步洗手法
    LC 1349. Maximum Students Taking Exam (Hungarian / Max Flow)
    P1879 [USACO06NOV]玉米田Corn Fields
    P1433 吃奶酪 (TSP)
    LC 1349. Maximum Students Taking Exam
    获取XML中的值
    TimeZoneInfo类的使用
    XML 克隆节点
    网络协议概述:物理层、连接层、网络层、传输层、应用层详解
    Vuex
  • 原文地址:https://www.cnblogs.com/yaowen/p/6892227.html
Copyright © 2011-2022 走看看