Javacript design mode-----appearance mode

Source: Internet
Author: User

Appearance mode is to provide a simple interface for the external one way, because the module internal methods are complex, can not be publicly disclosed, then we need a unified and simple external method (API) to invoke these intrinsic functions. At this point we can use the Appearance mode:

var module = (function () {var name = ' Bobi '; var _p = function () {//internal method, not public console.log (' Your name is ' + name);} var _q = function () {//internal method, not public console.log (' My name is ' + name);} var _c = function (m) {//internal method, do not expose name = M}return{f:function (m) {///public method _c (m);//Call Private method if (name = = ' Bobi ') {_p ()}else{_q ( ); }}} });

In module, all private methods do not need to be public, we just exposed an F method, as a bridge interface, F inside will execute call each internal method. We don't need to have a relationship with _q,_p's execution, just care about the public method of F. Here, F is used as a simple API to access these methods.

There are some drawbacks to the appearance pattern: that is, the performance problem of hierarchical access, which means that functions that could have been executed one level now need a second layer to implement, so when considering using this approach we have to consider the benefits and costs of doing so.

Javacript design mode-----appearance mode

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.