An example of the appearance pattern of a JavaScript design pattern

  • 2020-03-30 04:05:57
  • OfStack

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

A simple piece of code:


var getName = function(){
return " svenzeng "
}
var getSex = function(){
return ' man'
}

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


var getUserInfo = function(){
var info = a() + b();
return info;
}

The obvious answer is that the restaurant's stir-fry chefs don't fry a roast duck and cabbage in the same wok just because you've ordered them. He would rather offer you a set meal of cooked duck rice. Also in the program design, we need to ensure that the function or object as far as possible in a reasonable granularity, after all, not everyone like to eat roasted duck and just like to eat cabbage.

The appearance pattern also has the advantage of hiding real implementation details from the user, who CARES only about the interface at the highest level. For example, in the story of the duck set meal, you don't care whether the chef cooks the duck or the cabbage first, and you don't care where the duck grew up.

Finally, write an example of a look pattern that we've all used:


var stopEvent = function( e ){   //Both prevent event default behavior and bubbling
e.stopPropagation();
e.preventDefault();
}


Related articles: