{"id":1901,"date":"2014-11-04T12:09:51","date_gmt":"2014-11-04T17:09:51","guid":{"rendered":"http:\/\/alejandroayala.solmedia.ec\/?p=1901"},"modified":"2014-11-04T12:12:14","modified_gmt":"2014-11-04T17:12:14","slug":"como-evitar-el-uso-excesivo-de-ifelse","status":"publish","type":"post","link":"https:\/\/alejandroayala.solmedia.ec\/?p=1901","title":{"rendered":"Como evitar el uso excesivo de if\/else"},"content":{"rendered":"<p>Necesitamos llamar a varios m\u00e9todos y tenemos un m\u00e9todo central que dependiendo de alguna condici\u00f3n deberemos dirigir hacia una u otra funci\u00f3n de nuestro sistema. \u00bfQui\u00e9n no ha sido testigo mudo de alg\u00fan c\u00f3digo que por poca fortuna est\u00e1 en nuestras manos y se parece a algo as\u00ed?<\/p>\n<p><!--more--><\/p>\n<p>[java]<br \/>\npublic String methodCaller(String nombreMetodo) throws IOException {<br \/>\n    if(nombreMetodo.equals(&quot;metodo1&quot;)) {<br \/>\n        return metodo1(atributo(&quot;algo&quot;));<br \/>\n    }<br \/>\n    else if(nombreMetodo.equals(&quot;metodo2&quot;)) {<br \/>\n        return metodo2(atributo(&quot;algo mas&quot;));<br \/>\n    }<br \/>\n    else if(nombreMetodo.equals(&quot;metodo3&quot;)) {<br \/>\n        return metodo3(atributo(&quot;algo menos&quot;));<br \/>\n    }<br \/>\n    else if(nombreMetodo.equals(&quot;metodo4&quot;)) {<br \/>\n        return metodo4(atributo(&quot;algo mas&quot;));<br \/>\n    }<br \/>\n    else if(nombreMetodo.equals(&quot;metodo6&quot;)) {<br \/>\n        return metodo6(atributo(&quot;algo mas&quot;),<br \/>\n\t\t\tInteger.parseInt(atributo(&quot;cantidad&quot;)));<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Veamos como podemos mejorar este c\u00f3digo y <strong>evitar el uso excesivo de if else<\/strong>.<br \/>\nSe puede hacer de nuestros m\u00e9todos sean objetos que implementen una interfaz, veamos como hacerlo y como nos ayudar\u00e1<\/p>\n<h3>Interfaz<\/h3>\n<p>[java]<br \/>\npublic interface MiMetodo {<br \/>\n   public String call();<br \/>\n}<br \/>\n[\/java]<\/p>\n<h3>Implementaci\u00f3n<\/h3>\n<p>[java]<br \/>\npublic class MetodoUno implements MiMetodo {<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<h3>Mapping &amp; Llamada<\/h3>\n<p>[java]<br \/>\nprivate Map&lt;String, MiMetodo&gt; mappings = new HashMap&lt;String,MiMetodo&gt;();<\/p>\n<p>static {<br \/>\n    mappings.put(&quot;metodo1&quot;, new MetodoUno());<br \/>\n   \/\/.. otros mappings<br \/>\n}<\/p>\n<p>public String methodCaller(String nombreMetodo) throws IOException {<br \/>\n    MiMetodo miMetodo = mappings.get(nombreMetodo);<br \/>\n    return miMetodo.call();<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Con esto nuestros m\u00e9todos implementaran la interfaz y definir\u00e1n el contrato, recuperamos el objeto del mapa dependiendo del nombre del m\u00e9todo y la llamada a <em>call<\/em> que sea implementada por cada clase se ejecuta.<\/p>\n<p>F\u00e1cil de leer, mantener y no hemos afectado el funcionamiento de nuestro sistema.<\/p>\n<p>Fuente: <a href=\"http:\/\/stackoverflow.com\/questions\/13451840\/using-large-if-else-string-comparison-to-call-different-methods-more-efficient#494\">http:\/\/stackoverflow.com\/questions\/13451840\/using-large-if-else-string-comparison-to-call-different-methods-more-efficient#494<\/a><\/p>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=a1ejo_ayala\" class=\"twitter-share-button\" data-size=\"large\">Twittear<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Necesitamos llamar a varios m\u00e9todos y tenemos un m\u00e9todo central que dependiendo de alguna condici\u00f3n deberemos dirigir hacia una u otra funci\u00f3n de nuestro sistema. \u00bfQui\u00e9n no ha sido testigo mudo de alg\u00fan c\u00f3digo que por poca fortuna est\u00e1 en nuestras manos y se parece a algo as\u00ed?<\/p>\n","protected":false},"author":1,"featured_media":690,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[472,471,506],"_links":{"self":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1901"}],"collection":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1901"}],"version-history":[{"count":4,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1901\/revisions"}],"predecessor-version":[{"id":1905,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1901\/revisions\/1905"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/media\/690"}],"wp:attachment":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}