前端十大设计原则

观察者模式:

    <script type="text/javascript">
        // Subject(小宝宝)
        class Subject{
            constructor(name){
                //小宝宝的状态
                this.name = name;
                this.state = "开心";
                this.observers = []; //放观察者的
            }

            // attach 需要将观察者放到自身中
            attach(ther){
                this.observers.push(ther); 
            }

            // 改变状态,
            setState(state){
                this.state = state;
                //状态更新后,要通过给观察者
                this.observers.forEach(ther=>{
                    ther.update(this);
                })
            }
        }

        class Observer{
            constructor(name){
                this.name = name;
            }
            //观察小宝宝的状态
            update(baby){
              console.log(this.name+":"+baby.name +"现在的状态是:"+baby.state);
            }
        }


        let baby = new Subject("小宝宝");

        let father = new Observer("爸爸");
        let mother = new Observer("妈妈");

        baby.attach(father);
        baby.attach(mother);

        baby.setState("不开心");
    </script>

发布订阅者模式

 <script type="text/javascript">
        // 发布者, 订阅者 , 出版社
        var ChuBanShe={
            _authorArr :[], //所有的订阅者
            on(author){
                this._authorArr.push(author);
            },
            emit(value){ //发布 ,推送给订阅者
              this._authorArr.forEach(item=>{
                  //console.log(item.xxxx);
                  item.read(value);
              })
            }
        }

        ChuBanShe.on({"name":"张三",age:18,msg:"", read:function(value){
            console.log(this.name+"读取",value)
        }})

        ChuBanShe.on({"name":"李四",age:18,msg:"", read:function(value){
            console.log(this.name+"读取",value)
        }})

        ChuBanShe.on({"name":"王五",age:18,msg:"", read:function(value){
            console.log(this.name+"读取",value)
        }})

        ChuBanShe.emit("今日分享,设计模式,好好理解 ")
    </script>

工厂模式

<script type="text/javascript">
   
 

        var msgFactory = function (type,value) {
           if(this instanceof msgFactory){
               return new this[type](value);
           }else{
              return new msgFactory(type,value);
           }
        }

        msgFactory.prototype = {
            email: function (value) {
                var reg = /^ [ A-Za-z0-9!# $ %&'"“” + /= ? ^ _`{} | ~, ( ) :;<> [ ] - . ] * @ [ A-Za -z0-9- ] * . [ A-Za-z ] + ( ?: . [ A-Za-z ] + ) ? ( ?: . [ A-Za-z ] + ) ? $/;
                if (reg.test(value)) {
                   this.msg= "ok"
                } else {
                    this.msg= "邮件格式不正确"
                }
            },
            phone: function (value) {
                var reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
                if (reg.test(value)) {
                    this.msg = "ok"
                } else {
                    this.msg = "手机号码格式不正确"
                }
            },
            identity: function (value) {
                var reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
                if (reg.test(value)) {
                    this.msg = "ok"
                } else {
                    this.msg = "身份证格式不正确"
                }
            }
        }
    
       let result =new msgFactory("phone","13699212345");
       console.log(result)

       
    </script>

单例模式:

 <script type="text/javascript">
        var btn = document.getElementById("btnZheZhao");
        var div;
        btn.onclick = function () {
            createZheZhao();
        }
    </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值