JavaScript Design Patterns

Source: Internet
Author: User

One, single case mode:

The singleton pattern is the most commonly used pattern in JavaScript, which is to put its own code under a namespace, which has the advantage of reducing the use of global variables and avoiding naming conflicts in the case of multi-person co-development. This benefit is very convenient to maintain, as in the following example:

1 var foo = {2     name: ' Dog ',3     function() {  4         Console.log (this. Name); 5     }6}; 7 foo.action (); // called

Or

1 varDog =function() {2      This. Name = ' dog ';3      This. Action =function() {4         returnConsole.log ( This. Name);5     };6 action ();7 };8Dog ();//called
Second, the Factory mode:

Factory mode is the creation of the object's methods to the external object, the advantage is to solve the interaction between the objects, that is, coupling, avoid using new to instantiate the object, help to create modular code, maintenance is also convenient. The factory model is divided into simple Factory mode and abstract Factory mode.

Simple Factory mode:

1 var m = {}; 2 function () {3     console.log (' dog '); 4 }; 5 var function () {6    m.action (); 7 }; 8 Demo ()// call

Abstract Factory mode:

To design an abstract class, this class cannot be instantiated, can only be used to derive subclasses, and finally implements the factory method through the extension of the subclass. As follows:

1 varf =function() {};2F.prototype = {3C:function() {4         Throw NewError (' can\ ' t use this method ');//if calling this method will cause an error because it is used to derive a subclass that cannot be instantiated5     }6 };7 varE =function() {8F.call ( This);9 }TenE.prototype =Newf (); OneE.prototype.constructor =e; AE.PROTOTYPE.C =function() { -Console.log (' This method is redefine ')); - } the //called - varDemo =Newe (); -DEMO.C ();
Third, the façade mode:

The façade pattern is often the most intimate friend of the developer, and he is almost the core principle of all JavaScript libraries. Façade mode has two functions: one is to simplify the interface of the class, and the other is to eliminate the coupling between the class and the customer code that uses it. Examples are as follows:

1 function A () {  2  3} 4 function B () {  5  6} 7 function sum () {  8    A (); 9     B (); Ten }
Four, adapter mode:

The adapter pattern can be used to fit between an existing interface and an incompatible class. On the surface, the adapter pattern is much like a façade pattern, which wraps other objects and changes the interface they render. The difference between the two is how they change the interface, and the façade element presents a simplified interface that does not provide an additional choice, and sometimes it makes some assumptions in order to make it easier to accomplish common tasks. Instead, the adapter converts an interface to another interface, which does not filter some capabilities or simplifies the interface.

1 varstr = {2A: ' A ',3B: ' B ',4C: ' C '5 };6 functionI (S1,S2,S3) {7Console.log (S1 + ', ' + s2 + ', ' +S3);8 }9 functionDemo (o) {Ten I (O.A,O.B,O.C); One}
Five, Decorator mode:

Decorator mode can be used to transparently wrap objects in another object with the same interface, and adorners can be used to add functionality to an object, which can be used instead of a large number of subclasses. The decorator pattern and the composition pattern have a lot in common, they all implement a uniform interface with the wrapped object and will pass any method bar to these objects. However, the combination mode is used to organize many sub-objects into a whole, while the decorator pattern is used to add methods to the existing objects without modifying them or deriving subclasses from them. As follows:

1 var m = {}; 2 m.child = {}; 3 function () {}; 4 function () {};
Vi. Observer Pattern:

The Observer pattern is a powerful tool for managing the relationship between people and their tasks. There are two characters in the Observer pattern: The Observer and the observed. The benefit of this pattern is that you can observe the state of an object in the program and be notified when it changes.

1 varF1 =function(){2   //Code3 }4 varF2 =function(){5    //Code6 }7Addevent (element, ' click ', F1);8Addevent (element, ' click ', F2)9 TenElement.onclick =F1; OneElement.onclick = F2;
Seven, bridge mode:

Bridging mode is the isolation of abstraction from implementation, which changes independently over time. When designing a JavaScript API, it can be used to weaken the coupling between classes and objects. It can also be used to join multiple classes together. For example:

1 varClass1 =function(a,b,c) {2      This. A =A;3      This. B =b;4      This. C =C;5 };6 varClass2 =function(d) {7      This, db=D;8 };9 varDemo =function(a,b,c,d) {Ten      This. one =NewClass1 (a,b,c); One      This. both =NewClass2 (d); A};

JavaScript Design Patterns

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.