Write in front
Writing easy-to-maintain code, the most important aspect of which is the ability to find recurring themes in code and optimize them, which is the most valuable part of design patterns
Speaking of which ...
There is an article in head first design mode that says the mind of the use of patterns,
1, Beginner "mind": "I want to find a model for Hello World"
2, intermediate personnel mode: "Maybe here I need a single-piece mode"
3. The enlightened Mind: "It is quite natural to use a decorator mode here."
But,but,but, not to learn, even beginners are not the na ...
Let's look at the origins of design patterns:
The >>> design pattern originally originated in Christopher Alexander, a civil engineer in Vienna, Austria, when the model language was first created to solve complex engineering projects, and 30 years ago, software development engineers began incorporating the design principles written by Alexander into the first design pattern document.
>>>1995 years, published the "Design mode: Reusable Object-oriented Software Foundation", so after the experience of the development engineers to summarize and use, to grow into today's design pattern, has been widely used, is the primary development staff to improve programming skills guide.
So let's find a pattern for our Hello world, "罒 ω 罒" ... The JavaScript I use
Module (modules) mode
Modules are an integral part of any powerful application framework and are widely used in the framework of the program, helping us to clearly separate the code units in the organization Project ,
In module mode, you can have a common/private method and variable for an object, thereby masking special parts from the global scope
We're going to have a cartoon version of module~.
Believe that the smart you have observed, in module mode, there are two important points is public and private,
Module mode provides a way to mix public/private methods and variables to prevent them from leaking into the global scope and conflict with other developer interfaces.
With this pattern, only one public APIis returned, and everything else remains in the private closure.
Let's write a simple example:< implement a shopping cart via module mode >
In module mode, we noticed that we returned an object. He will automatically assign the value to Basketmodule so that we can interact with him.
Our functions can enjoy the freedom of private functions, because they are not exposed to other parts of the page,
When we try to output console.log (basket), we find that it is not working properly because basket only exists in the scope of the Basketmodule closure, not in the returned public object.
Module mode changes
1, the introduction of mixed
The introduction of patterns and variations shows how global variables are passed as parameters to the module's anonymous function if you want to use jquery or underscore in your pattern, and so on.
The methods in global variables are used to pass parameters to the module by means of global variables (jQuery, underscore).
2. Elicit
We declare a global variable without needing to use him. And then you can introduce the global variable using the introduction of a mixed method.
We have a public variable and method in the console output MyModule,
But Module,privatevariable and Privatemethod are still invisible to the outside world, and thus maintain the purity of global variables .
Revealing module (revelation) mode
After we understand the module pattern, let's look at a slightly improved version of the--revealing module mode:
The key to revealing module is that exposing public pointers to private functions and properties
This improved version of the module pattern created by Heilmann can make the scripting syntax more consistent. at the bottom of the template code, it is easy to point out which functions and variables can be publicly accessed, thus improving readability
However, the revealing module is more vulnerable than the modules created by the module mode, so use it with special care ('s finger licking ( ̄▽ ̄), hey, I'll notice)
Write it in the back.
The advantage of the module pattern is that it supports private data, in which the public part of the code is exposed to the private part, and the private part of the class cannot be contacted by the outside world.
So what are the disadvantages of module mode? As we access public and private members in different ways, when we want to change visibility, we need to modify every place that has ever used that member. There are some other drawbacks.
Although this module is still quite useful.
Reference books:
"JavaScript design mode"
"Head First design mode"
-module mode of "JavaScript" design pattern and its improvement