An example of the JavaScript design mode and javascript Design Mode

Source: Internet
Author: User

An example of the JavaScript design mode and javascript Design Mode

Appearance mode (facade mode) is a relatively simple and ubiquitous mode. The appearance mode provides a high-level interface that makes it easier for clients or subsystems to call.

It is represented by a piece of simple code:

Copy codeThe Code is as follows:
Var getName = function (){
Return "svenzeng"
}
Var getSex = function (){
Return 'man'
}

If you need to call the getName and getSex functions respectively, you can use a higher-level interface getUserInfo to call.

Copy codeThe Code is as follows:
Var getUserInfo = function (){
Var info = a () + B ();
Return info;
}

The answer is obvious. The cooking master in the dining hall won't cook these two dishes in one pot because you have made a duck and a cabbage order. He is more willing to provide you with a roast duck meal package. Similarly, in programming, we need to ensure that the function or object is at a reasonable granularity as much as possible.

Another advantage of the appearance mode is that the real implementation details can be hidden from the user, and the user only cares about the interface at the highest level. For example, in the story of the roast duck meal package, you do not care whether the master is roast duck or fried cabbage, or where the duck grows.

Finally, let's write an example of the appearance mode we have used:

Copy codeThe Code is as follows:
Var stopEvent = function (e) {// both stop the default event behavior and bubble
E. stopPropagation ();
E. preventDefault ();
}


The appearance mode in the design mode is generally used under what circumstances

The appearance mode is to put multiple complex classes in one appearance class for unified processing, so that external calls only deal with the appearance, without removing the call relationship between the internal classes, simplifies the complexity of client calls. Just like the example of sweet and sour pork ribs in the "software tips-Design Patterns" book: the cook is equivalent to the appearance, and what we want to eat (such as sweet and sour pork ribs) you just need to ask the Cook (call the appearance) and the cook (appearance) will give it to us. In this process, we do not have to know the production process of sweet and sour pork ribs, this reduces the coupling between client calls and various processing classes.
I suggest you take a look at the book "software tips-Design Patterns". The examples are easy to understand and have gained a lot! I hope the above content will help you!

Is the design pattern very useful in JavaScript development?

The design pattern is a set of summary of code design experiences that are repeatedly used, known by most people, classified and catalogued.
Since it is a summary of experience, it must be the result of the previous work. It is applicable everywhere, not only in java programming, but now C # follows up with the design pattern in many places.

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.