<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eliezer Rodrigues &#187; padrões de projetos</title>
	<atom:link href="http://www.eliezer.com.br/post/tag/padroes-de-projetos/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eliezer.com.br</link>
	<description>Desenvolvimento de software, linguagens de programação, artigos, utils, scrum, testes, linux e tecnologia de um modo geral</description>
	<lastBuildDate>Tue, 06 Dec 2011 02:12:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Sabe aquele monte de &#8220;if&#8221; e &#8220;else&#8221; no código? Limpando tudo em Java.</title>
		<link>http://www.eliezer.com.br/post/sabe-aquele-monte-de-if-e-else-no-codigo-limpando-tudo-em-java/</link>
		<comments>http://www.eliezer.com.br/post/sabe-aquele-monte-de-if-e-else-no-codigo-limpando-tudo-em-java/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 04:05:28 +0000</pubDate>
		<dc:creator>eliezer</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[boas praticas]]></category>
		<category><![CDATA[padrões de projetos]]></category>
		<category><![CDATA[strategy]]></category>

		<guid isPermaLink="false">http://www.eliezer.com.br/?p=37</guid>
		<description><![CDATA[Uma das coisas que considero mais ruins de se ler em um código são os malditos ifs encadeados. Veja, não é fácil de se ler, não é confiável e bem mais propício a bug. Portando se tem muitos ifs, algo não está correto. Vou utilizar polimorfismo para implementar o padrão de projeto Strategy. Um fiz [...]]]></description>
			<content:encoded><![CDATA[<p>Uma das coisas que considero mais ruins de se ler em um código são os malditos ifs encadeados. Veja, não é fácil de se ler, não é confiável e bem mais propício a bug. Portando se tem muitos ifs, algo não está correto.</p>
<p>Vou utilizar polimorfismo para implementar o padrão de projeto Strategy.</p>
<p>Um fiz um caso simples apenas para exemplificar o estudo.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StringUtils <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span>	REMOVE_ALL_SPACES	<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;remove-all-spaces&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span>	FILTER_ONLY_NUMBERS	<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;filter-only-numbers&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> format<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> action, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>REMOVE_ALL_SPACES.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>action<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> value.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>s&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>FILTER_ONLY_NUMBERS.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>action<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> value.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>D&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Bom não é tão feio, pq tb quem nunca viu algo pior que isso que atire a primeira pedra. <img src='http://www.eliezer.com.br/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Vamos fazer com que esse método format seja mais simples. No caso ele está com apenas duas comparações, mas lembre-se que  poderiam ser 18. <img src='http://www.eliezer.com.br/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Mas dá pra melhorar e muito utilizando Enum. Veja:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> StringFormat <span style="color: #009900;">&#123;</span>
&nbsp;
	TRIM_SPACES<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> format<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> value.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>s&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>,
	ONLY_NUMBERS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> format<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> value.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>D&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #003399;">String</span> format<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>E agora o nosso objetivo final, remover os ifs e else do método format:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StringUtils <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> format<span style="color: #009900;">&#40;</span>StringFormat action, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> action.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Legal né?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eliezer.com.br/post/sabe-aquele-monte-de-if-e-else-no-codigo-limpando-tudo-em-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

