模块模式与单例模式之间有何不同? [英] Different between Module Pattern and Singleton Pattern?

查看:117
本文介绍了模块模式与单例模式之间有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到,在某些项目中, Module Pattern 使用而不是 Singleton Pattern ,反之亦然。 / p>

我想知道, Module Pattern Singleton Pattern

解决方案

模块模式 是指通用机制的代码模块化。它可以很好地分割一个类跨几个文件,因为您可以独立定义构造函数和各种原型方法组。每个模块通常都封装在一个闭包中,以创建静态的局部变量 - 这被称为显示模块模式



单例模式 是指实例创建的限制,通常使用懒惰初始化



当然可以考虑模块模式是单身形式的专业化(参见维基百科文章),构造函数及其原型对象将占用单一实例的一部分。



然而,您也可以将它们独立组合:定义使用的类的模块单身进近。


I've seen that in some projects, Module Pattern uses instead of Singleton Pattern and vice versa.

I want to know exactly, what's the different between Module Pattern and Singleton Pattern?

解决方案

Module pattern in javascript refers to the modularisation of code for a common mechanism. It works quite well to split one "class" across several files as you can define constructor and various groups of prototype methods independently. Each of the modules is usually wrapped inside a closure to create static, local variables - this is called revealing module pattern.

Singleton pattern in javascript refers to the restriction of instance creations, often using lazy initialisation.

Of course you can consider the module pattern to be a specialisation of the singleton pattern (see the Wikipedia article), the constructor and its prototype object would take the part of the "single instance" then.

Yet you also could combine them "independently": A module that defines a class which uses the singleton approach.

这篇关于模块模式与单例模式之间有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆