JS design mode single case mode module mode

Source: Internet
Author: User
Tags object return

About Design Patterns
Let's talk about design patterns first. Many people feel that the "design pattern" is very iffy, Gof gang's "Designing Patterns" as a programming Bible, but I think it is not necessary. Design pattern plainly, is in a specific environment to solve a certain kind of common problems of a routine, according to this routine to do will be very handy. However, this does not mean that these design patterns are set on the universal, not to say that the set of design patterns to do things must be the best maintenance, performance best, the supremacy of the. Even if you do not know what design pattern, as long as the idea is correct, can also write design patterns.
A master says that design patterns are actually a remedy for language defects. Here are two layers of meaning, the first is that the design pattern is based on language, because there are defects in language, lack of ability, so need design mode to provide a general solution to make up for these deficiencies; Secondly, if you are separated from the specific language and the specific environment, the model may even be a bondage, Design patterns are not absolutely correct.
After understanding the above point of view, the following is a single example of JS to see how the JavaScript design pattern is going on.

Single case mode (Singleton)
The traditional single example pattern is probably the case (Java version):
public class singleton{
private static Singleton instance;
private int property;//Some properties
Private Singleton () {
A private constructor is used to ensure that only one instance
}
public static synchronized Singleton getinstance () {
if (instance = = null) {
Instance = new Singleton ();
}
return instance;
}
public void Method () {//Some methods}
The traditional singleton pattern is characterized by the use of private constructors to ensure that there is only one instance, so that the same instance object is invoked in different places. However this point for JS is a bit weak burst, because JS is to allow global variables, the solution to a single example is very simple, the definition of a global object is finished (is not feel very primitive, completely no mode to say):
var Jssingleton = {
Property: "Something",
Method:function () {
Console.log (' Hello World ');
}
Call is also very simple, no need to take into account what kind of name, anywhere access Jssingleton are the same object, there is no need to worry. If you're mad to tell me, you're just a a hungry man, lazy-style single case? If you really create objects are very resource-intensive, or you are a certain degree of performance cleanliness, it can only let JS parody to learn the traditional way:
var Jslazysingleton = (function () {
var instance;
Solve the problem of private constructors with closures (explained later)
function init () {
return {
Property: ' Some thing ',
Method:function () {}
}
}
return {
Getinstance:function () {
If the instance exists, it returns, and the call to Init () does not exist.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.