Javascript设计模式(摘译)

说明: 未完成。。。更新中。。。。

一、javascipt设计模式分类

设计模式分类有很多标准,最流行的三种如下

1)  creational  --  主要关注对象创建

Creational design patterns deal directly with object initialization procedures focusing on the creation of situation-specific objects. Without thinking about how objects are created, a level of complexity can be added to the design of an application. Creational design patterns work to solve this problem by adding a layer to the object creation process.

创建型设计模式直接处理对象的初始化程序,重点关注创建基于特定场景的对象。它不关注如何创建对象,其复杂性的层次直接加入到应用程序的设计中。创建型设计模式通过在对象创建过程上加上一层来解决问题。

2)   structural  --  主要关注对象的组合方法

Structural design patterns are ones that focus on easing the relationship between different components of an application. They help to provide stability by ensuring that if one part of the app changes, the entire thing doesn't need to as well.

3)   behavioral --   主要关注对象间的通信方式

Behavioral design patterns emphasize flexibility through the identification of common communication patterns between various objects.

二、日常使用的javascript设计模式

1)The Module Pattern 


2)The Revealing Module Pattern 

 

    var account = function(){
        var balance = 0;

        var deposit = function(money){
            balance + = money;
            console.log("balance after deposti: ",balance);
            sendMsg();
        };

        var withdraw = function(money){
            balance -= money;
            console.log("balance after withdraw",balance);
            sendMsg();
        };

        //私有方法
        var sendMsg = function(){
            console.log("sending message!")
        };

        //公共方法 -- send outside module
        return {
            deposit:deposit,
            withdraw: withdraw
        }
    };

    var a1 = account();
    a1.deposit(100);
    a1.withdraw(20);
    a1.sendMsg();  //could have a alert

 

3)The Singleton Pattern 

            var PersonSingleton =(function(){
                var instantiated;
                function init(){
                    function myOtherMethod() {
                        alert( 'my other method' );
                    }
                    return{
                        name: 'Anonymous',
                        work: function(){
                            console.log(this.name + "is working");
                        },
                        someOtherMethod: myOtherMethod
                    }
                }
                return{
                    //handles the prevention of additional instantiations
                    getInstance: function(){
                        if(!instantiated){
                            instantiated = init();
                        }
                        return instantiated;
                    }
                }
            })();

            var p1 = PersonSingleton.getInstance(); 
            p1.work();  //return Anonymouse
            var p2 = PersonSingleton.getInstance();
            p2.work();  //return Anonymouse


4)The Observer Pattern 


5)The Mediator Pattern 


6)The Prototype Pattern 


7)The Facade Pattern 


8)The Factory Pattern

 

参看:

  • https://carldanley.com/javascript-design-patterns/#creational-design-patterns

 

转载于:https://www.cnblogs.com/JoannaQ/p/4966091.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值