JavaScript Design Patterns

Source: Internet
Author: User

In the work need some front-end JS code, but write relatively simple, usually is the function, and then see the code, but still can not be very good to write a clearer code, recently in the Web front end of the communication group download some of the design mode of video, after reading hope to tidy up a bit, The design pattern of code to organize out, easy to exchange learning, but also easy to review their own later.

First, JavaScript design pattern concept

Design patterns are a set of reusable, thought-mature, categorized and countless practical design experiences that are strong. Design patterns are used to make system code reusable, extensible, decoupled, easier to understand, and to guarantee code reliability. Design patterns make code development truly engineering; Design patterns make the cornerstone of software engineering the same as the structure of a building. Only a solid foundation to build a good structure, to cover the strong building. is also a step forward for our advanced developers.

ii. development of design patterns and their application in JavaScript

Design patterns have been used in programming for many years, and it was first collated in a book co-authored by Erich Gamma, Richard Hlem, Palph Johnson and Jhon Vlissides. has now been applied to a variety of programming languages, the greatest charm is that all kinds of programming language performance is the same, but the syntax is slightly different. For example, it is easy to convert the Java implementation mode to C.

JavaScript is a particularly flexible language that was thought to be a toy language in the early years and could only do a little errands for Web whitewash. Project engineering is not very large, and it is not easy to talk about design patterns in JavaScript applications, but with the rise of Nodejs and HTML5 and web2.0, JavaScript itself is becoming more and more important.

But many of the things that should have been JavaScript, and not as a formal part, over the years, people have used their computer programming ideas, using a lot of obscure techniques to achieve a lot of JavaScript designers have not anticipated the task, such as the implementation of various design patterns, and object-oriented programming.

Three, design principles

The root cause of design patterns is for code reuse, which increases maintainability. There are the following principles:
"Open and close principle" on the expansion of development, to modify the closed, PS College entrance examination papers. Explanation: The test paper is in line with the majority of students, has been out, but in order to test the ability of students, need to add some difficult topics, this time can not modify the current test paper, you may add additional questions in the back.

The "Richter conversion principle" subclass inherits the parent class, which can be run independently, PS pirated discs. Explanation: Genuine CDs can be played, pirated discs can also be played.

"Dependency reversal principle" refers to an object, if the object has the underlying type, direct reference to the underlying, PS three monk water, directly from the well, but the middle of the hole to put in a bucket. Explanation: The water in the well, do not have to first hit the pool next to the tank, and then from the tank to the barrel to carry the mountain, directly from the wells into the bucket to carry the mountain can be.

Composition, aggregation reuse principle new objects should use some existing objects to make them part of the new object, PS has some parts of the camera and buys a new camera. Explanation: I have some parts of the camera, and then buy some parts to assemble a camera to take pictures, there is no need to buy a new camera, resulting in waste.

The "Dimitri principle" an object should have as little understanding of other objects as the PS reality object. Explanation: We can know a lot about our girlfriend, but it's bad for other people's girlfriends to know too well.

Later, I will also do a series of explanations for some common design patterns.

A single-instance pattern of JavaScript pattern design

The constructor mode of JavaScript pattern design

The builder mode of JavaScript pattern design

The Factory mode of JavaScript mode design

The proxy mode of JavaScript mode design

JavaScript Mode design Command mode

JavaScript Design Patterns

Related Article

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.