用JavaScript实现的设计模式之commandline(命令行)模式

</div><div data-lake-id="2c6c919b167e0b59e435228046511514">// Priority: ActiveX > HTML5 > Flash > Form(default)</div><div data-lake-id="518b4e458e68ddce258d8c85b53e7db1">function isActiveXSupported(){</div><div data-lake-id="83c767b09cc3486bde36ddf7f495bb48"> //...</div><div data-lake-id="31941aceee6e58ea58c76d3011ce695c"> return false;</div><div data-lake-id="9d92ec0c3c66f0ee106f4a9adf17a9b4">}</div><div data-lake-id="2a30c4a18df1b44cce4b02bab0a600cc">function isHTML5Supported(){</div><div data-lake-id="25d309cd71de8e6f456cc80ad9dbba31"> //...</div><div data-lake-id="b403ef30df9bcee55d550496ed6012b8"> return false;</div><div data-lake-id="4e931381a41609bc7284912f597a0f0e">}</div><div data-lake-id="40e75fd578081b668d7210047b68c0f9">function isFlashSupported(){</div><div data-lake-id="eea5fab09d6f465ab8755efc63ce425c"> //...</div><div data-lake-id="7bbe38b2295bc5e0f2ab0d511182880c"> return false;</div><div data-lake-id="7476a9f2b555981c175ec87fb3a280db">}</div><div data-lake-id="5e10345f1f7f0003290449dcfeafd019">var uploadAPI;</div><div data-lake-id="46726d2b2cd80369b2d4e0d8237f10e5">if ( isActiveXSupported()) {</div><div data-lake-id="919f98351b892404e614c01f27ef736a"> // lots of initialization work</div><div data-lake-id="dbda853a717cfc2d63486fffe0aa96c0"> uploadAPI = { "name": "ActiveX"};</div><div data-lake-id="be8d553a677927fe43b06dea795706f7">}</div><div data-lake-id="e093bf084f2cc3f040b2d9ce84c93e2d">else if( isHTML5Supported()) {</div><div data-lake-id="4d38a6922e62fa1d1260585c6020f39c"> // lots of initialization work</div><div data-lake-id="bfbb2d26238f34b62cdd814a1d12381a"> uploadAPI = { "name": "HTML5"};</div><div data-lake-id="da4eeb167c31eea9e9b64f1c6f4e7850">}</div><div data-lake-id="133c5379fcd3c3c39ca34e723c46f04e">else if( isFlashSupported()) {</div><div data-lake-id="d92b9e052e3b3d3834b6beaa1e7d6d9a"> // lots of initialization work</div><div data-lake-id="a9f5613bd76c985ce30f8790cac8f828"> uploadAPI = { "name": "Flash"};</div><div data-lake-id="0cec54e2f543de9cafcbd6e54e9f890a">}</div><div data-lake-id="b984e2c7df5a3f8e2f5e5a7dffcca215">else {</div><div data-lake-id="cb351d8e0cbffd1c6e3abcab90fdd1ba"> // lots of initialization work</div><div data-lake-id="a24d388345397aec2de0e36c06ca9e5f"> uploadAPI = { "name": "Form"};</div><div data-lake-id="ad49825666a62871426d1f8166f1ba38">}</div><div data-lake-id="b5206c732836f48394d353931c27e274">console.log(uploadAPI);</div><div data-lake-id="26f40f639db4a17ea355f310a358709d">

我们可以使用CommandLine设计模式,将这些冗长的IF-ELSE语句消除:

用JavaScript实现的设计模式之commandline(命令行)模式

</div><div data-lake-id="34d31c719239a94036dd74604a8d4a1b">Function.prototype.after = function( func ){</div><div data-lake-id="9a339d38dad4ebfea191841ffa97f8a5">    var _self = this;</div><div data-lake-id="9a2fca4b55a3a65c0fde16e3cd1d24e7">    return function() {</div><div data-lake-id="d4f0bf8efc408a6a46ac88c6c9a7bca9">       var ret = _self.apply( this, arguments );</div><div data-lake-id="fb37fd224680ade1e25d98d5d9e0ecb5">       if ( ret  ) {</div><div data-lake-id="69924f760a98b7c75dbf72e70fa9980a">          return ret;</div><div data-lake-id="133bf9dc2d8bcc065b7f35ad382b7dc9">       }</div><div data-lake-id="7e605395193603e7f337d15ec43a6038">       return func.apply( this, arguments);</div><div data-lake-id="b31b9eb451097273e66049f763f880dc">    }</div><div data-lake-id="cf2485ba61f0bc075b72ab210c41470b">}</div><div data-lake-id="ce06ca679fed5cc92f5bcbb5dfe42e90">// Priority: ActiveX > HTML5 > Flash > Form(default)</div><div data-lake-id="9293b7a74685bb3a71c58bbbc9951272">var getActiveX = function() {</div><div data-lake-id="4f7aa0af44ab4fec1126ed717cf6546a"> try {</div><div data-lake-id="117af64700d7ec73ab9de885d659fbf1">  // lots of initialization work</div><div data-lake-id="cc72f27a7a08d69e7451f2d509c0d4a5">  a();</div><div data-lake-id="e2712425506196d491c5651fd033df9a">  return { "name": "ActiveX"};</div><div data-lake-id="09f96b89d8c07f8bb8f7bb7e1bbf8a65"> }</div><div data-lake-id="5ab10a895d5595b6824b716e3c0a76fa"> catch (e) {</div><div data-lake-id="7de1583525e7ae16d1b50309281f804c">  // user broswer does not support ActiveX</div><div data-lake-id="58f8fbc738b13daa410bb6c89da0b58b">  return null;</div><div data-lake-id="e6bcbc288e77908ee1d03792d96c49d3"> }</div><div data-lake-id="ee39ebb00bc971f365d8508700155ef3">}</div><div data-lake-id="0d1bc92168b008fcfa42d4c8c0229313">var getHTML5 = function() {</div><div data-lake-id="65f892a33d4b68b067dba1218f0ab3ce"> try {</div><div data-lake-id="8892279c94357a94044f0ec2656931ef">  // lots of initialization work</div><div data-lake-id="1335d193eed8275b40f8242c9d6f5d49">  return { "name": "HTML5"};</div><div data-lake-id="ceb8f3b2e9e276fb47cca1878a4c7f35"> }</div><div data-lake-id="3120130bf899dbf5c105dacbaa00f534"> catch (e) {</div><div data-lake-id="da4ce3e9f1c9bb663e3a356ecbbf2eda">  // user broswer does not support HTML5</div><div data-lake-id="c204a4c18298224501ab663004b0b528">  return null;</div><div data-lake-id="7d6aa5e04d7f60e5679da8c2d89cbe6b"> }</div><div data-lake-id="10a89b66406097165a58cb39e528a032">}</div><div data-lake-id="97a8f72966d0c37ada363827b8bad460">var getFlash = function() {</div><div data-lake-id="fab2e21fb0bab1df535fa17230417aae"> try {</div><div data-lake-id="5da544adfede2632437f2e31b8275173">  // lots of initialization work</div><div data-lake-id="34943b77ca64694013fcb84462c4200a">  return { "name": "Flash"};</div><div data-lake-id="e005444fa70a7d1b14ef876ab23c5782"> }</div><div data-lake-id="57b856aa0d00d2ea42d27e6f4130e736"> catch (e) {</div><div data-lake-id="c8bd9034b602154a5dc0977cb54c4b10">  // user broswer does not support Flash</div><div data-lake-id="c3bce77553df8a84317d08c8f6e62cef">  return null;</div><div data-lake-id="8e9908456412015477d2693612d6d34d"> }</div><div data-lake-id="d4fb19e39a396e6ea66b373c26c0e5f2">}</div><div data-lake-id="e8079a165c8e1579cde1f4f789e7c7ce">var getForm = function() {</div><div data-lake-id="0c8950c14e3ec2ebfc9a7170390330a6"> return { "name": "Form"};</div><div data-lake-id="c2870e6f939680fdf195834f362f8766">}</div><div data-lake-id="ea3ee8e5b93f4bc569bda097fd9be4f4">var uploadAPI = getActiveX.after(getHTML5).after(getFlash).after(getForm)();</div><div data-lake-id="2a8e7001a140c0809b00ed8f491b3163">console.log(uploadAPI);</div><div data-lake-id="bb32b076915f40848ef2f65e76bc81aa">


上一篇:JAVA学习day10


下一篇:2018阿里云IoT极客创新挑战赛(GXIC)开启