<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-582121105715363092</id><updated>2025-05-25T03:17:09.330-07:00</updated><category term="howto"/><category term="Java"/><category term="android"/><category term="clean code"/><category term="design pattern"/><category term="Almanac"/><category term="best practise"/><category term="Eclipse"/><category term="app"/><category term="Database"/><category term="design"/><category term="junit"/><category term="pattern"/><category term="reusable"/><category term="singleton"/><category term="DAO"/><category term="Data Transfer Object"/><category term="SQLite"/><category term="agility"/><category term="refactoring"/><category term="test"/><category term="web"/><category term="Activity"/><category term="AdMob"/><category term="Advertisement"/><category term="AndroidAnnotations"/><category term="AndroidStudio"/><category term="Color"/><category term="DTO"/><category term="Factory"/><category term="Javascript"/><category term="View"/><category term="Windows"/><category term="adapter pattern"/><category term="agile"/><category term="annotations"/><category term="applied"/><category term="builder pattern"/><category term="command pattern"/><category term="decorator"/><category term="generic"/><category term="mobile"/><category term="null object"/><category term="null object pattern"/><category term="observer pattern"/><category term="phonegap"/><category term="pitfall"/><category term="quality"/><category term="reusable comments"/><category term="singleton pattern"/><category term="state"/><category term="state pattern"/><category term="strategy"/><category term="synchronized"/><category term="visitor"/><category term="AD_UNIT_ID"/><category term="ARGB"/><category term="AsyncTask"/><category term="Bean"/><category term="Bridge"/><category term="Callback"/><category term="Canvas"/><category term="Coloring"/><category term="Colour"/><category term="DMO"/><category term="DO"/><category term="Data Access Object"/><category term="Dependency Inversion"/><category term="Domain Object"/><category term="Draw"/><category term="Drawing"/><category term="Fragment"/><category term="Gson"/><category term="Hollywood Principle"/><category term="IDE"/><category term="Install"/><category term="IntelliJ"/><category term="Interpreter"/><category term="Inversion of control"/><category term="IoC"/><category term="Iterator"/><category term="JBoss"/><category term="JBoss7"/><category term="JBossAS7"/><category term="Json"/><category term="MVC"/><category term="Mapper test"/><category term="Mediator"/><category term="Mixing"/><category term="MySQL"/><category term="ORM"/><category term="ORMLite"/><category term="Paint"/><category term="ProgressBar"/><category term="RCP"/><category term="RGB"/><category term="Registry"/><category term="Repository"/><category term="Router"/><category term="Snippet"/><category term="SoC"/><category term="Splash"/><category term="Tools"/><category term="Vista"/><category term="abstract factory"/><category term="actionBar"/><category term="adapter"/><category term="appearance"/><category term="appl"/><category term="attachment"/><category term="autosave"/><category term="baseadapter"/><category term="bitstate"/><category term="chain of responsibility"/><category term="checklist"/><category term="class design"/><category term="clean code  developer theory coder"/><category term="code convention"/><category term="code style"/><category term="composite"/><category term="concept"/><category term="contest"/><category term="contraindication"/><category term="cpf"/><category term="cpfformatter"/><category term="customize"/><category term="dash"/><category term="db"/><category term="dialog"/><category term="documentation"/><category term="dotted"/><category term="edittext"/><category term="email"/><category term="enum"/><category term="facade"/><category term="factory pattern"/><category term="flyweight"/><category term="formatter"/><category term="good"/><category term="hint"/><category term="hybrid"/><category term="if"/><category term="image"/><category term="imageloader"/><category term="initialization"/><category term="instanceOf"/><category term="layout"/><category term="lazy"/><category term="line"/><category term="manifest"/><category term="memento"/><category term="monetize"/><category term="multilanguage"/><category term="multilingual"/><category term="native"/><category term="observer"/><category term="package"/><category term="pagseguro"/><category term="parse"/><category term="polymorphism"/><category term="project"/><category term="prototype"/><category term="proxy"/><category term="rename"/><category term="resources"/><category term="rest"/><category term="roboelectric"/><category term="samsung"/><category term="scrollview"/><category term="seal"/><category term="server"/><category term="shortcuts"/><category term="simple factory"/><category term="spark"/><category term="statements"/><category term="strings"/><category term="stroke"/><category term="switch"/><category term="syntax"/><category term="syntaxionary"/><category term="system"/><category term="tdd"/><category term="template method"/><category term="texthint"/><category term="textview"/><category term="textwatcher"/><category term="tizen"/><category term="tizen-build"/><category term="tizen-ide"/><category term="tizen-sdk"/><category term="tizen-studio"/><category term="tutorial"/><category term="util"/><category term="valuable"/><category term="webservice"/><title type='text'>Clean Code Development - Quality Seal</title><subtitle type='html'>Improve your skills and share your expirience!</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cleancodedevelopment-qualityseal.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>84</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-1225116348758818251</id><published>2017-01-12T08:34:00.004-08:00</published><updated>2020-10-12T12:26:06.323-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="hybrid"/><category scheme="http://www.blogger.com/atom/ns#" term="native"/><category scheme="http://www.blogger.com/atom/ns#" term="package"/><category scheme="http://www.blogger.com/atom/ns#" term="samsung"/><category scheme="http://www.blogger.com/atom/ns#" term="tizen"/><category scheme="http://www.blogger.com/atom/ns#" term="tizen-build"/><category scheme="http://www.blogger.com/atom/ns#" term="tizen-ide"/><category scheme="http://www.blogger.com/atom/ns#" term="tizen-sdk"/><category scheme="http://www.blogger.com/atom/ns#" term="tizen-studio"/><category scheme="http://www.blogger.com/atom/ns#" term="web"/><title type='text'>How to build and package tizen web and native projects (hybrid projects)</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
I&#39;ve been developing for the samsung tizen platform for a while. the biggest problems we were facing, was the build and package process of hybrid projects. because of the fact that the tizen platform is not so popular yet, there is still a huge need of information on how to use it and the community is not so strong. Most of the time you don&#39;t get satisfatory answers and help on their official site. Not to mention that many of the sites result in 404 or everything is written in Korean.&lt;br /&gt;
&lt;br /&gt;
For those reasons, i decided to share my experiencies with you in a very compressed that gets to the point without much &quot;bla bla bla&quot; :) And here are the choosen topics we will handle in this post:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;how to install the tizen sdk&lt;/li&gt;
&lt;li&gt;how to create web and native projects direct from the command line&lt;/li&gt;
&lt;li&gt;how to import it into the tizen studio IDE&lt;/li&gt;
&lt;li&gt;how to link native service projects with web projects&lt;/li&gt;
&lt;li&gt;how to build web project&lt;/li&gt;
&lt;li&gt;how to build native project&lt;/li&gt;
&lt;li&gt;how to create a security certificate and use it&lt;/li&gt;
&lt;li&gt;how to package web project (generate .wgt)&lt;/li&gt;
&lt;li&gt;how to package native project (generate .tpk)&lt;/li&gt;
&lt;li&gt;how to package a hybrid project (generate .wgt with native dependencies in it)&lt;/li&gt;
&lt;li&gt;how to install and run it on the wearable devices.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
The next post, i will focus on how to build it on a continuous integration like jenkins. So stay tuned and save this page if you are interested in tizen development too.&lt;br /&gt;
&lt;br /&gt;
Hope you like it enjoy it.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
OS (Operation System)&lt;/h3&gt;
Well, first of all let me say that i am a ubuntu user, so the steps bellow was done on &lt;b&gt;ubuntu 16.14 LTS&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Install tizen SDK&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;download it from: &lt;a href=&quot;https://developer.tizen.org/ko/development/tools/download?langredirect=1#installmanager&quot;&gt;https://developer.tizen.org/ko/development/tools/download?langredirect=1#installmanager&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;navigate to the donwloaded folder over terminal and give install permission to ti by typing: &lt;b&gt;&lt;i&gt;chmod +x {downloaded_file.bin}&lt;/i&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;install it by typing:&amp;nbsp;&lt;b&gt;&lt;i&gt;./{downloaded_file.bin}&lt;/i&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;just follow the wizard instructions and at the end open the Tizen Package Manager as proposed&lt;/li&gt;
&lt;li&gt;As soon as the tizen package manager has opened, install all packages wihout exception. &lt;br /&gt;(I know you&#39;ll think: why shall i install all if i don&#39;t need it all - Believe me, you&#39;ll thank me later :) That&#39;s the way we found that everything works just fine.)&lt;/li&gt;
&lt;li&gt;Install all in the suggested location &lt;b&gt;ex&lt;/b&gt;: &lt;b&gt;/home/{username}/tizen-sdk&lt;/b&gt; and so on.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;At the end you&#39;ll end up with a folder structure in your home like this: tizenIDE-data, tizen-sdk, tizen-sdk-data, tizen-studio and tizen-studio-data&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Create web and native projects direct from command line&lt;/h3&gt;
To do so, you&#39;ll need to ensure that your PATH is pointing to the downloaded tizen tools. Open your &quot;&lt;b&gt;&lt;i&gt;.bashrc&lt;/i&gt;&lt;/b&gt;&quot; file located in your home directory. (press &lt;b&gt;&lt;i&gt;Ctrl+h&lt;/i&gt;&lt;/b&gt; to show hidden files) in there, add the following lines (if not there yet)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;### exporting tizen tools&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;export PATH=$PATH:/home/ricardo/tizen-sdk/tools/ide/bin/&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;###source tizen-autocomplete&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;source /home/ricardo/tizen-sdk/tools/ide/bin/tizen-autocomplete&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Creating a default workspace first&lt;/h3&gt;
Create a folder like: &lt;b&gt;&lt;i&gt;ws_tizen_studio&lt;/i&gt;&lt;/b&gt; somewhere in your home. Ex: &lt;b&gt;&lt;i&gt;/home/ws_tizen_studio&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
this will be your workspace. As soon as you start the tizen studio IDE you can point to it. To do so, navigate to&amp;nbsp;&lt;b&gt;&lt;i&gt;/home/ricardo/tizen-studio/ide&lt;/i&gt;&lt;/b&gt; and double click the &lt;b&gt;&lt;i&gt;eclipse&lt;/i&gt;&lt;/b&gt; icon. As soon as you open your IDE for the first time, it will suggest a workspace directory. Change it to &lt;b&gt;/home/ws_tizen_studio&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Great, now we can create our sample projects like this (i am using here wearble-2.3.1 but feel free to use whatever version you need) In your terminal type:&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Create a sample tizen web project&lt;/h3&gt;
&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;b&gt;&lt;i&gt;tizen create native-project -p wearable-2.3.1 -t ServiceApp -n native -- /media/truecrypt5/dev/ws_tizen_studio/&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Create a sample tizen web project&lt;/h3&gt;
&lt;b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;i&gt;tizen create web-project -p wearable-2.3.1 &amp;nbsp;-t WebBasicapplication -n web -- /media/truecrypt5/dev/ws_tizen_studio/&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
If you are wondering how i know which tizen project types exist and how i figured it out, just type:&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen list web-project&lt;/span&gt;&lt;/i&gt;&lt;/b&gt; or&amp;nbsp;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen list native-project&lt;/span&gt;&lt;/i&gt;&lt;/b&gt; to see all project types available&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Import projects into tizen studio IDE&lt;/h3&gt;
Ok, if everything went fine, the next step will be to import those created projects into our brand new tizen studio IDE. &lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;IMPORTANT NOTE: &lt;/span&gt;&lt;/b&gt;You for sure will be asking yourself why i create the projects over command line, if i could create it over the IDE right? Well, after many days trying to figure it out why we couldn&#39;t build and package our projects, we found it out that if we create it over the tizen command line tool (CLI) than it works just fine.&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Import native project&lt;/h3&gt;
File &amp;gt;&amp;gt; Import &amp;gt;&amp;gt; select your native project.&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Import web project&lt;/h3&gt;
File &amp;gt;&amp;gt; New &amp;gt;&amp;gt; Tizen Project &amp;gt; template &amp;gt;&amp;gt; wearable v2.3.1 &amp;gt;&amp;gt; We application &amp;gt;&amp;gt; Basic UI &amp;gt;&amp;gt; More properties &amp;gt;&amp;gt; unmark: Use default location &amp;gt;&amp;gt; browse: select created web project &amp;gt;&amp;gt; finish&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Link native service projects with web projects&lt;/h3&gt;
In the tizen studio IDE &amp;gt;&amp;gt; select your web project &amp;gt;&amp;gt; right mouse click &amp;gt;&amp;gt; properties &amp;gt; tizen studio &amp;gt;&amp;gt; package &amp;gt;&amp;gt; multi &amp;gt;&amp;gt; select native project &amp;gt; OK&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Build web project&lt;/h3&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen build-web -- /home/ws_tizen_studio/web&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
the command above will create a &lt;b&gt;&lt;i&gt;.buildResult&lt;/i&gt;&lt;/b&gt; folder inside your project structure. In this folder you will encounter the project files without all unnecessary files. There is not .wgt yet. Those steps will be described a few steps later. To visualize the&lt;b&gt;&lt;i&gt; .buildResult&lt;/i&gt;&lt;/b&gt; folder it in your tizen studio IDE, make sure the hidden files are visible. To do so:&lt;br /&gt;
&lt;br /&gt;
Tizen Studio IDE &amp;gt;&amp;gt; project explorer &amp;gt;&amp;gt; dropdown arrow &amp;gt;&amp;gt; unselect: *.resources &amp;gt;&amp;gt; OK&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjba4suDBvQ46CEU3eTxBgy7tSL2HmOrtXZ_TN5cVBk981fIK5lIkoXBttMsP5XZRs5AprA8mVIclWa73cI2zGl-LpVxkh8eB2chZ4WaOEZmnIB1X5uMszgkuqBGNubOu59MVZ6biEvy_6d/s1600/tizen_ide1.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjba4suDBvQ46CEU3eTxBgy7tSL2HmOrtXZ_TN5cVBk981fIK5lIkoXBttMsP5XZRs5AprA8mVIclWa73cI2zGl-LpVxkh8eB2chZ4WaOEZmnIB1X5uMszgkuqBGNubOu59MVZ6biEvy_6d/s320/tizen_ide1.png&quot; width=&quot;238&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvGUtdft4kmjo4sWS0e1twN1E-7Db_3ekpTBlienphmJuReMWbY5hkVvpkb8jLdF56bZHNbXENarnouaHtBeHmatxG-1FrOrl2pv4oE5Pz0Us6x_SAFRKCsV09P9KOyG04Q4krP9AI-hku/s1600/tizen_ide2.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;303&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvGUtdft4kmjo4sWS0e1twN1E-7Db_3ekpTBlienphmJuReMWbY5hkVvpkb8jLdF56bZHNbXENarnouaHtBeHmatxG-1FrOrl2pv4oE5Pz0Us6x_SAFRKCsV09P9KOyG04Q4krP9AI-hku/s320/tizen_ide2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Build project native&lt;/h3&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;IMPORTANT NOTE:&lt;/span&gt;&lt;/b&gt; native projects can be run on wearable only if set to Release and arm. Debug and x86 works fine for the emulator only. The command bellow will create a Release folder inside your project with the binary and resources you&#39;ll need to package your .tpk file. How to package this project will be described next. Stay tuned.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen build-native -a arm -c llvm -C Release -- &amp;nbsp;/home/ws_tizen_studio/native&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Create a security certificate&lt;/h3&gt;
Before we can package the projects, we need a security certificate. In order to create your own certificate, run this:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;Command:&amp;nbsp;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
tizen certificate -a &lt;b&gt;{profileName}&lt;/b&gt; -p &lt;b&gt;{password}&lt;/b&gt; -c &lt;b&gt;{landcode}&lt;/b&gt; -s&lt;b&gt; {state}&lt;/b&gt; -ct &lt;b&gt;{city}&lt;/b&gt; -o &lt;b&gt;{ogranization}&lt;/b&gt; -u &lt;b&gt;{organizationUnit}&lt;/b&gt; -n &quot;&lt;b&gt;{yourName}&lt;/b&gt;&quot; -e &lt;b&gt;{yourEmail}&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;Ex:&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen certificate -a &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;tizen_profile&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt; -p &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;123456&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt; -c &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;BR&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt; -s &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Pernambuco&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt; -ct &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Recife&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt; -o &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt; -u &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Development&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt; -n &quot;&lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Ricardo Ferreira&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&quot; -e &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;email@yourCompany.com&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Ok, now you&#39;ll need to add your brand new certificate to the cli-config. To do so, enter:&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen security-profiles add -n tizen_profile -a /home/&lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;{youName}&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;/tizen-sdk-data/keystore/author/author.p12 -p 123456&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Running the command bellow, you&#39;ll be able to see all available certificates. You should see now your created &quot;&lt;b&gt;&lt;i&gt;tizen_profile&lt;/i&gt;&lt;/b&gt;&quot; certificate in the list:&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen security-profiles list&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
than check if certificate was also added to the cli-config by typing:&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen cli-config -l&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
If there is no certificate added, just add it by running this command:&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen cli-config &quot;default.profiles.path=/home/&lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;{yourName}&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;/tizen-sdk-data/ide/keystore/profiles.xml&quot;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Ok, now we are good to go and to package our projects. Lets move on!&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Package web project&lt;/h3&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen package -t wgt -s tizen_profile -- /home/ws_tizen_studio/web/.buildResult&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
You should see now a file called &lt;b&gt;web.wgt &lt;/b&gt;in the folder &lt;b&gt;.buildResult&lt;/b&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Package native project&lt;/h3&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen package -t tpk -s tizen_profile -- /home/ws_tizen_studio/native/Release&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
You should see now a file called &lt;b&gt;org.exemple.native-1.0.0-arm.tpk&lt;/b&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Package a hybrid project&lt;/h3&gt;
The next step is now to put both projects together. Because at the moment we have single packages for each project (web.wgt and org.exemple.native-1.0.0-arm.tpk) Our goal is to have a single package called web.wgt which contains both projects and its dependencies in it. The command bellow will achieve that for us:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen package -t wgt -s tizen_profile -r /home/ws_tizen_studio/native/Release/org.example.native-1.0.0-arm.tpk -- /home/ws_tizen_studio/web/.buildResult/web.wgt&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Take a look at the old web.wgt package. It has been overridden and now we have a hybrid application with both projects inside of it.&lt;br /&gt;
&lt;h3&gt;
&lt;br /&gt;&lt;/h3&gt;
&lt;h3&gt;
Install and run it on a Smart Watch&lt;/h3&gt;
In order to install the hybrid application in our wearable, we must connect to it first. First of all, go into your watch settings, enable wifi-connection, click on the wifi connection and scroll down to see the watch IP. (you&#39;ll need it to install the hybrid application) &lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;NOTE&lt;/span&gt;&lt;/b&gt;: Your PC must be connect to the same wifi connection also.&lt;b&gt;&lt;i&gt; Otherwise you can&#39;t transfer the package from the PC to the watch&lt;/i&gt;&lt;/b&gt;.&lt;br /&gt;
Ok, now navigate to the tools directory by typing:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;cd /home/&lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;{yourName}&lt;/span&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;/tizen-sdk/tools&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;./sdb connect &lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;{ip_watch}&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
As soon as the watch is connected, type: (you may try to execute the command above several times till the connection is established - It depends on the connection quality sometimes)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;./sdb install /home/ws_tizen_studio/web/.buildResult/web.wgt&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
If you have some logs in your native app (for testing purposes) you can enable it by typing this:&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;./sdb dlog org.example.native&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;NOTE:&lt;/span&gt;&lt;/b&gt; If for some reason you want to repeat those steps, don&#39;t forget to run the command bellow to clean old builds:&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen clean --&amp;nbsp;/home/ws_tizen_studio/web&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;tizen clean --&amp;nbsp;/home/ws_tizen_studio/native&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
That&#39;s all. Hope you like it. :)&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/1225116348758818251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/1225116348758818251'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2017/01/how-to-build-and-package-tizen-web-and.html' title='How to build and package tizen web and native projects (hybrid projects)'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjba4suDBvQ46CEU3eTxBgy7tSL2HmOrtXZ_TN5cVBk981fIK5lIkoXBttMsP5XZRs5AprA8mVIclWa73cI2zGl-LpVxkh8eB2chZ4WaOEZmnIB1X5uMszgkuqBGNubOu59MVZ6biEvy_6d/s72-c/tizen_ide1.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-6631828105199224815</id><published>2016-10-13T06:17:00.004-07:00</published><updated>2020-10-12T12:33:21.702-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="web"/><title type='text'>How to add / import javascript dependencies dynamically into your html</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show you, how to import / add javascript dependencies dynamically to your html files. I&#39;m playing with object orientation in javascript and in this context, I&#39;ve created a class that does exactly that and I&#39;d like to share with you.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;&#39;use strict&#39;

/** 
 * Use this class to import all dependencies dynamically into your html.
 * This class imports the dependencies only if not exits already.
 * &lt;pre&gt; * Usage example: 
 * add this line to your html. Ex: index.html
 * &amp;lt; script  src=&quot;../yourProjectPath/js/dependencies.js&quot; &amp;gt; &amp;lt; / script &amp;gt;
 * 
 * Then in your program.js used in this index.html, use it like this:
 * var dependencyArray = [];
 * dependencyArray.push(&quot;../../js/constants.js&quot;);
 * dependencyArray.push(&quot;../../js/utils.js&quot;);
 * dependencyArray.push(&quot;../../js/home.js&quot;);
 * var deps = new Dependency();
 * deps.addToBody(dependencyArray);
 * or alternativelly
 * deps.addToHead(dependencyArray);
 * &lt;/pre&gt;
* @author Ricardo Ferreira
 */
var Dependency = function(){
 
 //*****************************************************************************
 // Private methods 
 // As per convention, private methods in javascript starts with underscore &quot;_&quot;
 //*****************************************************************************
 
 /**
  * private method - Use this method to import all dependencies into the html head or body. 
  * @param {String} tag value &quot;head&quot; or &quot;body&quot;. Other values than that will return false
  * @param {Array} srcArray - A string array with the source paths relative to this file
  * @returns {Bool} true the dependency has been added successfully.
  * @author Ricardo Ferreira
  */
 function _into (tag, srcArray ) {
  if(srcArray){
   // this way to check for an array is 2/3 faster 
   // then &quot;srcArray instanceof Array&quot; and much faster
   // then &quot;Array.isArray(srcArray)&quot;
   if(srcArray.constructor === Array){
    var index1;
    var index2;
    var bodyTag  = &quot;body&quot;; 
    var headTag  = &quot;head&quot;;
    var srcTag   = &quot;src&quot;;
    var scriptTag  = &quot;script&quot;;
    var shallAddDependency;
    for (index1 = 0; index1 &amp;lt; srcArray.length; index1+=1) {
     // check if dependency does not exist already
     shallAddDependency = true;
     var srcPathDelimiter = &quot;/&quot;;
     var srcPath = srcArray[index1];
     var scriptTagArr = document.body.getElementsByTagName(scriptTag);
     var javascriptName = _getJavascriptName(srcPath, srcPathDelimiter);
     for (index2 = 0; index2 &amp;lt; scriptTagArr.length; index2+=1) {
      var javascriptSrc = scriptTagArr[index2].src;
      if( _contains( javascriptSrc, javascriptName ) ){
       shallAddDependency=false;
       break;
      }
     }
     if(shallAddDependency){
      var script = document.createElement( scriptTag );
      script.setAttribute( srcTag, srcPath );
      if(tag === headTag){
       document.head.appendChild( script );
      }else if(tag === bodyTag){
       document.body.appendChild( script );
      }else{
       return false;
      }
     }
    }return true;
   }return false;
  }return false;
 }
 
 /**
  * private method - Use this method to find out the javascript file name. 
  * @param {String} srcPath that contains the whole javascript path
  * @param {String} pathDelimiter is the delimiter used to split the path
  * @returns {String} the javascript file name. Ex: home.js
  * @author Ricardo Ferreira
  */
 function _getJavascriptName (srcPath, pathDelimiter){
  var arrayOffset = 1;
  var split = srcPath.split(pathDelimiter);
  var javascriptName = split[split.length-arrayOffset];
  return javascriptName;
 }
 /**
  * private method - Use this method to check if a substring is part of another string. 
  * @param {String} string that could contain substrings
  * @param {String} substring to be checked inside of string
  * @returns {Bool} true if substring is part of string.
  * @author Ricardo Ferreira
  */
 function _contains (string, substring) { 
  var invalidIndex = -1;
  return string.indexOf(substring) != invalidIndex; 
 };
 
 //*****************************************************************************
 // Public methods
 //*****************************************************************************

 /**
  * Use this method to import all dependencies into the html body. 
  * @param {Array} srcArray - A string array with the source paths relative to this file
  * @returns {Bool} true if dependencies has been added, false otherwise.
  * @author Ricardo Ferreira
  */
 this.appendToBody = function( srcArray ) {
  var bodyTag = &quot;body&quot;; 
  return _into(bodyTag , srcArray);
 },
 /**
  * Use this method to import all dependencies into the html head. 
  * @param {Array} srcArray - A string array with the source paths relative to this file
  * @returns {Bool} true if dependencies has been added, false otherwise.
  * @author Ricardo Ferreira
  */
 this.appendToHead = function( srcArray ) {
  var headTag = &quot;head&quot;; 
  return _into(headTag, srcArray);
 }

}; 
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all. Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6631828105199224815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6631828105199224815'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2016/10/how-to-add-import-javascript.html' title='How to add / import javascript dependencies dynamically into your html'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-6595889981840167656</id><published>2016-05-11T06:19:00.003-07:00</published><updated>2020-10-12T12:30:52.862-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="android"/><category scheme="http://www.blogger.com/atom/ns#" term="AndroidStudio"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="strategy"/><title type='text'>How to define build strategies in your android app over BuildConfig</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show you, how your could define your build strategies in android studio in an elegant, enhanceable way using the strategy design pattern.&lt;br /&gt;
&lt;br /&gt;
Most of the times, Google defines its product making usage of best practises and good, enhanceable design pattern. I saw recently, in a big project we got, that they were not taking advantage of this and instead of that, they were using enumerations, resulting in a bunch of switch cases and later on another bunch of if-else cases and a lot of repeated code that breaks the &lt;b&gt;&lt;span style=&quot;color: #b45f06;&quot;&gt;DRY-principle &lt;/span&gt;&lt;/b&gt;and even more important the&lt;b&gt;&lt;span style=&quot;color: #b45f06;&quot;&gt;&amp;nbsp;open-close-principle&lt;/span&gt;&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What does it mean to be programming respecting the open-close-principle?&lt;/b&gt; It means that every time a new component is created, that i don&#39;t have to change running code. i.e. I don&#39;t have to create another enum type, another switch-case to identify this type and later on another if-else case to know, depending on the comparation result, what kind of environment i want to set.&lt;br /&gt;
&lt;br /&gt;
That&#39;s the code we got. This code has a lot of problems. It is fragile. Every new environment forces you to change a chain of steps in the code. Further it forces you to hold 4 variables in a static way to be able to share it over the application instead of hold only one object responsible for that. We define new enum types and define a new if statement running the risk of breaking existing logic. All of this can be avoided using the strategy pattern.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s take a look at the actual code, before enhancing it to visualize the problems:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;    //&amp;nbsp;somewhere in code this method was called passing the enum defined in BuildConfig&lt;/pre&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;    setEnvironmentKeys(BuildConfig.ENVIRONMENT);
&lt;/pre&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;    // later in the class who defined this method over if-statements we identify the environment
    public static void setEnvironmentKeys(ENV env){
        try{
         if (MainApplication.isProduction()) {
                CONTENT_SERVER_URL = &quot;https://content.mycompany.com/&quot;;
                PROFILE_SERVER_URL = &quot;https://profile.mycompany.com/&quot;;
                IMAGE_SERVER_URL = &quot;https://image.mycompany.com/log&quot;;
                Utils.GOOGLE_DEVELOPER_KEY = Utils.getStringRes(R.string.key_production);
            } else if (env == MainApplication.ENV.STAGING) {
                CONTENT_SERVER_URL = &quot;http://content-staging.mycompany.com/&quot;;
                PROFILE_SERVER_URL = &quot;http://profile-staging.mycompany.com/&quot;;
                IMAGE_SERVER_URL = &quot;http://image.staging.maycompany.com/&quot;;
                Utils.GOOGLE_DEVELOPER_KEY = Utils.getStringRes(R.string.key_staging);
            }  else if (env == MainApplication.ENV.STAGING_TWO) {
                Utils.GOOGLE_DEVELOPER_KEY = Utils.getStringRes(R.string.key_staging);
                CONTENT_SERVER_URL = &quot;http://content-staging2.mycompany.com/&quot;;
                PROFILE_SERVER_URL = &quot;http://profile-staging2.mycompany.com/&quot;;
                IMAGE_SERVER_URL = &quot;http://image.staging2.mycompany.com/&quot;;
            }else if (MainApplication.isDev()) {
                Utils.GOOGLE_DEVELOPER_KEY = Utils.getStringRes(R.string.key_development);
                CONTENT_SERVER_URL = &quot;https://content.dev.mycompany.com/&quot;;
                PROFILE_SERVER_URL = &quot;https://profile.dev.mycompany.com/&quot;;
                IMAGE_SERVER_URL = &quot;https://image.dev.mycompany.com/&quot;;
            }
        }catch(Exception e){
            Log.wtf(TAG, &quot;Couldn&#39;t set ENV variables. Setting to Production as default.&quot;);
        }
    }
   &amp;nbsp;&lt;/pre&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;    // and somewhere was defined the enum with the build types
    public enum ENV {
        DEVELOPMENT(&quot;DEVELOPMENT&quot;),
        STAGING(&quot;STAGING&quot;),
        STAGING_TWO(&quot;STAGING_TWO&quot;),
        OPS_PREVIEW(&quot;OPS_PREVIEW&quot;),
        PRODUCTION(&quot;PRODUCTION&quot;), ;

        private final String name;

        private ENV(String s) {
            name = s;
        }

        public boolean equalsName(String otherName){
            return (otherName == null)? false:name.equals(otherName);
        }

        public String toString(){
            return name;
        }

    }
&lt;/pre&gt;
&lt;br /&gt;
The code above is functional but when it comes to enhancement, maintainability and so on, then we can see some problems on that. We will discuss this later on. Let&#39;s now see how we could do it better by using the strategy pattern. First have a look at the uml diagram of it.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitWyJ4C-hbDZMLb3VwFVH-jcTiYpZ6gKkDVRTHdEKcHCrnEyE56Wf_-mZzb2AuVl69cg53L6N1HNfPB0xh1dFml4hx1ui4k0pwODCNes6OEkpEtYqF2hiK0QeM1qvr39FIVHU3j7H8eWht/s1600/strategy.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;171&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitWyJ4C-hbDZMLb3VwFVH-jcTiYpZ6gKkDVRTHdEKcHCrnEyE56Wf_-mZzb2AuVl69cg53L6N1HNfPB0xh1dFml4hx1ui4k0pwODCNes6OEkpEtYqF2hiK0QeM1qvr39FIVHU3j7H8eWht/s320/strategy.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
And now the little code of if:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;   
  // this method is called somewhere in code
  setEnvironment(BuildConfig.ENVIRONMENT);
  
  // then we set the environment and use it without any change in code
  private Environment environment;
  public void setEnvironment(final Environment env){
   environment = env;
  }
  /**
   * In your android studio look for the tab: &quot;Build Variants&quot; (bottom left side)
   * and select &quot;Build Variant &amp;gt; debug&quot; while developing the application.
   * This will automatically instantiate this class and assign this value to BuildConfig.ENVIRONMENT
   * @author Ricardo Ferreira
   */
  public class DeveloperEnvironment extends Environment{
   private final static String CONTENT_SERVER_URL = &quot;https://content.dev.mycompany.com/&quot;;
   private final static String PROFILE_SERVER_URL = &quot;https://profile.dev.mycompany.com/&quot;;
   private final static String S3_AMAZON_SERVER_URL = &quot;https://s3.amazon.dev.mycompany.com&quot;;
   private final static String GOOGLE_DEVELOPER_KEY = &quot;123456&quot;;
   public DeveloperEnvironment() {
    super(CONTENT_SERVER_URL, PROFILE_SERVER_URL, S3_AMAZON_SERVER_URL, GOOGLE_DEVELOPER_KEY);
   }
  }
  /**
   * Build environment strategy - Every environment phase should extend from this strategy.
   * This way, you can access your app&#39;s environment configuration over the system class 
   * BuildConfig.ENVIRONMENT everywhere without the necessity of enumerations, switch cases 
   * or a bunch of if-else statements. If you create new environments, no change in your code
   * will be needed. 
   * @author Ricardo Ferreira
   */
  public abstract class Environment{
   private final String CONTENT_SERVER_URL;
   private final String PROFILE_SERVER_URL;
   private final String S3_AMAZON_SERVER_URL;
   private final String GOOGLE_DEVELOPER_KEY;
   // ... other environment variables ...
 public Environment(
   final String contentServerUrl, 
   final String profileServerUrl,
   final String s3amazonServerUrl, 
   final String googleDeveloperKey) {
  super();
  this.CONTENT_SERVER_URL = contentServerUrl;
  this.PROFILE_SERVER_URL = profileServerUrl;
  this.S3_AMAZON_SERVER_URL = s3amazonServerUrl;
  this.GOOGLE_DEVELOPER_KEY = googleDeveloperKey;
 }
 public String getContentServerUrl() {
  return CONTENT_SERVER_URL;
 }
 public String getProfileServerUrl() {
  return PROFILE_SERVER_URL;
 }
 public String getS3AmazonServerUrl() {
  return S3_AMAZON_SERVER_URL;
 }
 public String getGoogleDeveloperKey() {
  return GOOGLE_DEVELOPER_KEY;
 }
  }
&lt;/pre&gt;
&lt;br /&gt;
Coming back to our initial discussion. Imagine now you need to add a new enviromnet. lets say staging. with the approach above you just have to say to someone: create a class who extends from environment, analogical DeveloperEnvironment, define it in your build.gradle, like you would do for the other approach also and done! It would work just fine without touching already written, functional code.The other way around you would have to change the enum, change the if-else statements.

&lt;br /&gt;
&lt;br /&gt;
Here is how you could define the strategies in your build.gradle. This will automatically assign the right value to the variable ENVIRONMENT depending on which environment you choose over Android Studio by selecting the tab Build Variants on the bottom left side of your IDE:
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;
            debuggable true
            setApplicationIdSuffix(&quot;dev&quot;)
            versionNameSuffix &quot; Dev&quot;
            buildConfigField &quot;com.treslines.learn.environment.Environment&quot;, &quot;ENVIRONMENT&quot;, &quot;new com.treslines.learn.environment.DeveloperEnvironment()&quot;
            buildConfigField &quot;boolean&quot;, &quot;DEBUGLOG&quot;, &quot;true&quot;
            manifestPlaceholders = [
                    appName         : &quot;MyApp Debug&quot;
            ]
        }
        Staging {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;
            signingConfig signingConfigs.debug
            debuggable true
            setApplicationIdSuffix(&quot;staging&quot;)
            versionNameSuffix &quot; Staging&quot;
            buildConfigField &quot;com.treslines.learn.environment.Environment&quot;, &quot;ENVIRONMENT&quot;, &quot;new com.treslines.learn.environment.StagingEnrivornment()&quot;
            buildConfigField &quot;boolean&quot;, &quot;DEBUGLOG&quot;, &quot;true&quot;
            manifestPlaceholders = [
                    appName         : &quot;MyApp Stg&quot;
            ]
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;
            signingConfig signingConfigs.release
            zipAlignEnabled true

            buildConfigField &quot;com.treslines.learn.environment.Environment&quot;, &quot;ENVIRONMENT&quot;, &quot;new com.treslines.learn.environment.ProductionEnrivornment()&quot;
            buildConfigField &quot;boolean&quot;, &quot;DEBUGLOG&quot;, &quot;false&quot;
        }
    } 
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! hope you like it! :)&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6595889981840167656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6595889981840167656'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2016/05/how-to-define-build-strategies-in-your.html' title='How to define build strategies in your android app over BuildConfig'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitWyJ4C-hbDZMLb3VwFVH-jcTiYpZ6gKkDVRTHdEKcHCrnEyE56Wf_-mZzb2AuVl69cg53L6N1HNfPB0xh1dFml4hx1ui4k0pwODCNes6OEkpEtYqF2hiK0QeM1qvr39FIVHU3j7H8eWht/s72-c/strategy.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-421472252400916229</id><published>2016-03-15T20:13:00.006-07:00</published><updated>2020-10-12T12:29:31.653-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="android"/><category scheme="http://www.blogger.com/atom/ns#" term="cpf"/><category scheme="http://www.blogger.com/atom/ns#" term="cpfformatter"/><category scheme="http://www.blogger.com/atom/ns#" term="edittext"/><category scheme="http://www.blogger.com/atom/ns#" term="formatter"/><category scheme="http://www.blogger.com/atom/ns#" term="hint"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="texthint"/><category scheme="http://www.blogger.com/atom/ns#" term="textview"/><category scheme="http://www.blogger.com/atom/ns#" term="textwatcher"/><title type='text'>Fast CPF formatter to be used in Android&#39;s EditText View combined with a TextWatcher</title><content type='html'>Hi there! Today i&#39;m gonna show to you, how i solved a common problem, while dealing with brazilian cpf in edittext view in android studio.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Problem/Chalange:&lt;/b&gt; Mask a CPF editText field such a way, that as soon as the user starts typing digits in it, the text field inserts or updates its needed signs for the user&amp;nbsp;automatically.&lt;br /&gt;
&lt;br /&gt;
The first solution was very elegant and well thought and well done with a mask like ###.###.###-## but as soon as the user starts typing very fast in the text field, the mask algorithm was very slow and swallowed some digits in some cases.&lt;br /&gt;
&lt;br /&gt;
For this reason, i took the chalange to myself and decided to create my own, fast cpf formatter. Bellow the solution that solved the problem and that you may use in your apps as you may need.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;/h3&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new CpfWatcher((EditText) findViewById(R.id.inputCpf));

    }

    public class CpfWatcher implements TextWatcher {

        private EditText inputField;
        private int flag = 0;
        private int signsBeforeChange = 0;
        private int dots = 0;
        private int dash = 0;
        private String txtBeforeChange;

        public CpfWatcher(final EditText inputField) {
            this.inputField = inputField;
            this.inputField.setInputType(InputType.TYPE_CLASS_NUMBER);
            this.inputField.setHint(&quot;###.###.###-##&quot;);
            this.inputField.addTextChangedListener(this);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            final String input = s.toString();
            inputField.removeTextChangedListener(this);
            final String formattedCpf = formatCpf(input);
            inputField.setText(formattedCpf);
            inputField.addTextChangedListener(this);
            placeCursorDependingOnUserAction(start, formattedCpf, findOutIfUserWasTypingOrDeleting(input));
        }

        private void placeCursorDependingOnUserAction(int start, String formattedCpf, Action action) {
            switch (action){
                case TYPING:{
                    dots = formattedCpf.length() - formattedCpf.replace(&quot;.&quot;, &quot;&quot;).length();
                    dash = formattedCpf.length() - formattedCpf.replace(&quot;-&quot;, &quot;&quot;).length();
                    signsBeforeChange = dots+dash;
                    inputField.setSelection(formattedCpf.length());
                    break;
                }
                case DELETING:{

                    if(start-1 &amp;lt; 0){
                        inputField.setSelection(0);
                    }else if(txtBeforeChange.equalsIgnoreCase(formattedCpf)){
                        inputField.setSelection( start );
                        flag++;
                    }
                    else{
                        dots = formattedCpf.length() - formattedCpf.replace(&quot;.&quot;, &quot;&quot;).length();
                        dash = formattedCpf.length() - formattedCpf.replace(&quot;-&quot;, &quot;&quot;).length();
                        int sigsAfterChange = dots+dash;
                        if(signsBeforeChange &amp;gt; sigsAfterChange){
                            inputField.setSelection( start-1 );
                            signsBeforeChange = sigsAfterChange;
                        }else{
                            inputField.setSelection( start );
                        }
                    }
                    break;
                }
            }
        }

        private Action findOutIfUserWasTypingOrDeleting(String input) {
            Action action = Action.TYPING;
            if(flag &amp;lt;=input.length()){
                flag = input.length();
                action = Action.TYPING;
            }else{
                flag = input.length();
                action = Action.DELETING;
            }
            return action;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            txtBeforeChange = s.toString();
        }

        @Override
        public void afterTextChanged(Editable s) { /* NOP*/ }

        public  Editable getCpf(){
           return inputField.getText();
        }

    }

    public enum Action {
        TYPING, DELETING
    }

    private String formatCpf(String cpf) {
        String[] sign = new String[]{&quot;.&quot;, &quot;.&quot;, &quot;-&quot;};
        String[] signRegEx = new String[]{&quot;\\.&quot;, &quot;\\.&quot;, &quot;-&quot;};
        int[] signPos = new int[]{4, 8, 12};
        return formatCpfDependingOnLentgh(sign, signPos, removeAllSigns(cpf, signRegEx));
    }

    private String removeAllSigns(String cpf, String[] signRegEx) {
        String empty = &quot;&quot;;
        String tmp = cpf;
        for (String sign : signRegEx) {
            tmp = tmp.replaceAll(sign, empty);
        }
        return tmp;
    }

    private String formatCpfDependingOnLentgh(String[] sign, int[] signPos, String cpfWithoutAnySign) {
        String tmp = cpfWithoutAnySign;
        int aLentgh = tmp.length();
        if (aLentgh &amp;lt;= signPos[0] - 1) {
            // nothing to format
        }else if(aLentgh == signPos[0]-1) {
            tmp = create1Punctuation(tmp, sign);
        }
        else if (aLentgh &amp;gt; signPos[0] - 1 &amp;amp;&amp;amp; aLentgh &amp;lt; signPos[1] - 1) {
            tmp = create2Punctuations(tmp, sign);
        }
        else if (aLentgh == signPos[1] - 1) {
            tmp = create3Punctuations(tmp, sign);
        }
        else if (aLentgh &amp;gt; signPos[1] - 1 &amp;amp;&amp;amp; aLentgh &amp;lt; signPos[2] - 2) {
            tmp = create3Punctuations(tmp, sign);
        }
        else if (aLentgh == signPos[2] - 2) {
            tmp = create4Punctuations(tmp, sign);
        }
        else if (aLentgh &amp;gt; signPos[2] - 2 &amp;amp;&amp;amp; aLentgh &amp;lt; signPos[2]) {
            tmp = create4Punctuations(tmp, sign);
        } else {
            tmp = trunk4Punctuations(tmp, sign);
        }
        return tmp;
    }

    private String create1Punctuation(String tmp, String[] punc) {
        return tmp + punc[0];
    }

    private String create2Punctuations(String tmp, String[] punc) {
        String s1 = tmp.substring(0, 3);
        String s2 = tmp.substring(3, tmp.length());
        return s1 + punc[0] + s2;
    }

    private String create3Punctuations(String tmp, String[] punc) {
        String s1 = tmp.substring(0, 3);
        String s2 = tmp.substring(3, 6);
        String s3 = tmp.substring(6, tmp.length());
        return s1 + punc[0] + s2 + punc[1] + s3;
    }

    private String create4Punctuations(String tmp, String[] punc) {
        String s1 = tmp.substring(0, 3);
        String s2 = tmp.substring(3, 6);
        String s3 = tmp.substring(6, 9);
        String s4 = tmp.substring(9, tmp.length());
        return s1 + punc[0] + s2 + punc[1] + s3 + punc[2] + s4;
    }

    private String trunk4Punctuations(String tmp, String[] punc) {
        String s1 = tmp.substring(0, 3);
        String s2 = tmp.substring(3, 6);
        String s3 = tmp.substring(6, 9);
        String s4 = tmp.substring(9, 11);
        return s1 + punc[0] + s2 + punc[1] + s3 + punc[2] + s4;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
&lt;/pre&gt;&lt;pre class=&quot;brush: csharp&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre class=&quot;brush: csharp&quot;&gt;&lt;div style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; white-space: normal;&quot;&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/421472252400916229'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/421472252400916229'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2016/03/fast-cpf-formatter-to-be-used-in.html' title='Fast CPF formatter to be used in Android&#39;s EditText View combined with a TextWatcher'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-8486057442077081527</id><published>2016-01-19T06:47:00.002-08:00</published><updated>2020-10-12T12:31:55.352-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="MySQL"/><category scheme="http://www.blogger.com/atom/ns#" term="rest"/><category scheme="http://www.blogger.com/atom/ns#" term="server"/><category scheme="http://www.blogger.com/atom/ns#" term="spark"/><category scheme="http://www.blogger.com/atom/ns#" term="web"/><category scheme="http://www.blogger.com/atom/ns#" term="webservice"/><title type='text'>How to create REST web services in pure java as easy as with node.js - This is so beautiful!!! </title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show you how to create &lt;span style=&quot;background-color: orange;&quot;&gt;REST web services&lt;/span&gt; as fast and &lt;span style=&quot;background-color: orange;&quot;&gt;as simple as with node.js&lt;/span&gt;, but &lt;span style=&quot;background-color: orange;&quot;&gt;with PURE java!!!&lt;/span&gt; Yes!!!! it is possible and so beautiful and clean :) I love it and i am sure you&#39;ll love it too, if you love java like i do. ;)&lt;br /&gt;
&lt;br /&gt;
Now a days everybody is using and speaking from node.js, javascript, angular, ruby, etc. There are so many &quot;frameworks&quot; to do &quot;more or less&quot; the same thing, that i was wondering me, if there was nothing to help the java-community with. And yessssss...... there is! :)&lt;br /&gt;
&lt;br /&gt;
Putting things together and inspired by the post of &quot;&lt;a href=&quot;https://www.boxuk.com/insight/blog-posts/creating-rest-api-quickly-using-pure-java&quot; target=&quot;_blank&quot;&gt;creating REST api quickly using java&lt;/a&gt;&quot; i decided to give them a try and create a more detailed post of it.&lt;span style=&quot;background-color: orange;&quot;&gt; It works so fine and nice that i must share it with you!&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Technologies involved&lt;/h3&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;We will be using &lt;a href=&quot;http://sparkjava.com/&quot; target=&quot;_blank&quot;&gt;SparkJava &lt;/a&gt;which is a tiny, compact framework, designed to develop web applications with minimal effort.&lt;/li&gt;
&lt;li&gt;To create DAO&#39;s out of the box and manipulate database as easy as&amp;nbsp;like stealing candy from a babies... :) we will use &lt;a href=&quot;http://mvnrepository.com/artifact/com.j256.ormlite/ormlite-jdbc/4.48&quot; target=&quot;_blank&quot;&gt;ormlite jdbc&lt;/a&gt; for java. Another great tool i love.&lt;/li&gt;
&lt;li&gt;As a database we will use MySQL.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
Don&#39;t worry, we will see how to add those tools to your project in a few minutes step by step. There is no mystery using it.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Creating a maven project&lt;/h3&gt;
With your IDE, i use eclipse Enide 2015, Version: Mars Release (4.5.0), Build id: Nodeclipse-20150706-0921 in this example, create a simple maven project like that:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpyNCglLPN2EKtNEwyRpj0f-7r9eD5MjBtVQrkti__b3W2w8LX7PG5Bf2Gr-LiRfp4NJKxzcFC9-mOcmXTfcIk1hBSfU0m55lzbThEeVtKGm-cvMr7yqra5GtjWq-BZDTW6bDz5UWW9O9Q/s1600/eclipse1.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;265&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpyNCglLPN2EKtNEwyRpj0f-7r9eD5MjBtVQrkti__b3W2w8LX7PG5Bf2Gr-LiRfp4NJKxzcFC9-mOcmXTfcIk1hBSfU0m55lzbThEeVtKGm-cvMr7yqra5GtjWq-BZDTW6bDz5UWW9O9Q/s320/eclipse1.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiglavQQ7Kzx7fNwuXvRdTZQ3T7EfPTLPzP1Mkj3_IHdbtZMMsF3g1MhjSWy31lt2HeI053-dRNZaJQIl0KnkeTel1yF_E4HLaRGwruY6HNr-xDVypmbCGuciqbCGR00vmiaQ2PdPCl0UXB/s1600/eclipse2.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;247&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiglavQQ7Kzx7fNwuXvRdTZQ3T7EfPTLPzP1Mkj3_IHdbtZMMsF3g1MhjSWy31lt2HeI053-dRNZaJQIl0KnkeTel1yF_E4HLaRGwruY6HNr-xDVypmbCGuciqbCGR00vmiaQ2PdPCl0UXB/s320/eclipse2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkiAOBLCHtLLOhNYhLEAsiA63J8lg4W5TJfDRWqP3YxJQwq84ZjNXQXixy3h_eS4DK6YbgBjY1l4Oess6wRaXmAk51szbaLS5YYj1teeXQcmCydhNoXwwCjPrhCpZRlH5RMO1FVhW6mPVj/s1600/eclipse3.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;269&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkiAOBLCHtLLOhNYhLEAsiA63J8lg4W5TJfDRWqP3YxJQwq84ZjNXQXixy3h_eS4DK6YbgBjY1l4Oess6wRaXmAk51szbaLS5YYj1teeXQcmCydhNoXwwCjPrhCpZRlH5RMO1FVhW6mPVj/s320/eclipse3.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Open the&lt;b&gt; pom.xml &lt;/b&gt;and insert the following code to get the jars from spark, ormlite and mysql-connector:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;lt;!-- library dependencies --&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;lt;dependencies&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;groupId&amp;gt;com.sparkjava&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;artifactId&amp;gt;spark-core&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;version&amp;gt;2.2&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;groupId&amp;gt;com.j256.ormlite&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;artifactId&amp;gt;ormlite-jdbc&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;version&amp;gt;4.48&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;groupId&amp;gt;mysql&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;artifactId&amp;gt;mysql-connector-java&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&amp;lt;version&amp;gt;5.1.38&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;lt;/dependencies&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;clear: both; text-align: left;&quot;&gt;
Defining an Entity User&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Well, now we are fine to start with our service. Let&#39;s define first an user class. Use the code snippet bellow:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;package com.companyname.modulename.server.model;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
 
@DatabaseTable(tableName = &quot;users&quot;)
public class User {
    
    @DatabaseField(generatedId = true)
    private int id;
    
    @DatabaseField
    private String username;
    
    @DatabaseField
    private String email;
    
    public User() {
        // ORMLite needs a no-arg constructor 
    }
    
    public int getId() {
        return this.id;
    }
    
    public String getUsername() {    
        return this.username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
}
&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;clear: both; text-align: left;&quot;&gt;
Defining the Server with the services inside&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Now to the cool things. :) Let&#39;s define a GET and POST method to insert and retrieve data from the database.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;package com.companyname.modulename.server;

import static spark.Spark.get;
import static spark.Spark.post;

import java.sql.SQLException;

import com.companyname.modulename.server.model.User;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import spark.Request;
import spark.Response;
import spark.Route;

public class Server {

 private static Dao&lt;user string=&quot;&quot;&gt; userDao;
 // other DAO&#39;s my be defined here...

 // TUTORIAL EXECUTION DATE: 18/01/2016
 // IMPORTANT: Before runnning this class: make sure your mysql server is running
 //
 // Run as &amp;gt; Java Application... Than you should see a message similar to
 // that bellow:
 //
 // [Thread-0] INFO spark.webserver.SparkServer - == Spark has ignited ...
 // [Thread-0] INFO spark.webserver.SparkServer - &amp;gt;&amp;gt; Listening on 0.0.0.0:4567
 // [Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.0.2.v20130417
 // [Thread-0] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@6255aad1{HTTP/1.1}{0.0.0.0:4567}
 // [qtp1296900317-19] INFO spark.webserver.MatcherFilter - The requested route [/favicon.ico] has not been mapped in Spark
 
 
 // In your Browser type in: http://localhost:4567/users/1
 // than you should see: User: username=test, email=test@test.net
 public static void main(String[] args) throws SQLException {
  initMySQL();

  // define the http action methods here...
  // get sample: http://localhost:4567/users/1
  get(&quot;/users/:id&quot;, new Route() {
   public Object handle(Request req, Response res) throws Exception {
    final User user = userDao.queryForId(req.params(&quot;:id&quot;));
    if (user != null) {
     return &quot;Username: &quot; + user.getUsername(); // or JSON? :-)
    } else {
     final int httpNotFound = 404;
     final String msg = &quot;User not found&quot;;
     res.status(httpNotFound); 
     return msg;
    }
   }
  });

  // post sample: (using postman)
  // http://localhost:4567/users?username=ricardo&amp;amp;email=ricardo@test.com
  post(&quot;/users&quot;, new Route() {
   public Object handle(Request request, Response response) throws SQLException {
    final String username = request.queryParams(&quot;username&quot;);
    final String email = request.queryParams(&quot;email&quot;);

    final User user = new User();
    user.setUsername(username);
    user.setEmail(email);

    final int createdUserId = userDao.create(user);
    final int httpCreated = 201;
    response.status(httpCreated);
    return createdUserId;
   }
  });
 }

 // make sure your mysql server is running, than:
 // connect and start to previously created MySQL database called spark
 // with username: root and password: 123456
 private static void initMySQL() throws SQLException {
  final String databaseUrl = &quot;jdbc:mysql://localhost/spark&quot;;
  final ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
  ((JdbcConnectionSource) connectionSource).setUsername(&quot;root&quot;);
  ((JdbcConnectionSource) connectionSource).setPassword(&quot;123456&quot;);
  // creates tables and DAO&#39;s to be used in GET/POST or whatever 
  // you may define in main method
  createTablesIfNotExitsAlready(connectionSource);
  initDAOs(connectionSource);
 }

 private static void createTablesIfNotExitsAlready(final ConnectionSource connectionSource) throws SQLException {
  TableUtils.createTableIfNotExists(connectionSource, User.class);
  // other tables may be defined here...
 }

 private static void initDAOs(final ConnectionSource connectionSource) throws SQLException {
  userDao = DaoManager.createDao(connectionSource, User.class);
  // other DAO&#39;s may be defined here...
 }

}

&lt;/user&gt;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;clear: both; text-align: left;&quot;&gt;
Creating the database&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Just one more thing! :) Before you can run this, make sure you have created the database called &lt;b&gt;spark&lt;/b&gt; with user &lt;b&gt;root&lt;/b&gt; and password &lt;b&gt;123456&lt;/b&gt;. To do so, under linux, first install the mysql server by typing this line in your terminal: (to open a terminal press Ctrl+Alt+T)&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;sudo apt-get install mysql-server&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
When the installer prompts the window to define the password for the root user, type &lt;b&gt;123456&lt;/b&gt; and complete the installation.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;clear: both; text-align: left;&quot;&gt;
Creating the Table User with MySQL Workbench&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
That is a nice exercise. I was not used to it, and that&#39;s way i&#39;m showing it here also. Download it and double click it to install from here&amp;nbsp;&lt;a href=&quot;https://dev.mysql.com/downloads/workbench/&quot;&gt;https://dev.mysql.com/downloads/workbench/&lt;/a&gt;&amp;nbsp;Once you are done with, start it and create a new db called spark like that:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Once you&#39;ve opened it, there should be a connection available. This is the connection you&#39;ve previously installed. Double click it and connect with your credentials. &lt;b&gt;root&lt;/b&gt; and &lt;b&gt;123456&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx0n0GtWaJalBhvdsz1Emh7VhWsOvKd7xMUz7gN3Eci2jLAC9uHQwtn8lc7eHFkQuzZ_mK4YW61o9HggnTLutmY6LwGK4AtUGkabRNWCDy7y1qajMO07B5LEG6TSKFAp-U5MItOmO0F4Km/s1600/mysql2.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;204&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx0n0GtWaJalBhvdsz1Emh7VhWsOvKd7xMUz7gN3Eci2jLAC9uHQwtn8lc7eHFkQuzZ_mK4YW61o9HggnTLutmY6LwGK4AtUGkabRNWCDy7y1qajMO07B5LEG6TSKFAp-U5MItOmO0F4Km/s320/mysql2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Than this window will open. Here create a new schema called spark and inside of it, create a table called User with id, username and email like shown bellow(the record there will not appear yet, be patient :)&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhunEbjtH9IVe_Ic6ZmKhyphenhyphenfkr5idEVSb1nq9MRMOV5K9gYa4KvK2XlxOeipsvYkD_l67MeXdR6UtRmJVhqnSXhOBKfmhwnD7XcqdTRyqZXhGELXd7vwDTMuJTsq_cj7MWByAydgejutAul/s1600/mysql3.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;313&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhunEbjtH9IVe_Ic6ZmKhyphenhyphenfkr5idEVSb1nq9MRMOV5K9gYa4KvK2XlxOeipsvYkD_l67MeXdR6UtRmJVhqnSXhOBKfmhwnD7XcqdTRyqZXhGELXd7vwDTMuJTsq_cj7MWByAydgejutAul/s320/mysql3.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;clear: both; text-align: left;&quot;&gt;
Lets play with now!&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Ok, now we are fine. &lt;span style=&quot;background-color: orange;&quot;&gt;Note that we have not written much code&lt;/span&gt;. Most of the work was infrastructure. Real code there is almost nothing. Ormlite abstracts table creation, manipulation and so on. Spark defines urls and http actions. Ok, if everything went well, you should be able to run the Server.class as Java Application. You should see the following statement in the console(&lt;span style=&quot;background-color: orange;&quot;&gt;which means your server is running well and the service is listening to it on port 4567&lt;/span&gt;):&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;[main] INFO com.j256.ormlite.table.TableUtils - creating table &#39;users&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;[main] INFO com.j256.ormlite.table.TableUtils - executed create table statement changed 0 rows: CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER AUTO_INCREMENT , `username` VARCHAR(255) , `email` VARCHAR(255) , PRIMARY KEY (`id`) ) ENGINE=InnoDB&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;[Thread-0] INFO spark.webserver.SparkServer - == Spark has ignited ...&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;[Thread-0] INFO spark.webserver.SparkServer - &amp;gt;&amp;gt; Listening on 0.0.0.0:4567&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;[Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.0.2.v20130417&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;[Thread-0] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@2695c365{HTTP/1.1}{0.0.0.0:4567}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;clear: both;&quot;&gt;
Testing the service&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
Well, now we should be able to test our services. Lets POST something using postman.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWL8ttv2jzDUVJBWYRa4qW3ggJIACk3cVaN05AQx0zTXGrSgoy0tKLJxwwih9UrVAk4J1ml8eYXyRe6SA-hArmdq1XiYVlUVzVNYm4fQgXPdeNtwNZcgNBkI84HOZFVzGpPxzmbJNCcyme/s1600/postman1.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;137&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWL8ttv2jzDUVJBWYRa4qW3ggJIACk3cVaN05AQx0zTXGrSgoy0tKLJxwwih9UrVAk4J1ml8eYXyRe6SA-hArmdq1XiYVlUVzVNYm4fQgXPdeNtwNZcgNBkI84HOZFVzGpPxzmbJNCcyme/s320/postman1.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
After that, try to execute a GET to retrieve the previous inserted data.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeqTIOeg819Tm7nKU_p03xhoh9XhPDMBuLqP4Dnm03GmUPLKaPZNhaWX3nLO40S70OoM0917-heQMplj8Sr2v6Xoa1TIvVnTdQPSaiLzSOa4d8SF41NcG7VHTa2xaXudJdUkxQ8WqocCAC/s1600/postman2.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;137&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeqTIOeg819Tm7nKU_p03xhoh9XhPDMBuLqP4Dnm03GmUPLKaPZNhaWX3nLO40S70OoM0917-heQMplj8Sr2v6Xoa1TIvVnTdQPSaiLzSOa4d8SF41NcG7VHTa2xaXudJdUkxQ8WqocCAC/s320/postman2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
You could also check the data in the database by running this command line:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;b&gt;SELECT * FROM spark.users;&lt;/b&gt; You should see the record now:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9rqIlUdBoQHBV8BtXwLYPU5g4pYjQbcAXEX7RkwV4iiDgeJgy4TnhdjlVk97_Y9pbQERJVfFlfZkWciCBUZImVP80ejEyq9TSkJzkCYmbVK0f4fCBh3TRrjU3va9sOXUEcySg08jMNs_H/s1600/postman3.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;247&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9rqIlUdBoQHBV8BtXwLYPU5g4pYjQbcAXEX7RkwV4iiDgeJgy4TnhdjlVk97_Y9pbQERJVfFlfZkWciCBUZImVP80ejEyq9TSkJzkCYmbVK0f4fCBh3TRrjU3va9sOXUEcySg08jMNs_H/s320/postman3.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
That&#39;s all! hope you like it! :)&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/8486057442077081527'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/8486057442077081527'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2016/01/how-to-create-rest-web-services-in-pure.html' title='How to create REST web services in pure java as easy as with node.js - This is so beautiful!!! '/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpyNCglLPN2EKtNEwyRpj0f-7r9eD5MjBtVQrkti__b3W2w8LX7PG5Bf2Gr-LiRfp4NJKxzcFC9-mOcmXTfcIk1hBSfU0m55lzbThEeVtKGm-cvMr7yqra5GtjWq-BZDTW6bDz5UWW9O9Q/s72-c/eclipse1.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-3192920839831524459</id><published>2015-08-20T16:55:00.002-07:00</published><updated>2020-10-12T12:33:51.086-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="android"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="pagseguro"/><title type='text'>How to run/install pagseguro on android without thirdpart apps</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show to you, &lt;span style=&quot;background-color: orange;&quot;&gt;how to run &lt;b&gt;PagSeguro &lt;/b&gt;on &lt;b&gt;Android&lt;/b&gt;&lt;/span&gt;&lt;b&gt; &lt;/b&gt;Apps without any third part app from PagSeguro.&lt;br /&gt;
&lt;br /&gt;
Everybody knows how important it is to offer payment methods in our apps. PagSeguro is a very common payment method in Brazil, but very tricky to get it run correctly on android apps. In fact almost every payment method is very annoying to implement! :)&lt;br /&gt;
&lt;br /&gt;
For this reason I decided to implement my own &lt;span style=&quot;background-color: orange;&quot;&gt;&lt;b&gt;PagSeguro &lt;/b&gt;&lt;/span&gt;solution and share a &lt;span style=&quot;background-color: orange;&quot;&gt;&lt;b&gt;complete sample&lt;/b&gt;&lt;/span&gt; with the world.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
First Step - Get a PagSeguro Account&lt;/h3&gt;
To do so, register yourself on &lt;a href=&quot;https://pagseguro.uol.com.br/&quot; target=&quot;_blank&quot;&gt;pagseguro&lt;/a&gt;, log in and go to: &lt;i&gt;&lt;b&gt;Vender &amp;gt; Ferramentas para desenvolvedores &amp;gt; Sandbox&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkthEvDWMvy69K46tc_Sr2R-QbAq4p6JL-oeqxmTKN72x_3pWgdiU9Rjcix30mgR951HBgCPPYRIvimu0nwK7djDc6hQI41aNKH4IWmXN3Jw5VvgXSJDkOg3Rv6yi_9vvIxM0MjGY00_yG/s1600/pagseguroSandbox.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;144&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkthEvDWMvy69K46tc_Sr2R-QbAq4p6JL-oeqxmTKN72x_3pWgdiU9Rjcix30mgR951HBgCPPYRIvimu0nwK7djDc6hQI41aNKH4IWmXN3Jw5VvgXSJDkOg3Rv6yi_9vvIxM0MjGY00_yG/s320/pagseguroSandbox.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
There you&#39;ll find a automatic created test buyer (comprador de teste) and a test seller (vendedor de teste) like in the images bellow. kepp it open, then you&#39;ll need those values in the app we will see here.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-yQOyjh5zNI0BnUBJmHZqTgwVqQH_CMHytx07L7xtXj3Mwq0KxbZn7RlGkpYhs-vC5LujlUn3Sg-zDTE0VqrTyupVP8hJlFKPjH8VTl4Rjnpb02G4jFlRhYZ6Go5akbSzmw6PCH5MoZPW/s1600/pagseguroComprador.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;299&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-yQOyjh5zNI0BnUBJmHZqTgwVqQH_CMHytx07L7xtXj3Mwq0KxbZn7RlGkpYhs-vC5LujlUn3Sg-zDTE0VqrTyupVP8hJlFKPjH8VTl4Rjnpb02G4jFlRhYZ6Go5akbSzmw6PCH5MoZPW/s320/pagseguroComprador.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEin3885bYz_aDUb2qXxyADC9chRDK7QJTIBhqiqbbiCOQn-gA4xrfDtLWMq9nwa3AzmnpSowNgA3cueAD2w_oWfaZhb1nYNyCDcDQu-UhiqOoMGMUUua55yax9arVgp1onwKxwjkh0svtXs/s1600/pagseguroVendedor.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;271&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEin3885bYz_aDUb2qXxyADC9chRDK7QJTIBhqiqbbiCOQn-gA4xrfDtLWMq9nwa3AzmnpSowNgA3cueAD2w_oWfaZhb1nYNyCDcDQu-UhiqOoMGMUUua55yax9arVgp1onwKxwjkh0svtXs/s320/pagseguroVendedor.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Second Step - Fork or just download the sample from Github&lt;/h3&gt;
The sample was developed using android studio and tested on my pagseguro sandbox account. It is available from here: &lt;a href=&quot;https://github.com/treslines/pagseguro_android&quot; target=&quot;_blank&quot;&gt;https://github.com/treslines/pagseguro_android&lt;/a&gt;. The image bellow shows some transactions i made using my sandbox account:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiphRFGtEvKaGkz-KWNQ7TdR75xRzP6dKl-NKc6GXaUmFU6ezwE0TEvsnwffrQS9htio08zEMZLzKJl_DYkBWAgrYjfjcw0mGXlQPX8Q1SYENDRHcdQCYlSDzZLucQgsY0ks5Vv4q5tUTFi/s1600/pagseguroPagamento.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;157&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiphRFGtEvKaGkz-KWNQ7TdR75xRzP6dKl-NKc6GXaUmFU6ezwE0TEvsnwffrQS9htio08zEMZLzKJl_DYkBWAgrYjfjcw0mGXlQPX8Q1SYENDRHcdQCYlSDzZLucQgsY0ks5Vv4q5tUTFi/s320/pagseguroPagamento.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h3&gt;
What you&#39;ll get?&lt;/h3&gt;
The pictures bellow show how the app looks like. It addresses already navigations issues e user notifications.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhz8nF2CGYdQVxZutHihAJJvYO9bdWRLXztFV0FueSGkY4mizTfZpsmtzalY6RL7Pk4HOIPuv2hhA17-ts5wi1UtfA4ZCThvdOzoe-nJclof51CpI0uMyE0bG7jsBaT4sGDVpyGYR2Imgl/s1600/Screenshot_2015-08-20-07-51-38.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhz8nF2CGYdQVxZutHihAJJvYO9bdWRLXztFV0FueSGkY4mizTfZpsmtzalY6RL7Pk4HOIPuv2hhA17-ts5wi1UtfA4ZCThvdOzoe-nJclof51CpI0uMyE0bG7jsBaT4sGDVpyGYR2Imgl/s320/Screenshot_2015-08-20-07-51-38.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqrSmrcIZGb_64TzXnMqN-GQ21rO5f641LkwkfWesqzi-QRE1I91NXG6CVC_Vvn8Ih0-2GuRrROkUDQIBFmvNAO4FXbuNCGuOyXkGIctYm9JOO-YrO0ODOp80_spcL7bP670IBtYm1gaXw/s1600/Screenshot_2015-08-20-07-51-46.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqrSmrcIZGb_64TzXnMqN-GQ21rO5f641LkwkfWesqzi-QRE1I91NXG6CVC_Vvn8Ih0-2GuRrROkUDQIBFmvNAO4FXbuNCGuOyXkGIctYm9JOO-YrO0ODOp80_spcL7bP670IBtYm1gaXw/s320/Screenshot_2015-08-20-07-51-46.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2_5V2JqtXugz-cbEuO-IVfE80okxH2XUOlkjyrj8WIfo-6GV8z9dTT0TLJoU3C2-ZYsOwgf1Rb3ceqUe79ZWVyd1p908x3F-GINOgl5olE3gIrlTpyWoYACqmjqBA6_ibTHEPu8znSjHo/s1600/Screenshot_2015-08-20-07-51-52.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2_5V2JqtXugz-cbEuO-IVfE80okxH2XUOlkjyrj8WIfo-6GV8z9dTT0TLJoU3C2-ZYsOwgf1Rb3ceqUe79ZWVyd1p908x3F-GINOgl5olE3gIrlTpyWoYACqmjqBA6_ibTHEPu8znSjHo/s320/Screenshot_2015-08-20-07-51-52.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXGDYgv1-hwM96jXbNN4GkPTJNq-fsVmQ_76xD4f8hyphenhypheniakU1Vi6afVV43BfkwsrRhX5lRjXiVvXYAOLdIwgFSmlige3vwHOHcWDv9LsQJkoZhwRAzjC_qJ88VL5x6JTns4s1EUIVoSVLqR/s1600/Screenshot_2015-08-20-07-52-06.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXGDYgv1-hwM96jXbNN4GkPTJNq-fsVmQ_76xD4f8hyphenhypheniakU1Vi6afVV43BfkwsrRhX5lRjXiVvXYAOLdIwgFSmlige3vwHOHcWDv9LsQJkoZhwRAzjC_qJ88VL5x6JTns4s1EUIVoSVLqR/s320/Screenshot_2015-08-20-07-52-06.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEN_sOii3r3e9Yz-mMR8qtPCLzWXZUCF-w86mdDkhLW48LB55dNhtL0ixOO_SpJvvR5TKO6GmqGBmdEjsgUO04Dds1_1fwrusAxowkEXFEtj_E-j8yVTlpLPBn0JHH5jDxjeHjRGpMqFPk/s1600/Screenshot_2015-08-20-07-52-36.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEN_sOii3r3e9Yz-mMR8qtPCLzWXZUCF-w86mdDkhLW48LB55dNhtL0ixOO_SpJvvR5TKO6GmqGBmdEjsgUO04Dds1_1fwrusAxowkEXFEtj_E-j8yVTlpLPBn0JHH5jDxjeHjRGpMqFPk/s320/Screenshot_2015-08-20-07-52-36.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-6QQQajJox7GeMmBZ1k2-JEzJiDDOb2bn_7gM618AqmeydGRYbtpYhL2YkiPKSKfTdGSDH_sGqsnr7OXdMyKQA7_UsBinTjq7MJXi5b_Pd1HYahf7r5FQdwYai0XuLQWrO_DC4CXU2LAj/s1600/Screenshot_2015-08-20-13-01-12.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-6QQQajJox7GeMmBZ1k2-JEzJiDDOb2bn_7gM618AqmeydGRYbtpYhL2YkiPKSKfTdGSDH_sGqsnr7OXdMyKQA7_UsBinTjq7MJXi5b_Pd1HYahf7r5FQdwYai0XuLQWrO_DC4CXU2LAj/s320/Screenshot_2015-08-20-13-01-12.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDpUqyIjbkWcLb95BLW4AU5xL7ukH9JHBoMsuS7qmByPgFOs5TjnSMo6mWIrAtyi1or2widB-F97egs1Ce8qBaX7PNThU_Ur0MvitmJLeHQMmPygedX3OIVyilsE1hZHz_vLKZdJ3nOlDf/s1600/Screenshot_2015-08-20-13-01-45.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDpUqyIjbkWcLb95BLW4AU5xL7ukH9JHBoMsuS7qmByPgFOs5TjnSMo6mWIrAtyi1or2widB-F97egs1Ce8qBaX7PNThU_Ur0MvitmJLeHQMmPygedX3OIVyilsE1hZHz_vLKZdJ3nOlDf/s320/Screenshot_2015-08-20-13-01-45.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoi1TJIE7xKFxxSqufKDCPBUmPTsFns405xxklyhKrw7W9uJkinQJO_H-UHLoD1R4EQf8CVf0Jo-jqJhEgA_gwXMVVkaWMFfOEBNPZzCJaq95fqjMgbI_LxYycpBC8akBBU0w2pdjfclYz/s1600/Screenshot_2015-08-20-13-02-13.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoi1TJIE7xKFxxSqufKDCPBUmPTsFns405xxklyhKrw7W9uJkinQJO_H-UHLoD1R4EQf8CVf0Jo-jqJhEgA_gwXMVVkaWMFfOEBNPZzCJaq95fqjMgbI_LxYycpBC8akBBU0w2pdjfclYz/s320/Screenshot_2015-08-20-13-02-13.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn2hrB8N83N_VEaRyGhak0N1Vfg26dVzybMry5VBkND6WRxfoJamW_9vwiK43It6b9RrdDnhvPZ3u_TfEAGOy4kdgImfijJ0z_sJBvcuYFwD44FFLh7MuSGhyphenhyphenJjL3pCOg9EyqXU9VSRF5s/s1600/Screenshot_2015-08-20-13-02-25.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn2hrB8N83N_VEaRyGhak0N1Vfg26dVzybMry5VBkND6WRxfoJamW_9vwiK43It6b9RrdDnhvPZ3u_TfEAGOy4kdgImfijJ0z_sJBvcuYFwD44FFLh7MuSGhyphenhyphenJjL3pCOg9EyqXU9VSRF5s/s320/Screenshot_2015-08-20-13-02-25.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdRi_5tMuzoGCNoK4OdZJwLAkXdmsbvyRHUxsOj5LP13rcWDUFch5nymSGo6adWBmiKSJuzEARMfHLuqR0bCTM9rVE_q5RK-MQMNTMHOxSu-oUmLaEnTPm_SWlMMwe8-Cre_vtUybrleZc/s1600/Screenshot_2015-08-20-13-02-31.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdRi_5tMuzoGCNoK4OdZJwLAkXdmsbvyRHUxsOj5LP13rcWDUFch5nymSGo6adWBmiKSJuzEARMfHLuqR0bCTM9rVE_q5RK-MQMNTMHOxSu-oUmLaEnTPm_SWlMMwe8-Cre_vtUybrleZc/s320/Screenshot_2015-08-20-13-02-31.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB8_oJllM4ftIIpVDoIY76wCoG8D7Ftpo_MqZQpCmFehGULrCU40qc-PyVFJMc7BkmYggRBKqaxcpkaOGiKgv7PLKxoa1yfV9tRDhr-Mq_SQae6ZzhYztHVa0yFQlFuo9yJefLEfmvC_PR/s1600/Screenshot_2015-08-20-13-02-36.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB8_oJllM4ftIIpVDoIY76wCoG8D7Ftpo_MqZQpCmFehGULrCU40qc-PyVFJMc7BkmYggRBKqaxcpkaOGiKgv7PLKxoa1yfV9tRDhr-Mq_SQae6ZzhYztHVa0yFQlFuo9yJefLEfmvC_PR/s320/Screenshot_2015-08-20-13-02-36.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQ0TJtIax3H6fdAOiKN74oFvt1R4Ugbykq8Z6KAC6ueIfg5nefTKXiWOyhDN4_qec7wFxPu066H5IUtXFxqE53bO3fqZdeq2wSV1cUS4iqqquUg2mSBLWyRMc3ks6wNox55mKYVv-3j-z3/s1600/Screenshot_2015-08-20-13-02-57.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQ0TJtIax3H6fdAOiKN74oFvt1R4Ugbykq8Z6KAC6ueIfg5nefTKXiWOyhDN4_qec7wFxPu066H5IUtXFxqE53bO3fqZdeq2wSV1cUS4iqqquUg2mSBLWyRMc3ks6wNox55mKYVv-3j-z3/s320/Screenshot_2015-08-20-13-02-57.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiz8IcXVS5Ihp3Mi_9d2gxnR9WEEoK7kOqKgfXjTGMoCq8C8HaqAqZkMOQa3LASVWSPIO401zaEWiMeK4N0imM3vL8vORkBZF7E8HtuVUKLmG_A69rnmvIAaZyCxkEDw53QZtPUOISKBcdO/s1600/Screenshot_2015-08-20-13-03-08.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiz8IcXVS5Ihp3Mi_9d2gxnR9WEEoK7kOqKgfXjTGMoCq8C8HaqAqZkMOQa3LASVWSPIO401zaEWiMeK4N0imM3vL8vORkBZF7E8HtuVUKLmG_A69rnmvIAaZyCxkEDw53QZtPUOISKBcdO/s320/Screenshot_2015-08-20-13-03-08.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsiSOjTfOh2ZxzLfKYAV-FLjz5tM7Ir6AQ7jm-ZyBGNI3vjDN28FZ09NbkalOYsH2lUOgW9COpcLTt6L2ZzxKs7mpODEqOLwrnGN0enM5jXzzMcYmIpAVChJD9-7o1AtCu0TzlhYW5hOtO/s1600/Screenshot_2015-08-20-13-03-32.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsiSOjTfOh2ZxzLfKYAV-FLjz5tM7Ir6AQ7jm-ZyBGNI3vjDN28FZ09NbkalOYsH2lUOgW9COpcLTt6L2ZzxKs7mpODEqOLwrnGN0enM5jXzzMcYmIpAVChJD9-7o1AtCu0TzlhYW5hOtO/s320/Screenshot_2015-08-20-13-03-32.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMuNoyAtEb3p-to58bGPbzG7B18R_jEt9i0F-vJ_1WPhWcYzwfkXf0BDicTKr7m0iZ-TIGr8P2f-rGsPebn3VocQFgM6k6WF0y7cu5UzTGXQYbv28GrqO3Xg7EmV75ho2zlT8rqGjmy77T/s1600/Screenshot_2015-08-20-13-03-48.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMuNoyAtEb3p-to58bGPbzG7B18R_jEt9i0F-vJ_1WPhWcYzwfkXf0BDicTKr7m0iZ-TIGr8P2f-rGsPebn3VocQFgM6k6WF0y7cu5UzTGXQYbv28GrqO3Xg7EmV75ho2zlT8rqGjmy77T/s320/Screenshot_2015-08-20-13-03-48.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h3&gt;
Go download it!&lt;/h3&gt;
Go now to github:&amp;nbsp;&lt;a href=&quot;https://github.com/treslines/pagseguro_android&quot; target=&quot;_blank&quot;&gt;https://github.com/treslines/pagseguro_android&lt;/a&gt;. download it and test it. Any feedback or contribution to make it better is welcome!&lt;br /&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/3192920839831524459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/3192920839831524459'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2015/08/how-to-runinstall-pagseguro-on-android.html' title='How to run/install pagseguro on android without thirdpart apps'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkthEvDWMvy69K46tc_Sr2R-QbAq4p6JL-oeqxmTKN72x_3pWgdiU9Rjcix30mgR951HBgCPPYRIvimu0nwK7djDc6hQI41aNKH4IWmXN3Jw5VvgXSJDkOg3Rv6yi_9vvIxM0MjGY00_yG/s72-c/pagseguroSandbox.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-937922716024429164</id><published>2015-08-13T05:35:00.002-07:00</published><updated>2020-10-12T12:27:38.666-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Almanac"/><category scheme="http://www.blogger.com/atom/ns#" term="android"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Android AlertDialog utilities, redirect to google play store, check if third part app is istalled</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna share some utility methods i use very often while developing. Creating a popup dialog, a confirm dialog, ok-cancel-dialog, check if a third part app is installed, redirect to google play store if some app is missing and so on are common tasks in our daily business.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;/**
 * Use this class to create alert dialogs on the fly, redirect to google play store and so on...&amp;lt; br / &amp;gt;
 * &amp;lt; br / &amp;gt;Author: Ricardo Ferreira, 8/13/15
 */
public final class AppUtil {

    /**
     * Shows a confirm dialog with a custom message and custom quit button text.
     * If you pass null to btnTxt, &quot;ok&quot; will be shown per default.
     *
     * @param context the apps context This value can&#39;t be null.
     * @param msg     the message to be shown. his value can&#39;t be null or empty.
     * @param btnTxt  the text to be shown in the quit button. For example &quot;close&quot;
     */
    public static void showConfirmDialog(@NonNull final Context context, @NonNull final String msg, final String btnTxt) {
        if (msg.isEmpty()) {
            throw new IllegalArgumentException(&quot;This value can&#39;t be empty!&quot;);
        }
        final AlertDialog dialog = new AlertDialog.Builder(context).setMessage(msg)
                .setPositiveButton((btnTxt == null ? &quot;Ok&quot; : btnTxt), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();
    }

    /**
     * Shows a confirm dialog with a custom message, custom buttons and custom actions.
     *
     * @param ctx                the apps context. This value can&#39;t be null.
     * @param msg                the message to be shown This value can&#39;t be null or empty.
     * @param okBtn              the text to be shown in the ok button. If you pass null, ok will be shown per default
     * @param cancelBtn          the text to be shown in the cancel button. If you pass null, cancel will be shown per default
     * @param okAction           the action to be performed as soon as the user presses the ok button. if you pass null, nothing happens.
     * @param cancelAction       the action to be performed as soon as the user presses the cancel button. if you pass null, nothing happens.
     * @param backPressendAction the action to be performed as soon as the user presses the system back button. if you pass null, nothing happens.
     */
    public static void showOkCancelDialog(@NonNull final Context ctx, @NonNull final String msg, final String okBtn, final String cancelBtn, final AlertAction okAction, final AlertAction cancelAction, final AlertAction backPressendAction) {
        if (msg.isEmpty()) {
            throw new IllegalArgumentException(&quot;This value can&#39;t be empty!&quot;);
        }
        final AlertDialog dialog = new AlertDialog.Builder(ctx).setMessage(msg)
                .setPositiveButton((okBtn == null ? &quot;Ok&quot; : okBtn), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (okAction != null) {
                            okAction.perform();
                        }
                    }
                }).setNegativeButton((cancelBtn == null ? &quot;Cancel&quot; : cancelBtn), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (cancelAction != null) {
                            cancelAction.perform();
                        }
                    }
                }).setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        dialog.dismiss();
                        if (backPressendAction != null) {
                            backPressendAction.perform();
                        }
                    }
                }).create();
        dialog.show();
    }

    public interface AlertAction {
        void perform();
    }

    /**
     * Use this method to check if a third part app is istalled or not
     * @param ctx the app&#39;s context
     * @param packageName the third part app&#39;s package name. something like: com.example.package
     * @return true if the app is installed.
     */
    public static boolean isAppInstalled(final Context ctx, final String packageName) {
        PackageManager pm = ctx.getPackageManager();
        boolean installed = false;
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        return installed;
    }

    /**
     *  Use this method to open the google play store
     * @param activity the app which wants to redirect to the google play store
     * @param googlePlayStoreId the third part app&#39;s package name. something like: com.example.package
     * @param requestCode the request code to be used in the method onActivityForResult in the app which called this method.
     */
    public static void navigateToGooglePlayStore(final Activity activity, final String googlePlayStoreId, final int requestCode) {
        try {
            activity.startActivityForResult(new Intent(Intent.ACTION_VIEW, Uri.parse(&quot;market://details?id=&quot; + googlePlayStoreId)), requestCode);
        } catch (android.content.ActivityNotFoundException anfe) {
            // last chance: try again a second time with a different approach
            activity.startActivityForResult(new Intent(Intent.ACTION_VIEW, Uri.parse(&quot;http://play.google.com/store/apps/details?id=&quot; + googlePlayStoreId)), requestCode);
        }
    }
}
&lt;/pre&gt;
&lt;h3&gt;
Usage Example:&lt;/h3&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;//...&amp;nbsp;&lt;/pre&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;// check if app is istalled
return AppUtil.isAppInstalled(context,packageName);
//...&amp;nbsp;&lt;/pre&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;// redirect to google play store
AppUtil.navigateToGooglePlayStore(activity ,googlePlayStoreId, requestCode);
//...&amp;nbsp;&lt;/pre&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;// create a Ok-Cancel-Dialog
final String msg = &quot;The app will no work without PagSeguro.&quot;;
        AppUtil.showOkCancelDialog(context, msg, &quot;Install&quot;, &quot;Cancel&quot;,
        new AppUtil.AlertAction() {
            @Override
            public void perform() {
                navigateToGooglePlayStore(PAG_SEGURO_PACKAGE_NAME);
            }
        }, new AppUtil.AlertAction() {
            @Override
            public void perform() {
                finish();
            }
        }, new AppUtil.AlertAction() {
            @Override
            public void perform() {
                finish();
            }
        });
//... code omitted
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! hope you like it! :)&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/937922716024429164'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/937922716024429164'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2015/08/android-alertdialog-utilities-redirect.html' title='Android AlertDialog utilities, redirect to google play store, check if third part app is istalled'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-8649622886839740897</id><published>2015-08-11T05:08:00.002-07:00</published><updated>2020-10-12T12:24:55.113-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Router"/><title type='text'>Router Design Pattern - Route messages, Objects or whatever you  want!</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show you the router pattern i wrote myself in action. The Router design pattern is a very useful 
programming design pattern whenever &lt;span style=&quot;background-color: orange;&quot;&gt;you need to be able to send objects or messages between instances over the application&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it. &lt;span style=&quot;background-color: yellow;&quot;&gt;If you use it anywhere, please come back and give me a feedback. It would be nice to have more real use cases.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Router Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicBT4Q4DLF_IlrpYqEh2aOwoJYYNdd6c0HRjSnRwF22J9NVNkSLOQ-7e19XW8JLxazXE-767c9znD1WuwlybAX9DLPdRI1Q-e_J5JYvngxgSd7A1jv9D9zfW-qQhRy9d1NAc7bbPDcKhKc/s1600/routerClass.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;134&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicBT4Q4DLF_IlrpYqEh2aOwoJYYNdd6c0HRjSnRwF22J9NVNkSLOQ-7e19XW8JLxazXE-767c9znD1WuwlybAX9DLPdRI1Q-e_J5JYvngxgSd7A1jv9D9zfW-qQhRy9d1NAc7bbPDcKhKc/s320/routerClass.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s1600/flyweight.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could create clients that listen to server responses. The servers are lazy instantiated and are created on demand.

&lt;br /&gt;
&lt;h3&gt;
Response and Request Interfaces&lt;/h3&gt;
Those interfaces defines a contract to be used to design which type the client will use to send requests and receive responses. Let&#39;s take a closer look to it:
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public interface Response &amp;lt; T &amp;gt;  {
  T getResponse();
  void setResponse(T value);
}

public interface Request &amp;lt; T &amp;gt;  {
  T getRequest();
  void setRequest(T value);
}

&lt;/pre&gt;
&lt;h3&gt;
Client and Server Interfaces&lt;/h3&gt;
Those interfaces defines a contract to be used while implementing concrete clients and servers. It uses either the Response or the Request depending on the what you are implementing:
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public interface Client {
   &amp;lt; T extends Response &amp;lt; ? &amp;gt;  &amp;gt;  void onServerResponse(T response);
}

public interface Server {
  &amp;lt; T extends Request &amp;lt; ? &amp;gt; &amp;gt; void onClientRequest(T request, Client client);
}

&lt;/pre&gt;
&lt;h3&gt;
Routable interface and Router class&lt;/h3&gt;
The routable defines the contract for the router. The router itself is designed as a singleton and can be accessed and used everywhere in the application sending and receiving messages or objects. In this implementation the servers are lazy implemented and created on demand. For sure you may adapt it to your needs. Feel free to do it and give me feedback of the usage of it in your applications. 
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public interface Routable {
  public  &amp;lt; T extends Client &amp;gt;  void registerClient(T clientImpl);
  public void registerServer(Class &amp;lt; ? extends Server &amp;gt;  serverImpl);
  public  &amp;lt; T extends Request &amp;lt; ? &amp;gt;  &amp;gt;  void routeClientToServer(Class &amp;lt; ? extends Client &amp;gt;  clientImpl, Class &amp;lt; ? extends Server &amp;gt;  serverImpl, T request);
  public void removeClient(Class &amp;lt; ? &amp;gt;  serverClass);
  public void removeAllClients();
  public void removeServer(Class &amp;lt; ? &amp;gt;  clientClass);
  public void removeAllServers();
  public boolean isRegistered(Class &amp;lt; ? &amp;gt;  clazz);
}


public class Router implements Routable {

  private Map &amp;lt; String, Client &amp;gt;  clients = new HashMap &amp;lt; String, Client &amp;gt; ();
  // using sets to avoid duplicates
  public Set &amp;lt; Class &amp;lt; ? extends Client &amp;gt;  &amp;gt;  clientSet = new HashSet &amp;lt; Class &amp;lt; ? extends Client &amp;gt;  &amp;gt; ();
  public Set &amp;lt; Class &amp;lt; ? extends Server &amp;gt;  &amp;gt;  serverSet = new HashSet &amp;lt; Class &amp;lt; ? extends Server &amp;gt;  &amp;gt; ();
  private static final Router ROUTER = new Router();

  private Router() {
    // singleton - can be accessed anywhere in the application
  }

  public static Router turnOn() {
    return ROUTER;
  }

  public  &amp;lt; T extends Request &amp;lt; ? &amp;gt;  &amp;gt;  void routeClientToServer(Class &amp;lt; ? extends Client &amp;gt;  clientImpl, Class &amp;lt; ? extends Server &amp;gt;  serverImpl, T request) {
    doNotAllowNullValue(clientImpl);
    doNotAllowNullValue(serverImpl);
    doNotAllowNullValue(request);
    doNotAllowUnregisteredNullValue(isRegistered(clientImpl));
    // just to ensure that the server implementation exits already
    doNotAllowUnregisteredNullValue(isRegistered(serverImpl));
    // as we now know that the server implementation exists,
    // we just create a lazy instance over reflection on demand
    try {
      serverImpl.newInstance().onClientRequest(request, clients.get(clientImpl.getName()));
    } catch (InstantiationException e) {
      // we shall never run into this situation, except if the user does NOT define
      // a default constructor in any of the concrete implementation of Server as per
      // convention.
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
  }

  public void removeServer(Class &amp;lt; ? &amp;gt;  serverClass) {
    serverSet.remove(serverClass);
  }

  public void removeAllServers() {
    serverSet.clear();
  }

  public void removeClient(Class &amp;lt; ? &amp;gt;  clientclass) {
    clients.remove(clientclass.getName());
    clientSet.remove(clientclass);
  }

  public void removeAllClients() {
    clients.clear();
  }

  public boolean isRegistered(Class &amp;lt; ? &amp;gt;  clazz) {
    boolean result = false;
    boolean searchBreak = false;
    Iterator &amp;lt; Class &amp;lt; ? extends Client &amp;gt;  &amp;gt;  iterator = clientSet.iterator();
    while (iterator.hasNext()) {
      Class &amp;lt; ? extends Client &amp;gt;  next = iterator.next();
      // note: we can&#39;t use equalsIgnoreCase here
      if (next.getName().equals(clazz.getName())) {
        result = true;
        searchBreak = true;
        break;
      }
    }
    if (!searchBreak) {
      Iterator &amp;lt; Class &amp;lt; ? extends Server &amp;gt;  &amp;gt;  it = serverSet.iterator();
      while (it.hasNext()) {
        Class &amp;lt; ? extends Server &amp;gt;  next = it.next();
        // note: we can&#39;t use equalsIgnoreCase here
        if (next.getName().equals(clazz.getName())) {
          result = true;
          searchBreak = true;
          break;
        }
      }
    }
    return result;
  }

  public  &amp;lt; T extends Client &amp;gt;  void registerClient(T clientImpl) {
    doNotAllowNullValue(clientImpl);
    clientSet.add((Class &amp;lt; ? extends Client &amp;gt; ) clientImpl.getClass());
    clients.put(clientImpl.getClass().getName(), clientImpl);
  }

  public void registerServer(Class &amp;lt; ? extends Server &amp;gt;  serverImpl) {
    doNotAllowNullValue(serverImpl);
    serverSet.add(serverImpl);
  }

  private void doNotAllowNullValue(Object toCheck) {
    if (toCheck == null) {
      final String msg = &quot;You can&#39;t pass null to this method!&quot;;
      throw new NullPointerException(msg);
    }
  }

  private void doNotAllowUnregisteredNullValue(boolean isRegistered) {
    if (!isRegistered) {
      final String msg = &quot;Either the client or the server was not registered in this router. Register it first!&quot;;
      throw new IllegalArgumentException(msg);
    }
  }
}
&lt;/pre&gt;
&lt;h3&gt;
Sample Implementation and Test&lt;/h3&gt;
Now let&#39;s see how a real implementation could looks like and how it works in practise. First of all we are gonna define some responses and requests. Then we will create the clients and servers. Finally we will test it, by running a junit test to show it in action.
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;//SAMPLE CLIENT RESPONSE
public class ClientResponse implements Response &amp;lt; String &amp;gt;  {
  private String response;
  public String getResponse() {return response;}
  public void setResponse(String value) {response = value;}
}

//SAMPLE SERVER REQUEST
public class ServerRequest implements Request &amp;lt; String &amp;gt;  {
  String request;
  public String getRequest() {return request;}
  public void setRequest(String value) {request = value;}
}

// SAMPLE CLIENT IMPL
public class ClientImpl implements Client {
  public  &amp;lt; T extends Response &amp;lt; ? &amp;gt; &amp;gt; void onServerResponse(T response) {
    System.out.println(response.getResponse());
  }
}

//SAMPLE SERVER IMPL
public class ServerImpl implements Server {
  public &amp;lt; T extends Request &amp;lt; ? &amp;gt; &amp;gt;  void onClientRequest(T request, Client client) {
    // handle request and depending on it create response
    ClientResponse clientResponse = new ClientResponse();
    clientResponse.setResponse(&quot;Server is sending a response to client...&quot;);
    // route response back to client immediately or whenever you want
    client.onServerResponse(clientResponse);
  }
}

public class RouterTest {
  @Test
  public void testRouter() {
    Router.turnOn().registerClient(new ClientImpl());
    // servers would be only referenced and lazy instantiated later
    Router.turnOn().registerServer(ServerImpl.class);
    System.out.println(&quot;Client is sending a request to server...&quot;);
    ServerRequest request = new ServerRequest();
    request.setRequest(&quot;Client is sending a request to server...&quot;);
    Router.turnOn().routeClientToServer(ClientImpl.class, ServerImpl.class, request);
  }
}

&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! hope you like it! :)&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/8649622886839740897'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/8649622886839740897'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2015/08/router-design-pattern-route-messages.html' title='Router Design Pattern - Route messages, Objects or whatever you  want!'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicBT4Q4DLF_IlrpYqEh2aOwoJYYNdd6c0HRjSnRwF22J9NVNkSLOQ-7e19XW8JLxazXE-767c9znD1WuwlybAX9DLPdRI1Q-e_J5JYvngxgSd7A1jv9D9zfW-qQhRy9d1NAc7bbPDcKhKc/s72-c/routerClass.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-2157234677852570906</id><published>2014-09-24T05:45:00.006-07:00</published><updated>2020-10-12T12:32:45.571-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="android"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="junit"/><category scheme="http://www.blogger.com/atom/ns#" term="test"/><title type='text'>How to run junit tests inside the android project</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show you how to create and run junit tests inside your android project without creating a separated test project. With those tests we will rapidly be able to automate and test the app&#39;s logic and some simple UI behaviors. The example below is very straightforward and much more intuitive than other approaches i saw out there.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Defining the TestInstrumentation&amp;nbsp;&lt;/h3&gt;
First of all, define in your manifest file the following entries. &lt;span style=&quot;background-color: orange;&quot;&gt;&lt;b&gt;IMPORTANT&lt;/b&gt;&lt;/span&gt;: While the definition of the test instrumentation will be placed outside your application tag, the test runner must be defined&amp;nbsp; inside your application tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEib2NtGkbbkDhv4j6QoxMv1Hzvcz1EY6a0_qA2beid9Bw3T6UPTD3brUvy08pm7h9V5VYiI4TnbrnupSxjqJtCuPQwNo2T5Y35pZwmZHV9kXBHv-wc_mYqikPNiZUV5pIB4BeW9h7xSKNaS/s1600/A2.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;243&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEib2NtGkbbkDhv4j6QoxMv1Hzvcz1EY6a0_qA2beid9Bw3T6UPTD3brUvy08pm7h9V5VYiI4TnbrnupSxjqJtCuPQwNo2T5Y35pZwmZHV9kXBHv-wc_mYqikPNiZUV5pIB4BeW9h7xSKNaS/s1600/A2.png&quot; width=&quot;320&quot; /&gt;&amp;nbsp;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;&amp;lt; manifest &amp;gt;
...
&amp;lt; instrumentation 
  android:name=&quot;android.test.InstrumentationTestRunner&quot;
  android:targetPackage=&quot;com.treslines.ponto&quot; 
/ &amp;gt;
&amp;lt; / manifest &amp;gt;
&lt;/pre&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;&amp;lt; application &amp;gt;
...
&amp;lt; uses-library android:name=&quot;android.test.runner&quot; / &amp;gt;
...
&amp;lt; / application &amp;gt;
&lt;/pre&gt;
&lt;h3&gt;
Creating the test packages&lt;/h3&gt;
Android works with some conventions while testing. So it is &lt;span style=&quot;background-color: orange;&quot;&gt;&lt;b&gt;extremelly important&lt;/b&gt;&lt;/span&gt; to follow those conventions. Otherwise you&#39;ll get compile or run errors while trying to run it. One convention is that all tests for a specific class must be placed in the same package structure but with a futher sub-folder called test as you can see below. Because i want to test the activities in the package &lt;b&gt;com.treslines.ponto.activity&lt;/b&gt; i must create a test package called &lt;b&gt;com.treslines.ponto.activity&lt;span style=&quot;background-color: orange;&quot;&gt;.test&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS_1GzOG-yBE8gESxCCu13OasDJJ2Lm38K7MER_1PaQZ_JpvuR3Abfh54dwl9VuG2xv2g6Kq68IxCZH0mlxIIZFMc04D7ImsvuGF_nY-MtqChhO-s9OuY62b3JmPovzfyreUsutjsO0HFb/s1600/A1.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;185&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS_1GzOG-yBE8gESxCCu13OasDJJ2Lm38K7MER_1PaQZ_JpvuR3Abfh54dwl9VuG2xv2g6Kq68IxCZH0mlxIIZFMc04D7ImsvuGF_nY-MtqChhO-s9OuY62b3JmPovzfyreUsutjsO0HFb/s1600/A1.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h3&gt;
Creating the test itself&lt;/h3&gt;
That&#39;s the cool part of it. Here you can write your junit tests as usual. Again android&amp;nbsp; gives us some conventions to follow. All test classes must have the same name as the class under test with the &lt;b&gt;suffix &lt;span style=&quot;background-color: orange;&quot;&gt;Test&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;background-color: orange;&quot;&gt; &lt;/span&gt;on it. And all test methods must start with the &lt;b&gt;prefix &lt;span style=&quot;background-color: orange;&quot;&gt;test &lt;/span&gt;&lt;/b&gt;on it. If you follow those conventions everything will work just fine.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;// IMPORTANT: All test cases MUST have a suffix &quot;Test&quot; at the end
//
// THAN:
// Define this in your manifest outside your application tag:
//  &amp;lt; instrumentation 
//    android:name=&quot;android.test.InstrumentationTestRunner&quot;
//    android:targetPackage=&quot;com.treslines.ponto&quot; 
//  / &amp;gt;
//
// AND:
// Define this inside your application tag:
//  &amp;lt; uses-library android:name=&quot;android.test.runner&quot; / &amp;gt;
//
// The activity you want to test will be the &quot;T&quot; type of ActivityInstrumentationTestCase2
public class AlertaActivityTest extends ActivityInstrumentationTestCase2 &amp;lt; AlertaActivity &amp;gt; {

 private AlertaActivity alertaActivity;
 private AlertaController alertaController;

 public AlertaActivityTest() {
  // create a default constructor and pass the activity class
  // you want to test to the super() constructor
  super(AlertaActivity.class);
 }

 @Override
 // here is the place to setup the var types you want to test
 protected void setUp() throws Exception {
  super.setUp();
  
  // because i want to test the UI in the method testAlertasOff()
  // i must set this attribute to true
  setActivityInitialTouchMode(true);

  // init variables
  alertaActivity = getActivity();
  alertaController = alertaActivity.getAlertaController();
 }

 // usually we test some pre-conditions. This method is provided
 // by the test framework and is called after setUp()
 public void testPreconditions() {
  assertNotNull(&quot;alertaActivity is null&quot;, alertaActivity);
  assertNotNull(&quot;alertaController is null&quot;, alertaController);
 }

 // test methods MUST start with the prefix &quot;test&quot;
 public void testVibrarSomAlertas() {
  assertEquals(true, alertaController.getView().getVibrar().isChecked());
  assertEquals(true, alertaController.getView().getSom().isChecked());
  assertEquals(true, alertaController.getView().getAlertas().isChecked());
 }

 // test methods MUST start with the prefix &quot;test&quot;
 public void testAlertasOff() {
  Switch alertas = alertaController.getView().getAlertas();
  // because i want to simulate a click on a view, i must use the TouchUtils
  TouchUtils.clickView(this, alertas);
  // wait a little (1.5sec) because the UI needs its time
  // to change the switch&#39;s state and than check new state of the switches
  new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
    assertEquals(false, alertaController.getView().getVibrar().isChecked());
    assertEquals(false, alertaController.getView().getSom().isChecked());
   }
  }, 1500);
 }
}
&lt;/pre&gt;
&lt;h2&gt;
Running the JUnit tests&lt;/h2&gt;
The only difference while running junit test in android is that you&#39;ll be calling &lt;span style=&quot;background-color: orange;&quot;&gt;&lt;b&gt;Run As &amp;gt; Android JUnit Test&lt;/b&gt;&lt;/span&gt; instead of just JUnit Test like you are used to in java.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgnioj1V3X9UDY_4GeZTU487oDm3aPM_ePFKHBKR_8PdQsXUWxuPXYEqWtz3YwsOwcRCJmZE4t6J6Yck6haEOZXxhPByISy26h8RAmAA2pJPTwC0_mLzjq0ABPa4_v-ciQvKgBpmtrJuur/s1600/A3.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;167&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgnioj1V3X9UDY_4GeZTU487oDm3aPM_ePFKHBKR_8PdQsXUWxuPXYEqWtz3YwsOwcRCJmZE4t6J6Yck6haEOZXxhPByISy26h8RAmAA2pJPTwC0_mLzjq0ABPa4_v-ciQvKgBpmtrJuur/s1600/A3.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it! :)&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/2157234677852570906'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/2157234677852570906'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/09/how-to-run-junit-tests-inside-android.html' title='How to run junit tests inside the android project'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEib2NtGkbbkDhv4j6QoxMv1Hzvcz1EY6a0_qA2beid9Bw3T6UPTD3brUvy08pm7h9V5VYiI4TnbrnupSxjqJtCuPQwNo2T5Y35pZwmZHV9kXBHv-wc_mYqikPNiZUV5pIB4BeW9h7xSKNaS/s72-c/A2.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-5550911413521358191</id><published>2014-09-08T11:15:00.003-07:00</published><updated>2020-10-12T12:37:40.620-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="android"/><category scheme="http://www.blogger.com/atom/ns#" term="Gson"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Json"/><category scheme="http://www.blogger.com/atom/ns#" term="parse"/><title type='text'>how to import/parse or create json/gson as arraylist from strings.xml in android?</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show to you, how we can &lt;span style=&quot;background-color: orange;&quot;&gt;profit from the existing Gson library&lt;/span&gt; while dealing with array lists in the string.xml file. The benefits of it is that can define as many strings.xml entries as we need and import/parse the whole list structures &lt;span style=&quot;background-color: orange;&quot;&gt;almost without any effort&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Gson library&lt;/h3&gt;
download the Gson library from here &lt;a href=&quot;https://code.google.com/p/google-gson/downloads/detail?name=google-gson-2.2.4-release.zip&amp;amp;can=4&amp;amp;q=&quot; target=&quot;_blank&quot;&gt;Gson 2.2.4&lt;/a&gt;. Don&#39;t worry about the &quot;deprecated&quot; tag. It is not really deprecated. Only parts of it acc. to its developer. We can continue using it. Than unzip it and cut/paste the following jar (gson-2.2.4.jar) in the libs folder from your project.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Json sample&lt;/h3&gt;
Open your strings.xml file and copy/paste this line sample entry in it(It is a representation of an array list of Points with x and y coordinates):&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt; &amp;lt; string name=&quot;point_array&quot; &amp;gt; {\&quot;points\&quot;:[{\&quot;x\&quot;: 255,\&quot;y\&quot;: 689},{\&quot;x\&quot;: 199,\&quot;y\&quot;: 658}&amp;lt; / string &amp;gt; &lt;/pre&gt;
&lt;h3&gt;
Defining a Data Container&lt;/h3&gt;
You&#39;ll need a data container to hold the read points. So lets define a structure for it.
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;           
public class PointContainer {
    private List &amp;lt; Point &amp;gt; points;
    public List &amp;lt; Point &amp;gt; getPoints() {return points;}
    public void setPoints(List &amp;lt; Point &amp;gt; points) {this.points = points;}
}
&lt;/pre&gt;
&lt;h3&gt;
Usage of the Gson lib&lt;/h3&gt;
With the Gson lib, import/parse the array into a json structure. &lt;span style=&quot;background-color: orange;&quot;&gt;The benefits of it is that can define as many strings.xml entries as you need and import the whole list structure almost without any effort&lt;/span&gt;.
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;           
    String pointArray = getResources().getString(R.string.point_array);
    PointContainer pointContainer = new Gson().fromJson(pointArray, PointContainer.class);
    List &amp;lt; Point &amp;gt; points = pointContainer.getPoints();
    // do something with the data here...
&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;
Reading List of Lists&lt;/h3&gt;
Here it gets more tricky. Offen we will need to read a list of list from json format into java code. To do so, lets see how it works by writing a simple example.We will do the same way as the code above. the only difference is to note that if we have list of list we will need to create a class that holds the sublists and so on...&lt;br /&gt;
&amp;nbsp;

&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;&amp;lt; string name=&quot;string_points&quot; &amp;gt;{&quot;items&quot;:[{&quot;points&quot;:[{&quot;x&quot;: 644,&quot;y&quot;: 420},{&quot;x&quot;: 644,&quot;y&quot;: 421},{&quot;points&quot;:[{&quot;x&quot;: 752,&quot;y&quot;: 348},{&quot;x&quot;: 752,&quot;y&quot;: 349}]}]} &amp;lt; / string &amp;gt;

public class ItemContainer {
 private List &amp;lt; Item &amp;gt; items ;
 public List &amp;lt; Item &amp;gt; getItems() {
  return items 
 }
 public void setItems(List &amp;lt; Item &amp;gt; items ) {
  this.items  = items 
 }
}

public class Item {
 private List &amp;lt; Point &amp;gt; points;
 public List &amp;lt; Point &amp;gt; getPoints() {
  return points;
 }
 public void setPoints(List &amp;lt; Point &amp;gt; points) {
  this.points = points;
 }
}

String string = cxt.getResources().getString(R.string.string_points);
List &amp;lt; ItemContainer &amp;gt; l =  new ArrayList &amp;lt; ItemContainer &amp;gt;();
Gson gson = new Gson();
l.add(gson.fromJson(string, ItemContainer.class));
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/5550911413521358191'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/5550911413521358191'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/09/how-to-importparse-or-create-jsongson.html' title='how to import/parse or create json/gson as arraylist from strings.xml in android?'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-6781499314583888830</id><published>2014-08-27T04:57:00.002-07:00</published><updated>2020-10-12T12:37:33.116-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="android"/><category scheme="http://www.blogger.com/atom/ns#" term="AndroidStudio"/><category scheme="http://www.blogger.com/atom/ns#" term="appearance"/><category scheme="http://www.blogger.com/atom/ns#" term="Eclipse"/><category scheme="http://www.blogger.com/atom/ns#" term="IDE"/><category scheme="http://www.blogger.com/atom/ns#" term="IntelliJ"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="shortcuts"/><title type='text'>How to make intellij works like eclipse incl. appearance and shortcuts in 1 min.</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Since a few days I&#39;m thinking about to move to AndroidStudio&#39;s IntelliJ IDEA. One of biggest problem and resistance i had, was the fact that IntelliJ IDEA has totally different shortcuts than eclipse. We don&#39;t want to learn or invent the wheel again for the same thing, right? For this reason, i&#39;ll show to you, &lt;span style=&quot;background-color: orange;&quot;&gt;how to use eclipse&#39;s shortcuts in IntelliJ&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
Another thing i don&#39;t like very much, is the color schema. I&#39;ve been programming with eclipse since years &lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span class=&quot;hps&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;although I like&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;black look and&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;feel&lt;/span&gt;&lt;span class=&quot;hps&quot;&gt;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;I still prefer&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;the traditional&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;white appearance&lt;/span&gt;&lt;span&gt;. So i will also show, how we can &lt;span style=&quot;background-color: orange;&quot;&gt;override the color schema&lt;/span&gt; of IntelliJ &lt;span style=&quot;background-color: orange;&quot;&gt;with a few clicks&lt;/span&gt;.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span&gt;Changing the shortcut list&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span&gt;Once you´ve downloaded and installed your AndroidStudio, start it and follow the sequences bellow:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDw1BGq7b4w2yYoxBbPbNO5Cg55N4DPRvqAXWl_esKmuTrZ_bXfHu4imbQfXI_Xg6tlcA7Y_m9NglXY5LJkBDwOad4GtAtj_Drf6LJ0mdkhdjg9pvSJVf0vJ9qXz38C9gJj72X_-Jhdo2t/s1600/1.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;238&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDw1BGq7b4w2yYoxBbPbNO5Cg55N4DPRvqAXWl_esKmuTrZ_bXfHu4imbQfXI_Xg6tlcA7Y_m9NglXY5LJkBDwOad4GtAtj_Drf6LJ0mdkhdjg9pvSJVf0vJ9qXz38C9gJj72X_-Jhdo2t/s1600/1.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtvsM-1bXdbkqZAMEStN9s_UH1hbN9Lzst9pCxrhp5S8qpX33PbPP7dEqb7uwF-q8tr8O0_Bkfv935CyuZy0lkQ1okt_dlVTHIwWFqYYM8tRlC2CgOIMnYKfX180CXUkA_B473M_KE53Ph/s1600/2.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;238&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtvsM-1bXdbkqZAMEStN9s_UH1hbN9Lzst9pCxrhp5S8qpX33PbPP7dEqb7uwF-q8tr8O0_Bkfv935CyuZy0lkQ1okt_dlVTHIwWFqYYM8tRlC2CgOIMnYKfX180CXUkA_B473M_KE53Ph/s1600/2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEB7FfWXjpHvjq-LcMNFn8aNhkS30wb2qpcZsfBWbQ3uCUKez8vFYm99pZu9q55d8vf3u9kNYBubNmed_bZq1C2GiVsvpaza2gKRtcnS6VmwR0QJFo2_cgU2BIHBM4eLt2YI09w_B4Go3B/s1600/3.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;225&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEB7FfWXjpHvjq-LcMNFn8aNhkS30wb2qpcZsfBWbQ3uCUKez8vFYm99pZu9q55d8vf3u9kNYBubNmed_bZq1C2GiVsvpaza2gKRtcnS6VmwR0QJFo2_cgU2BIHBM4eLt2YI09w_B4Go3B/s1600/3.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span&gt;&lt;span id=&quot;goog_161849329&quot;&gt;&lt;/span&gt;&lt;span id=&quot;goog_161849330&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span&gt;&lt;span id=&quot;goog_161849329&quot;&gt;Changing the color Schema&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span&gt;&lt;span id=&quot;goog_161849329&quot;&gt;Now let&#39;s change also the color schema. To do so, copy this eclipse.xml file and save it in your desktop with the extension &lt;span style=&quot;background-color: orange;&quot;&gt;&lt;b&gt;.xml&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span&gt;&lt;span id=&quot;goog_161849329&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span id=&quot;goog_161849330&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;&amp;lt; ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;? &amp;gt;
&amp;lt; scheme name=&quot;eclipse&quot; version=&quot;1&quot; parent_scheme=&quot;Default&quot; &amp;gt;
  &amp;lt; option name=&quot;LINE_SPACING&quot; value=&quot;1.2&quot; /  &amp;gt;
  &amp;lt; option name=&quot;EDITOR_FONT_SIZE&quot; value=&quot;12&quot; /  &amp;gt;
  &amp;lt; option name=&quot;EDITOR_FONT_NAME&quot; value=&quot;Courier New&quot; /  &amp;gt;
  &amp;lt; colors &amp;gt;
    &amp;lt; option name=&quot;CARET_ROW_COLOR&quot; value=&quot;e8f2fe&quot; /  &amp;gt;
  &amp;lt; / colors &amp;gt;
  &amp;lt; attributes &amp;gt;
    &amp;lt; option name=&quot;ANNOTATION_NAME_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;646464&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;1&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CSS.FUNCTION&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CSS.IDENT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CSS.KEYWORD&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;1&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CSS.PROPERTY_NAME&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CSS.PROPERTY_VALUE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CSS.STRING&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CSS.TAG_NAME&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CSS.URL&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CUSTOM_KEYWORD1_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CUSTOM_KEYWORD2_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;660e7a&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CUSTOM_KEYWORD3_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;6666&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CUSTOM_KEYWORD4_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;660000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CUSTOM_LINE_COMMENT_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8080&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CUSTOM_MULTI_LINE_COMMENT_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8080&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;CUSTOM_STRING_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;EL.BOUNDS&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;EL.BRACKETS&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;EL.IDENT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;EL.KEYWORD&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;EL.NUMBER&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;EL.PARENTHS&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;EL.STRING&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;EL_BACKGROUND&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; value=&quot;edffed&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;HTML_ATTRIBUTE_NAME&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;HTML_ATTRIBUTE_VALUE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;HTML_ENTITY_REFERENCE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;HTML_TAG_NAME&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;INSTANCE_FIELD_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;c0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_BLOCK_COMMENT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;3f7f5f&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_DOC_COMMENT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;3f5fbf&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_DOC_MARKUP&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;1&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_INVALID_STRING_ESCAPE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;c0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; value=&quot;ffcccc&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_KEYWORD&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;7f0055&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_LINE_COMMENT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;3f7f5f&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_NUMBER&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_STRING&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;2a00ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JAVA_VALID_STRING_ESCAPE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.BADCHARACTER&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; value=&quot;ffcccc&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.BLOCK_COMMENT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;808080&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.BRACES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.BRACKETS&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.COMMA&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.DOC_COMMENT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;808080&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.DOC_MARKUP&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; value=&quot;e2ffe2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.DOC_TAG&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;1&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; value=&quot;808080&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;1&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.DOT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.INVALID_STRING_ESCAPE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; value=&quot;ffcccc&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.KEYWORD&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.LINE_COMMENT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;808080&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.NUMBER&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.OPERATION_SIGN&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.PARENTHS&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.REGEXP&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;-1&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.SEMICOLON&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.STRING&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JS.VALID_STRING_ESCAPE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;JSP_DIRECTIVE_NAME&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;PROPERTIES.KEY&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;1&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;PROPERTIES.VALID_STRING_ESCAPE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;PROPERTIES.VALUE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;SCOPE_KEY_Problems&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;SCOPE_KEY_Production&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;SCOPE_KEY_Tests&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;STATIC_FIELD_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;c0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;TEXT&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;323232&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;WARNING_ATTRIBUTES&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; value=&quot;f4c82d&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;2&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; value=&quot;f4c82d&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;XML_ATTRIBUTE_NAME&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;ff&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;XML_ATTRIBUTE_VALUE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;8000&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;XML_ENTITY_REFERENCE&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;XML_TAG_DATA&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
    &amp;lt; option name=&quot;XML_TAG_NAME&quot; &amp;gt;
      &amp;lt; value &amp;gt;
        &amp;lt; option name=&quot;FOREGROUND&quot; value=&quot;80&quot; /  &amp;gt;
        &amp;lt; option name=&quot;BACKGROUND&quot; /  &amp;gt;
        &amp;lt; option name=&quot;FONT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_COLOR&quot; /  &amp;gt;
        &amp;lt; option name=&quot;EFFECT_TYPE&quot; value=&quot;0&quot; /  &amp;gt;
        &amp;lt; option name=&quot;ERROR_STRIPE_COLOR&quot; /  &amp;gt;
      &amp;lt; / value &amp;gt;
    &amp;lt; / option &amp;gt;
  &amp;lt; / attributes &amp;gt;
&amp;lt; / scheme &amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;
Setting the color Schema &lt;/h3&gt;
Now cut this saved file from your desktop using Ctrl+X and put it in your user&#39;s directory by pressing Ctrl+V like that: (Note, your user&#39;s color config directory &lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span class=&quot;hps&quot;&gt;may&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;equal to&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;my&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;presented here&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;in this example) &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuaTPe8cZ5jXdIS5mYT7vNm0Gq9zUQWapSNtgOzPvZzFlS5Ny_WFbzHS_U9UxsnXNv4k6yP1Q5yBy4ij7gVW7YgyTNlR8_ItvYK2mo7G4aBkW77g4oaQsuUGAsJtD-9jrit0xZBHeVoEGG/s1600/6.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;144&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuaTPe8cZ5jXdIS5mYT7vNm0Gq9zUQWapSNtgOzPvZzFlS5Ny_WFbzHS_U9UxsnXNv4k6yP1Q5yBy4ij7gVW7YgyTNlR8_ItvYK2mo7G4aBkW77g4oaQsuUGAsJtD-9jrit0xZBHeVoEGG/s1600/6.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span class=&quot;hps&quot;&gt;Now you should see this file by looking at:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbu81zlM0UZvL0gycq1HJRuOvJ9obp8Z4k1T5rHg47o_v_b-C-4aPcSqaBez2khqUgmTseYESIOe6ZamSdVIWwS7-x1PAqKER8PjQP6YzF7kNG9fHhyphenhyphenKdWnMztCIsuS8l0cq5ZNBtvsBnW/s1600/4.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;238&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbu81zlM0UZvL0gycq1HJRuOvJ9obp8Z4k1T5rHg47o_v_b-C-4aPcSqaBez2khqUgmTseYESIOe6ZamSdVIWwS7-x1PAqKER8PjQP6YzF7kNG9fHhyphenhyphenKdWnMztCIsuS8l0cq5ZNBtvsBnW/s1600/4.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPa7pd-oQPe7F6EuXtqf6zG0ilMDquZfM-an6avH2N7nbNuQtPaaRWUqDJ8HbIHDUwUzmzTmbTd4UJqp2nIglX3QYHy1R5ByL4fPti17nncyyO1oz-OvksmWA2qmjiEbiaVS783HF9Y4E-/s1600/5.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPa7pd-oQPe7F6EuXtqf6zG0ilMDquZfM-an6avH2N7nbNuQtPaaRWUqDJ8HbIHDUwUzmzTmbTd4UJqp2nIglX3QYHy1R5ByL4fPti17nncyyO1oz-OvksmWA2qmjiEbiaVS783HF9Y4E-/s1600/5.png&quot; width=&quot;265&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span class=&quot;hps&quot;&gt;Done! That&#39;s all! Hope you like it. &lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span lang=&quot;en&quot;&gt;&lt;span class=&quot;hps&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span lang=&quot;en&quot;&gt;&lt;span class=&quot;hps&quot;&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6781499314583888830'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6781499314583888830'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/how-to-make-intellij-works-like-eclipse.html' title='How to make intellij works like eclipse incl. appearance and shortcuts in 1 min.'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDw1BGq7b4w2yYoxBbPbNO5Cg55N4DPRvqAXWl_esKmuTrZ_bXfHu4imbQfXI_Xg6tlcA7Y_m9NglXY5LJkBDwOad4GtAtj_Drf6LJ0mdkhdjg9pvSJVf0vJ9qXz38C9gJj72X_-Jhdo2t/s72-c/1.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-5983543265189407742</id><published>2014-08-22T12:33:00.004-07:00</published><updated>2020-10-12T12:38:06.606-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="memento"/><title type='text'>Programming Design Pattern - Memento Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Memento design 
pattern in action. The Memento design pattern is a very useful 
programming design pattern whenever &lt;span style=&quot;background-color: orange;&quot;&gt;you need to perform save, undo and restore functions&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Memento Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhiSlFiucdfTA8JyW6HztLF-hzg-DvAuQ1FWLaQDUKhZpS7mqvzzOjoodAAKR40f1LCcDF1vmqlOiKVVHKzMuhqFKoxCdEZ56zDOwwFVSSz1BTmRhfSx-Wxu_UjKLgtETvIZ0FYr5JaoZU/s1600/memento.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;309&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhiSlFiucdfTA8JyW6HztLF-hzg-DvAuQ1FWLaQDUKhZpS7mqvzzOjoodAAKR40f1LCcDF1vmqlOiKVVHKzMuhqFKoxCdEZ56zDOwwFVSSz1BTmRhfSx-Wxu_UjKLgtETvIZ0FYr5JaoZU/s1600/memento.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTBGexgQdMNkx0Amd5CuVVui35R9YlF89JTvXZrlxe2PuTPLBo3uoy1lm3zsseCbQmtPD2Mssfle1WvMzWLYZ8om13Mhs4EPLpdrxISirfDXxJcqwE-nClBwow5NO3LlYhnLIGPEmjM_hp/s1600/iterator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiafZxnXTkY-piP2nhhLw46dHNTMGJ2errhXyIrcrPSQx_THpGoM1T0ETVWjCdzXQ4uJLfJcQe6oCy8IRERRYd3ZQPC3tB2dfUTiMCwuZyZ-rr1ZnOQBg3mgi5ovpsyvVj1lBNGHwjEGcUt/s1600/bridge.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could perform save and undo actions restoring objects from a text file.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Memento&lt;/h3&gt;
That&#39;s the generic interface you could use while dealing with memento&#39;s capabilities. We will implement the memento on the fly in the Originator bellow. In other words, as soon as we need it in line.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;// 1 STEP: DEFINE THE MEMENTO 
public interface Memento &amp;lt; T &amp;gt; {
    T getMemento();
    void setMemento(T state);
}
&lt;/pre&gt;
&lt;h3&gt;
The Originator&lt;/h3&gt;
That&#39;s the generic interface we will use to implement the Originator. I will create it also on the fly in the client. In a few seconds bellow. :)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;//2 STEP: DEFINE THE ORIGINATOR 
public interface Save &amp;lt; T &amp;gt; extends Memento &amp;lt; T &amp;gt; {
    Memento &amp;lt; T &amp;gt; save();
    void restore(Memento &amp;lt; T &amp;gt; memento);
}
&lt;/pre&gt;
&lt;h3&gt;
The Interface Undo&lt;/h3&gt;
That&#39;s the generic interface we will use to create the care taker. I will create it also on the fly in the client. Be patient. :)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;//3 STEP: DEFINE THE CARETAKER
public interface Undo &amp;lt; T &amp;gt; {
    public void addMemento(Memento &amp;lt; T &amp;gt; state) ;
    public Memento &amp;lt; T &amp;gt; getMemento(int index);
}
&lt;/pre&gt;
&lt;h3&gt;
The concrete CareTaker&lt;/h3&gt;
That&#39;s the abstract implementation of the generic interface. I will create it also on the fly in the client. Stay tuned. :)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;//4 STEP: IMPLEMENT A ABSTRACTCARATAKER
public abstract class UndoCareTaker &amp;lt; T &amp;gt; implements Undo &amp;lt; T &amp;gt; {
    private List &amp;lt; Memento &amp;lt; T &amp;gt; &amp;gt; mementoList = new ArrayList &amp;lt; Memento &amp;lt; T &amp;gt; &amp;gt; ();
    public void addMemento(Memento &amp;lt; T &amp;gt; state) {
        if (state != null) {
            mementoList.add(state);
        }
    }
    public Memento &amp;lt; T &amp;gt; getMemento(int index) {
        int min = 0;
        int max = mementoList.size()-1;
        if(mementoList.isEmpty()){
            String msg = &quot;CareTaker has no entry! Passed index:&quot;;
            throw new IndexOutOfBoundsException(msg + index);
        }
        if(!(index &amp;gt; = min &amp;amp;&amp;amp; index &amp;lt; = max)){
            String msg = &quot;Passed index:&quot;+index+&quot; &amp;gt; Allowed index range: &quot;;
            throw new IndexOutOfBoundsException(msg + min + &quot; - &quot; + max);
        }
        return mementoList.get(index);
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Originator Ifself&lt;/h3&gt;
That&#39;s the abstract implementation of the interface generic interface Save.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;//5 STEP: IMPLEMENT A ABSTRACTORIGINATOR
public abstract class SaveOriginator &amp;lt; T &amp;gt; implements Save &amp;lt; T &amp;gt;{
    private T state;
    public void setMemento(T state) {this.state = state;}
    public T getMemento() {return state;}
    public void restore(Memento &amp;lt; T &amp;gt; memento) {setMemento(memento.getMemento());}
    public Memento &amp;lt; T &amp;gt; save() {
        Memento &amp;lt; T &amp;gt; memento = new Memento &amp;lt; T &amp;gt; () { // created on the fly as i promissed! :)
            private T state;
            @Override
            public void setMemento(T state) {this.state = state;}
            @Override
            public T getMemento() {return state;}
        };
        memento.setMemento(state);
        return memento;
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Finally, let&#39;s see how it works in practise.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        Save &amp;lt; String &amp;gt; originator = new SaveOriginator &amp;lt; String &amp;gt; (){};//created on the fly
        Undo &amp;lt; String &amp;gt; careTaker = new UndoCareTaker &amp;lt; String &amp;gt; (){};//created on the fly
        originator.setMemento(&quot;State #1&quot;);
        originator.setMemento(&quot;State #2&quot;);
        careTaker.addMemento(originator.save());
        originator.setMemento(&quot;State #3&quot;);
        careTaker.addMemento(originator.save());
        originator.setMemento(&quot;State #4&quot;);
        System.out.println(&quot;Current State: &quot; + originator.getMemento());
        originator.restore(careTaker.getMemento(0));
        System.out.println(&quot;First saved State: &quot; + originator.getMemento());
        originator.restore(careTaker.getMemento(1));
        System.out.println(&quot;Second saved State: &quot; + originator.getMemento());
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/5983543265189407742'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/5983543265189407742'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-memento.html' title='Programming Design Pattern - Memento Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhiSlFiucdfTA8JyW6HztLF-hzg-DvAuQ1FWLaQDUKhZpS7mqvzzOjoodAAKR40f1LCcDF1vmqlOiKVVHKzMuhqFKoxCdEZ56zDOwwFVSSz1BTmRhfSx-Wxu_UjKLgtETvIZ0FYr5JaoZU/s72-c/memento.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-8456199201785012847</id><published>2014-08-22T12:05:00.007-07:00</published><updated>2020-10-12T12:32:04.327-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="observer"/><category scheme="http://www.blogger.com/atom/ns#" term="observer pattern"/><title type='text'>Programming Design Pattern - Observer Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Observer design 
pattern in action. The Observer design pattern is a very useful 
programming design pattern whenever &lt;span style=&quot;background-color: orange;&quot;&gt;you need to notify classes or objects depending on changes or on user interactions. This is one of the most used pattern i think&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Observer Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF9u_P4YsL6zQiN0js9LCj4vECvN9IRKxGBuLZsyzcMzCUwjQCWwXATsl_156JkNkXC_rZQVM1zlWmJlMUeNxj5TxhONrImJJt2HbjjuUKS3L-lbcsuh6vYAwXPu6JQE8IReyVgzrXVZcB/s1600/observer.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;224&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF9u_P4YsL6zQiN0js9LCj4vECvN9IRKxGBuLZsyzcMzCUwjQCWwXATsl_156JkNkXC_rZQVM1zlWmJlMUeNxj5TxhONrImJJt2HbjjuUKS3L-lbcsuh6vYAwXPu6JQE8IReyVgzrXVZcB/s1600/observer.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCl_lMKuEUlFOsuyBfbh6eRIpV-g_2ZGqMuLDm_5PICJI7XqCTqa1NsiZdcwDc1eRXItx44XVE-LkiuilH_0D1H_lgqmJUc8I4m4lDEfP6L9VyXL9cjf07Li6HVQPRuWaezc4QAW8E4Ic_/s1600/Adapter.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTBGexgQdMNkx0Amd5CuVVui35R9YlF89JTvXZrlxe2PuTPLBo3uoy1lm3zsseCbQmtPD2Mssfle1WvMzWLYZ8om13Mhs4EPLpdrxISirfDXxJcqwE-nClBwow5NO3LlYhnLIGPEmjM_hp/s1600/iterator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could notify all postmans every time new posts arrives in the post office central station.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Observer and Observable&lt;/h3&gt;
Those interfaces are part of the default java library. For that reason we will not invent the wheel again. We will just use it.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The PostOffice Observable&lt;/h3&gt;
This will be our observable. In other words, the object which holds a register method for observers interested in being notified, as soon as changes in the post office occurs.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;import java.util.Observable;
public class PostOffice extends Observable {
    public void receberCartas(){
        System.out.println(&quot;New post arrived!&quot;);
        setChanged();
    }
    public void distributePost(){
        notifyObservers();
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Postman Observer&lt;/h3&gt;
This is the observer itself interested in being notified, as soon as changes in the post office occurs.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;import java.util.Observable;
import java.util.Observer;
public class Postman implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        System.out.println(&quot;Postman: distributing posts...&quot;);
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Let&#39;s see how those classes interacts together. Pay close attention, because this pattern will surely be one of the most used in your programming life! ;)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        PostOffice postoffice =  new PostOffice();
        // adding postmans to the post office (observers)
        postoffice.addObserver(new Postman());
        // Simulating arrival of letters in the central post office
        postoffice.receberCartas();
        // simulating postmans distributing letters
        postoffice.distributePost();
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! I hope you like it!&lt;div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/8456199201785012847'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/8456199201785012847'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-observer.html' title='Programming Design Pattern - Observer Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjF9u_P4YsL6zQiN0js9LCj4vECvN9IRKxGBuLZsyzcMzCUwjQCWwXATsl_156JkNkXC_rZQVM1zlWmJlMUeNxj5TxhONrImJJt2HbjjuUKS3L-lbcsuh6vYAwXPu6JQE8IReyVgzrXVZcB/s72-c/observer.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-4646756462532383504</id><published>2014-08-22T09:27:00.003-07:00</published><updated>2020-10-12T12:38:20.289-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="adapter"/><category scheme="http://www.blogger.com/atom/ns#" term="adapter pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Adapter Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Adapter design 
pattern in action. The Adapter design pattern is a very useful 
programming design pattern whenever &lt;span style=&quot;background-color: orange;&quot;&gt;you need to ensure communication between incompatible interfaces, classes or objects&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Adapter Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCl_lMKuEUlFOsuyBfbh6eRIpV-g_2ZGqMuLDm_5PICJI7XqCTqa1NsiZdcwDc1eRXItx44XVE-LkiuilH_0D1H_lgqmJUc8I4m4lDEfP6L9VyXL9cjf07Li6HVQPRuWaezc4QAW8E4Ic_/s1600/Adapter.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;138&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCl_lMKuEUlFOsuyBfbh6eRIpV-g_2ZGqMuLDm_5PICJI7XqCTqa1NsiZdcwDc1eRXItx44XVE-LkiuilH_0D1H_lgqmJUc8I4m4lDEfP6L9VyXL9cjf07Li6HVQPRuWaezc4QAW8E4Ic_/s1600/Adapter.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTBGexgQdMNkx0Amd5CuVVui35R9YlF89JTvXZrlxe2PuTPLBo3uoy1lm3zsseCbQmtPD2Mssfle1WvMzWLYZ8om13Mhs4EPLpdrxISirfDXxJcqwE-nClBwow5NO3LlYhnLIGPEmjM_hp/s1600/iterator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiafZxnXTkY-piP2nhhLw46dHNTMGJ2errhXyIrcrPSQx_THpGoM1T0ETVWjCdzXQ4uJLfJcQe6oCy8IRERRYd3ZQPC3tB2dfUTiMCwuZyZ-rr1ZnOQBg3mgi5ovpsyvVj1lBNGHwjEGcUt/s1600/bridge.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could adapt incompatible interfaces so that they can interact together. We will connect a&amp;nbsp; rounded 3-pins plug with a rectangular 2-pins plug.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Incompatible Classes&lt;/h3&gt;
The classes to adapt.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class CylinderSocket {
    public CylinderPin cylinderPin;
    
    public CylinderSocket(){
        this.cylinderPin=new CylinderPin(&quot;corrent&quot;, &quot;neutro&quot;, &quot;terra&quot;);
    }
}
public class RectangularPlug {//ADAPTEE
    public String corrente;
    public String neutro;

    public String getPower() {
        return &quot;power on!&quot;;
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Adapter&lt;/h3&gt;
To be able to adapt something, first of all we must define the common interface between at least two classes/objects.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public interface Plugable { // &quot;TARGET&quot; INTERFACE
    public String getPower();
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Finally, let&#39;s test our adapter which will connect a rounded plug with a rectangular plug.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        // Rounded plug with 3 pins with rectangular plug with 2 pins
        CylinderSocket cylinderSocket = new CylinderSocket();
        // Client knows only the interface
        Plugable plugable = new PlugAdapter(cylinderSocket);
        // see if connection works
        System.out.println(plugable.getPower());
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/4646756462532383504'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/4646756462532383504'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-adapter.html' title='Programming Design Pattern - Adapter Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCl_lMKuEUlFOsuyBfbh6eRIpV-g_2ZGqMuLDm_5PICJI7XqCTqa1NsiZdcwDc1eRXItx44XVE-LkiuilH_0D1H_lgqmJUc8I4m4lDEfP6L9VyXL9cjf07Li6HVQPRuWaezc4QAW8E4Ic_/s72-c/Adapter.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-292010156614146466</id><published>2014-08-22T08:39:00.005-07:00</published><updated>2020-10-12T12:38:29.028-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Mediator"/><title type='text'>Programming Design Pattern - Mediator Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Mediator design 
pattern in action. The Mediator design pattern is a very useful 
programming design pattern whenever &lt;span style=&quot;background-color: orange;&quot;&gt;you want to centralize logics making it maintainable when a lot of objects has to communicate together&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Mediator Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpwDyb3vKCt9b4EECMF3ndG1xHR6ASKQh8rCzhJfS2oD8FLM_CIFKwBVywhXXMGp5JgHM7LqXmM9wDZtWPVZAxmlneimzV8gIO4o9xYECxwPGhxK2kIjUW6ETn5KTc2YPS5RScU1bFprWP/s1600/mediator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;111&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpwDyb3vKCt9b4EECMF3ndG1xHR6ASKQh8rCzhJfS2oD8FLM_CIFKwBVywhXXMGp5JgHM7LqXmM9wDZtWPVZAxmlneimzV8gIO4o9xYECxwPGhxK2kIjUW6ETn5KTc2YPS5RScU1bFprWP/s1600/mediator.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTBGexgQdMNkx0Amd5CuVVui35R9YlF89JTvXZrlxe2PuTPLBo3uoy1lm3zsseCbQmtPD2Mssfle1WvMzWLYZ8om13Mhs4EPLpdrxISirfDXxJcqwE-nClBwow5NO3LlYhnLIGPEmjM_hp/s1600/iterator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiafZxnXTkY-piP2nhhLw46dHNTMGJ2errhXyIrcrPSQx_THpGoM1T0ETVWjCdzXQ4uJLfJcQe6oCy8IRERRYd3ZQPC3tB2dfUTiMCwuZyZ-rr1ZnOQBg3mgi5ovpsyvVj1lBNGHwjEGcUt/s1600/bridge.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could centralize the logic from a bunch of devices in a smart house making it better maintainable and decoupling the objects from each other.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Events&lt;/h3&gt;
In our example, we will be dealing with a lot of devices interacting with each other in a smart house. Those devices fire events. Let&#39;s define some events for each of them. After that we will define the devices itself.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public enum Evento {
    TERMOMETRO, METEOROLOGIA, LIXO, DISPERTADOR, 
    AGENDA, MAQUINA_CAFE, AR_CONDICIONADO,BORRIFADOR_PLANTAS;
}
public interface Occurrence {
    void ocorrencia(Evento evento, Object origin);
}
&lt;/pre&gt;
&lt;h3&gt;
The Devices to mediate&lt;/h3&gt;
In our example, we have a lot of devices in our house to mediate. Let&#39;s define some.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public abstract class Device{
    private Occurrence occurrence;
    public Device(Occurrence centralControl) {
        this.occurrence = centralControl;
    }
    public void inform(Evento evento) {
        occurrence.ocorrencia(evento, this);
    }
    public abstract void update();
}
public class Agenda extends Device{

    public Agenda(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void exameMedico(){
        inform(Evento.AGENDA);
    }
    @Override
    public void update() {
        System.out.println(&quot;Visitando médico...!&quot;);
    }

}
public class ArCondicionado extends Device{
    public ArCondicionado(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void fazerManutencao(){
        inform(Evento.AR_CONDICIONADO);
    }
    @Override
    public void update() {
        System.out.println(&quot;Manutenção feita com sucesso!&quot;);
    }
}
public class BorrifadorPlantas extends Device{
    public BorrifadorPlantas(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void faltandoAgua(){
        inform(Evento.BORRIFADOR_PLANTAS);
    }
    @Override
    public void update() {
        System.out.println(&quot;Registro aberto. Pode borrifar!&quot;);
    }
}
public class Dispertador extends Device {

    public Dispertador(Occurrence ocorrencia) {
        super(ocorrencia);
    }

    public void horaDeTrabalhar() {
        inform(Evento.DISPERTADOR);
    }

    @Override
    public void update() {
        System.out.println(&quot;Acorrrrrdaaaaaa! Vai trabalhar menino!!!&quot;);
    }
}
public class MaquinaCafe extends Device{

    public MaquinaCafe(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void cofreMoedasCheio(){
        inform(Evento.MAQUINA_CAFE);
    }
    @Override
    public void update() {
        System.out.println(&quot;Cofre esvaziado! Maquina is ready!&quot;);
    }

}
public class Meteorologia extends Device{

    public Meteorologia(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    
    public void alertaDeTempestade(){
        inform(Evento.METEOROLOGIA);
    }
    @Override
    public void update() {
        System.out.println(&quot;Fique em casa! Tornado se aproximando!&quot;);
    }

}
public class RecolhaLixo extends Device{

    public RecolhaLixo(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void colocarLixoPraFora(){
        inform(Evento.LIXO);
    }
    @Override
    public void update() {
        System.out.println(&quot;O caminhão de lixo irá passar daqui a pouco!&quot;);
    }

}
&lt;/pre&gt;
&lt;h3&gt;
The Mediator&lt;/h3&gt;
This class will be mediating between the devices in the house. It is like the CPU in the computer if you like the analogy. It centralizes the logic in one place.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class CentralControle implements Occurrence {
    @Override
    public void ocorrencia(Evento evento, Object origin) {
        switch (evento) {
        case AGENDA:((Agenda)origin).update();break;
        case AR_CONDICIONADO:
            // FAZER MANUTENÇÃO...
            // ...ATUALIZAR APARELHO
            ((ArCondicionado)origin).update();
            break;
        case BORRIFADOR_PLANTAS:((BorrifadorPlantas)origin).update();break;
        case DISPERTADOR:((Dispertador)origin).update();break;
        case LIXO:((RecolhaLixo)origin).update();break;
        case MAQUINA_CAFE:((MaquinaCafe)origin).update();break;
        case METEOROLOGIA:((Meteorologia)origin).update();break;
        case TERMOMETRO:((Termometro)origin).update();break;
        default:break;}
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/292010156614146466'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/292010156614146466'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-mediator.html' title='Programming Design Pattern - Mediator Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpwDyb3vKCt9b4EECMF3ndG1xHR6ASKQh8rCzhJfS2oD8FLM_CIFKwBVywhXXMGp5JgHM7LqXmM9wDZtWPVZAxmlneimzV8gIO4o9xYECxwPGhxK2kIjUW6ETn5KTc2YPS5RScU1bFprWP/s72-c/mediator.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-6378415431343552334</id><published>2014-08-22T06:52:00.004-07:00</published><updated>2020-10-12T12:30:29.557-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Iterator"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Iterator Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Iterator design 
pattern in action. The Iterator design pattern is a very useful 
programming design pattern whenever &lt;span style=&quot;background-color: orange;&quot;&gt;you need to iterate over structures without knowing how this structure is implemented&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Iterator Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTBGexgQdMNkx0Amd5CuVVui35R9YlF89JTvXZrlxe2PuTPLBo3uoy1lm3zsseCbQmtPD2Mssfle1WvMzWLYZ8om13Mhs4EPLpdrxISirfDXxJcqwE-nClBwow5NO3LlYhnLIGPEmjM_hp/s1600/iterator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;142&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTBGexgQdMNkx0Amd5CuVVui35R9YlF89JTvXZrlxe2PuTPLBo3uoy1lm3zsseCbQmtPD2Mssfle1WvMzWLYZ8om13Mhs4EPLpdrxISirfDXxJcqwE-nClBwow5NO3LlYhnLIGPEmjM_hp/s1600/iterator.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiafZxnXTkY-piP2nhhLw46dHNTMGJ2errhXyIrcrPSQx_THpGoM1T0ETVWjCdzXQ4uJLfJcQe6oCy8IRERRYd3ZQPC3tB2dfUTiMCwuZyZ-rr1ZnOQBg3mgi5ovpsyvVj1lBNGHwjEGcUt/s1600/bridge.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could iterate over a restaurant&#39;s menu, showing the food options to our clients.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Iterable and Iterator&lt;/h3&gt;
Because those interfaces are part of the java default library, we will not invent the wheel again. So we will make usage of it. &lt;span style=&quot;background-color: orange;&quot;&gt;You don&#39;t need to implement it&lt;/span&gt;. I&#39;ve just inserted here in case you want to understand the details and ideas behind it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;import java.util.Iterator;
public interface Iterable &amp;lt; T &amp;gt; {
    Iterator &amp;lt; T &amp;gt; iterator();
}
public interface Iterator &amp;lt; E &amp;gt; {
    boolean hasNext();
    E next();
    void remove();
}
&lt;/pre&gt;
&lt;h3&gt;
The Product&lt;/h3&gt;
Well, the idea behind the iterator is to iterate over a structure without knowing the details about this implementation. We just want to iterate over it to get some information about this structure. For this reason we first need a product (in our case Food) and a structure (in our case the MenuIterator). &lt;span style=&quot;background-color: orange;&quot;&gt;Note: &lt;/span&gt;in the real world we would name it just Menu. But for this tutorial here, I&#39;ve intentionally defined MenuIterator to facilitate the understanding.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Food {
    private String name;
    private String description;
    private boolean vegetarian;
    private double price;
    public Food(String name, String description, boolean vegetarian, double price) {
        super();
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public boolean isVegetarian() {
        return vegetarian;
    }
    public void setVegetarian(boolean vegetarian) {
        this.vegetarian = vegetarian;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return getName()+&quot;, &quot;+getPrice()+&quot; R$\n  - &quot;+getDescription();
    }
}
public class MenuIterator implements Iterator &amp;lt; Food &amp;gt; {
    private Food[] foodOptions;
    int position = 0;
    MenuIterator(Food[] foodOptions){this.foodOptions=foodOptions;}
    @Override
    public boolean hasNext() {
        if(position &amp;gt; = foodOptions.length || foodOptions[position]==null){
            return false;
        }return true;
    }
    @Override
    public Food next() {
        final Food food = foodOptions[position];
        position++;
        return food;
    }
    @Override
    public void remove() {/*NOP*/}
}
&lt;/pre&gt;
&lt;h3&gt;
The Barmaid&lt;/h3&gt;
Our Barmaid will use the iterator to navigate over the menu showing the food options to the client.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Barmaid {
    private Iterable &amp;lt; Food &amp;gt; menu;
    public Barmaid(Menu menu) {
        this.menu = menu;
    }
    public void showMenu(){
        final Iterator &amp;lt; Food &amp;gt; iterator = menu.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Finally, we&#39;ve opened our restaurant and the barmaid is showing the menu&#39;s to the clients. :)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        String name = &quot;Roast beef&quot;;
        String description = &quot;Delicious roast beef argentino&quot;;
        boolean vegetarian = false;
        double price = 12.99;
        final Food roastBeef = new Food(name,description,vegetarian, price);
        name = &quot;Sopa tomate&quot;;
        description = &quot;Deliciosa sopa de tomate com oregano e creme de leite&quot;;
        vegetarian = true;
        price = 8.50;
        final Food sopaTomate = new Food(name,description,vegetarian, price);
        Food[] foodOptions = new Food[]{roastBeef, sopaTomate };
        final Menu menu = new Menu(foodOptions);
        final Barmaid barmaid = new Barmaid(menu);
        barmaid.showMenu();
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6378415431343552334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6378415431343552334'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-iterator.html' title='Programming Design Pattern - Iterator Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTBGexgQdMNkx0Amd5CuVVui35R9YlF89JTvXZrlxe2PuTPLBo3uoy1lm3zsseCbQmtPD2Mssfle1WvMzWLYZ8om13Mhs4EPLpdrxISirfDXxJcqwE-nClBwow5NO3LlYhnLIGPEmjM_hp/s72-c/iterator.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-5475716279921027771</id><published>2014-08-22T05:48:00.007-07:00</published><updated>2020-10-12T12:39:15.366-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Interpreter"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Interpreter Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Interpreter design 
pattern in action. The Interpreter design pattern is a very useful 
programming design pattern while &lt;span style=&quot;background-color: orange;&quot;&gt;dealing with transformations, language issues, changing from one system to another and so on&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Interpreter Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjULwosS4XuOeIq5SwL1bgZbOsGCjmHR0CiaPYJSnYmE1YDW_uyPDm-bXN_yhwy5OTEeoX-50JSGvrp8sGieCVbaGo-MIZiMj1qYbaH77VCcJaC1LWyEVJDt7NPXgVitN4wjAvkFQJo4S1N/s1600/interpret.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;147&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjULwosS4XuOeIq5SwL1bgZbOsGCjmHR0CiaPYJSnYmE1YDW_uyPDm-bXN_yhwy5OTEeoX-50JSGvrp8sGieCVbaGo-MIZiMj1qYbaH77VCcJaC1LWyEVJDt7NPXgVitN4wjAvkFQJo4S1N/s1600/interpret.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiafZxnXTkY-piP2nhhLw46dHNTMGJ2errhXyIrcrPSQx_THpGoM1T0ETVWjCdzXQ4uJLfJcQe6oCy8IRERRYd3ZQPC3tB2dfUTiMCwuZyZ-rr1ZnOQBg3mgi5ovpsyvVj1lBNGHwjEGcUt/s1600/bridge.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could interpret different number systems like decimal to octal or binary and so on.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Context of the interpretation&lt;/h3&gt;
This is one of the core concept of interpretation. It must define the context. It is in the nature of the thing. Without a context, no interpretation. So let&#39;s define our context first. We want to implement a number system interpretation. For this reason I&#39;m defining the types of number system. After that I&#39;m able to define the Interpreter itself and the NumberSystemInterpreter.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public enum InterpreterContext {INTEGER_TO_BIN,INTEGER_TO_HEX,INTEGER_TO_OCTAL;}

public interface Interpreter &amp;lt; T, V &amp;gt; {
    T interpret(V toInterpret, InterpreterContext context);
}

public class NumberSystemInterpreter implements Interpreter &amp;lt; String, Integer &amp;gt; {
    @Override
    public String interpret(Integer toInterpret, InterpreterContext context) {
        switch (context) {
        case INTEGER_TO_BIN:
            return Integer.toBinaryString(toInterpret);
        case INTEGER_TO_HEX:
            return Integer.toHexString(toInterpret);
        case INTEGER_TO_OCTAL:
            return Integer.toOctalString(toInterpret);
        default:return &quot;No grammar available&quot;;
        }
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Expressions&lt;/h3&gt;
That&#39;s also typical for Interpreter pattern. We need some expressions to be interpreted. In our case we will be defining three Expressions. One for each number system to be interpreted.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public interface Expression &amp;lt; T, V &amp;gt; {
    String interpret(Interpreter &amp;lt; T, V &amp;gt; interpreter);
}
public class OctalExpression implements Expression &amp;lt; String, Integer &amp;gt; {
    private int toInterpret;
    public OctalExpression(int integer){this.toInterpret=integer;}
    @Override
    public String interpret(Interpreter &amp;lt; String, Integer &amp;gt; numberSystem) {
        return numberSystem.interpret(toInterpret, InterpreterContext.INTEGER_TO_OCTAL);
    }
}
public class HexExpression implements Expression &amp;lt; String, Integer &amp;gt; {
    private int toInterpret;
    public HexExpression(int integer){this.toInterpret=integer;}
    @Override
    public String interpret(Interpreter &amp;lt; String, Integer &amp;gt; numberSystem) {
        return numberSystem.interpret(toInterpret, InterpreterContext.INTEGER_TO_HEX);
    }
}
public class BinaryExpression implements Expression &amp;lt; String, Integer &amp;gt; {
    private int toInterpret;
    public BinaryExpression(int integer){
        this.toInterpret=integer;
    }
    @Override
    public String interpret(Interpreter &amp;lt; String, Integer &amp;gt; interpreter) {
        return interpreter.interpret(toInterpret, InterpreterContext.INTEGER_TO_BIN);
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Finally, let&#39;s see the interpreter in action. ;)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        Interpreter &amp;lt; String, Integer &amp;gt; numberSystem = new NumberSystemInterpreter();
        Expression &amp;lt; String, Integer &amp;gt; decimalToBin = new BinaryExpression(23);
        System.out.println(decimalToBin.interpret(numberSystem));
        Expression &amp;lt; String, Integer &amp;gt; decimalToHex = new HexExpression(23);
        System.out.println(decimalToHex.interpret(numberSystem));
        Expression &amp;lt; String, Integer &amp;gt; decimalToOctal = new OctalExpression(23);
        System.out.println(decimalToOctal.interpret(numberSystem));
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/5475716279921027771'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/5475716279921027771'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-interpreter.html' title='Programming Design Pattern - Interpreter Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjULwosS4XuOeIq5SwL1bgZbOsGCjmHR0CiaPYJSnYmE1YDW_uyPDm-bXN_yhwy5OTEeoX-50JSGvrp8sGieCVbaGo-MIZiMj1qYbaH77VCcJaC1LWyEVJDt7NPXgVitN4wjAvkFQJo4S1N/s72-c/interpret.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-8964154595993905091</id><published>2014-08-20T12:49:00.002-07:00</published><updated>2020-10-12T12:39:23.756-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bridge"/><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Bridge Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Bridge design 
pattern in action. The Bridge design pattern is a very useful 
programming design pattern whenever &lt;span style=&quot;background-color: orange;&quot;&gt;you need to be able to vary abstractions and implementations of structures&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Bridge Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiafZxnXTkY-piP2nhhLw46dHNTMGJ2errhXyIrcrPSQx_THpGoM1T0ETVWjCdzXQ4uJLfJcQe6oCy8IRERRYd3ZQPC3tB2dfUTiMCwuZyZ-rr1ZnOQBg3mgi5ovpsyvVj1lBNGHwjEGcUt/s1600/bridge.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;115&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiafZxnXTkY-piP2nhhLw46dHNTMGJ2errhXyIrcrPSQx_THpGoM1T0ETVWjCdzXQ4uJLfJcQe6oCy8IRERRYd3ZQPC3tB2dfUTiMCwuZyZ-rr1ZnOQBg3mgi5ovpsyvVj1lBNGHwjEGcUt/s1600/bridge.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s1600/flyweight.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could separete the remote control from the TV, so we can vary both sides. (The abstraction[remote controls] and the implemention[TV&#39;s] of it) This way we will be able to implement platforms dependent TV&#39;s and remote controls.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Bridge&lt;/h3&gt;
This is the common interface that acts between two object structures (the bridge). The TV types are the structure of different clients that can have different remote controls.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public interface TV {
    public void on();
    public void off();
    public void setChannel(int id);
}
public class Sony implements TV {
    public void on() {System.out.println(&quot;Sony TV on!&quot;);}
    public void off() {System.out.println(&quot;Sony TV off!&quot;);}
    public void setChannel(int id){System.out.println(&quot;Sony Channel set: &quot;+id);}
}
public class Philips implements TV {
    private int channelId=0;//SPECIFIC FEATURE FROM PHILIPS
    public void on() {System.out.println(&quot;Philips TV on!&quot;);}
    public void off() {System.out.println(&quot;Philips TV off!&quot;);}
    public void setChannel(int id){
        this.channelId=id;
        System.out.println(&quot;Philips Channel set: &quot;+id);
    }
    // SPECIFIC IMPLEMENTATION FROM PHILIPS
    public int getChannelId() {return channelId;}
}
&lt;/pre&gt;
&lt;h3&gt;
The Remote Control&lt;/h3&gt;
Each TV communicates over different remote controls and each of them could have more, less or different functions.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public abstract class ControleRemoto {
    protected TV tv;
    public ControleRemoto(TV tv) {this.tv = tv;}
    public abstract void on();
    public abstract void off();
    public abstract void setChannel(int id);
}
public class PhilipsControleRemoto extends ControleRemoto {
    public PhilipsControleRemoto(TV tv) {super(tv);}
    public void on() {tv.on();}
    public void off() {tv.off();}
    public void setChannel(int id) {tv.setChannel(id);}
    // SPECIFIC IMPLEMENTATION FOR PHILIPS
    public void nextChannel(){
        final int currentChannelId = ((Philips)tv).getChannelId();
        tv.setChannel(currentChannelId+1);
    }
}
public class SonyControleRemoto extends ControleRemoto {
    public SonyControleRemoto(TV tv) {super(tv);}
    public void on() {tv.on();}
    public void off() {tv.off();}
    public void setChannel(int id) {tv.setChannel(id);}
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Here we can see how the clients (TV&#39;s) communicate over the bridges with the different remote controls. With this concept, both, TV&#39;s and Remote Control&#39;s, can vary.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        final ControleRemoto philips = new PhilipsControleRemoto(new Philips());
        final ControleRemoto sony = new SonyControleRemoto(new Sony());
        sony.setChannel(3);
        // PHILIPS HAS MORE FUNCTIONALITIES
        philips.setChannel(18);
        ((PhilipsControleRemoto)philips).nextChannel();
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That´s all. Hope you like it!
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/8964154595993905091'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/8964154595993905091'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-bridge.html' title='Programming Design Pattern - Bridge Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiafZxnXTkY-piP2nhhLw46dHNTMGJ2errhXyIrcrPSQx_THpGoM1T0ETVWjCdzXQ4uJLfJcQe6oCy8IRERRYd3ZQPC3tB2dfUTiMCwuZyZ-rr1ZnOQBg3mgi5ovpsyvVj1lBNGHwjEGcUt/s72-c/bridge.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-6420788837612636887</id><published>2014-08-20T12:22:00.003-07:00</published><updated>2020-10-12T12:39:31.779-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="chain of responsibility"/><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Chain of Responsibility Pattern Applied - Best Practise</title><content type='html'>&lt;br /&gt;
Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Chain of Responsibility design 
pattern in action. The Chain of Responsibility design pattern is a very useful 
programming design pattern while &lt;span style=&quot;background-color: orange;&quot;&gt;dealing processes that happens in sequences&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Chain of Responsibility Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOvD9VIQlKReJK0TlkD7cOUi4ZGutykTqn2i0HclGwd8DTGg-vNME7cemV5jwF3obCC2s2Q9vgfq9lMVjhCGSzUMFjFLFDPs2ZoScWWfVomzpUQOyj2UBeDRw4pdnnfQ2tY4SRKw79nvaC/s1600/chain.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;161&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOvD9VIQlKReJK0TlkD7cOUi4ZGutykTqn2i0HclGwd8DTGg-vNME7cemV5jwF3obCC2s2Q9vgfq9lMVjhCGSzUMFjFLFDPs2ZoScWWfVomzpUQOyj2UBeDRw4pdnnfQ2tY4SRKw79nvaC/s1600/chain.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s1600/flyweight.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could implement a customer after sales feedback robot. How could we do it? In this example we will see it.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The abstract chain&lt;/h3&gt;
This is the base class to be implemented by all participants of this chain.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public abstract class Inspector &amp;lt; T &amp;gt; {
    protected T toInspect;
    protected Inspector &amp;lt; T &amp;gt; next;
    public Inspector(Inspector &amp;lt; T &amp;gt; next) {this.next = next;}
    public void inspect(T toInspect){
        this.toInspect=toInspect;
        if(toInspect==responsibility()){
          System.out.println(responsibility() + &quot; &quot; + action());
        }toNextInspector();
    }
    public abstract T responsibility();
    public abstract T action();
    public void toNextInspector(){
        if(next instanceof NullObject){return;}
        next.inspect(toInspect);
    }
    public static class NullObject extends Inspector &amp;lt; String &amp;gt; {
        public NullObject() {super(null);}
        public String responsibility() {return null;}
        public String action() {return null;}
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The participants of the chain&lt;/h3&gt;
Every participant has a single responsibility and gets a chance to process it. If it can not handle it, it will pass it over to the next till the end.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Complaint extends Inspector &amp;lt; String &amp;gt; {
    public Complaint(Inspector &amp;lt; String &amp;gt; next) {super(next);}
    public String responsibility() {return &quot;Reclamação&quot;;}
    public String action() {return &quot;Enviar pro departamento de qualidade!&quot;;}
}
public class Billing extends Inspector &amp;lt; String &amp;gt; {
    public Billing(Inspector &amp;lt; String &amp;gt; next) {super(next);}
    public String responsibility() {return &quot;Cobrança&quot;;}
    public String action() {return &quot;Enviar pro departamento de pos-vendas!&quot;;}
}
public class Praise extends Inspector &amp;lt; String &amp;gt; {
    public Praise(Inspector &amp;lt; String &amp;gt; next) {super(next);}
    public String responsibility() {return &quot;Elogio&quot;;}
    public String action() {return &quot;Enviar pro RH!&quot;;}
}
public class Suggestion extends Inspector &amp;lt; String &amp;gt; {
    public Suggestion(Inspector &amp;lt; String &amp;gt; next) {super(next);}
    public String responsibility() {return &quot;Sugestão&quot;;}
    public String action() {return &quot;Enviar para o setor de desenvolvimento!&quot;;}
}
public class Spam extends Inspector &amp;lt; String &amp;gt; {
    public Spam(Inspector &amp;lt; String &amp;gt; next) {super(next);}
    public String responsibility() {return &quot;Spam&quot;;}
    public String action() {return &quot;Jogar no lixo!&quot;;}
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Finally, let´s see how our customer after sales robot works. :)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        Inspector &amp;lt; String &amp;gt; inspector;
        String[] incomingPost = new String[] { 
                &quot;ABC&quot;, 
                &quot;Spam&quot;, 
                &quot;Elogio&quot;, 
                &quot;Reclamação&quot;, 
                &quot;Sugestão&quot;,
                &quot;Cobrança&quot; };
        for (String post : incomingPost) {
            inspector = new Spam(new Complaint(new Praise(new Billing(new Suggestion(new Inspector.NullObject())))));
            inspector.inspect(post);
        }
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all. Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6420788837612636887'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/6420788837612636887'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-chain-of.html' title='Programming Design Pattern - Chain of Responsibility Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOvD9VIQlKReJK0TlkD7cOUi4ZGutykTqn2i0HclGwd8DTGg-vNME7cemV5jwF3obCC2s2Q9vgfq9lMVjhCGSzUMFjFLFDPs2ZoScWWfVomzpUQOyj2UBeDRw4pdnnfQ2tY4SRKw79nvaC/s72-c/chain.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-4912545406219185848</id><published>2014-08-20T12:06:00.005-07:00</published><updated>2020-10-12T12:39:39.856-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="composite"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Composite Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Composite design 
pattern in action. The Composite design pattern is a very useful 
programming design pattern while &lt;span style=&quot;background-color: orange;&quot;&gt;dealing with trees, folders and structures&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Composite Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEii6hZn53JlOtowZxyhlA2cVqcPSGNmo39Q4Kns10AR9c-IYpQiPxrWygNFNNC42EMeH_8SZnhiyFq8LmjnFkGDcY9z05HDZuLgtFLRu15I9DQIcMtCWycNtxzk0P_YulWCNIJoDWMoxlIa/s1600/composite.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;104&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEii6hZn53JlOtowZxyhlA2cVqcPSGNmo39Q4Kns10AR9c-IYpQiPxrWygNFNNC42EMeH_8SZnhiyFq8LmjnFkGDcY9z05HDZuLgtFLRu15I9DQIcMtCWycNtxzk0P_YulWCNIJoDWMoxlIa/s1600/composite.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could implement a menu structure for a restaurant. How could we do it? In this example we will see it.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Menu Component&lt;/h3&gt;
That&#39;s the base class for all menu itens (topics) and sub-itens (menu itself)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public abstract class MenuComponent {
    // MAKE INTERFACE FROM FOOD
    public String getName(){throw new UnsupportedOperationException();}
    public void setName(String name){throw new UnsupportedOperationException();}
    public String getDescription(){throw new UnsupportedOperationException();}
    public void setDescription(String description){throw new UnsupportedOperationException();}
    public boolean isVegetarian(){throw new UnsupportedOperationException();}
    public void setVegetarian(boolean vegetarian){throw new UnsupportedOperationException();}
    public double getPrice(){throw new UnsupportedOperationException();}
    public void setPrice(double price){throw new UnsupportedOperationException();}
    public void showMenu(){throw new UnsupportedOperationException();}
    // COMPOSITE METHODS
    public void add(MenuComponent component){throw new UnsupportedOperationException();}
    public void remove(MenuComponent component){throw new UnsupportedOperationException();}
    public MenuComponent getChild(int index){throw new UnsupportedOperationException();}
}
&lt;/pre&gt;
&lt;h3&gt;
The Menu itens&lt;/h3&gt;
Well, let´s implement the base item we wanna offer in our restaurant. (Food)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Food extends MenuComponent{
    private String name;
    private String description;
    private boolean vegetarian;
    private double price;
    public Food(String name, String description, boolean vegetarian, double price) {
        super();
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public String getDescription() {return description;}
    public void setDescription(String description) {this.description = description;}
    public boolean isVegetarian() {return vegetarian;}
    public void setVegetarian(boolean vegetarian) {this.vegetarian = vegetarian;}
    public double getPrice() {return price;}
    public void setPrice(double price) { this.price = price;}
    public void showMenu() {System.out.println(toString());}
    @Override
    public String toString() {
        return getName()+&quot;, R$&quot;+getPrice()+&quot; - vegetário: &quot;+isVegetarian()+&quot;\n  - &quot;+getDescription();
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Menu ifself&lt;/h3&gt;
Well, if we want to present the restaurant&#39;s specialties, we must define the menu itself. This happens here.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Menu extends MenuComponent {
    private List &amp;lt; MenuComponent &amp;gt; tree =  new ArrayList &amp;lt; MenuComponent &amp;gt; ( );
    private String name, description;
    public Menu(String name, String description) {
        this.name = name; this.description = description;
    }
    public String getName() {return name;}
    public String getDescription() {return description;}
    public void showMenu() {
        System.out.println(getName());
        System.out.println(getDescription());
        System.out.println(&quot;--------------------&quot;);
        final Iterator&lt;menucomponent&gt; iterator = tree.iterator();
        while(iterator.hasNext()){
            iterator.next().showMenu();
        }
    }
    public void add(MenuComponent component) {tree.add(component);}
    public void remove(MenuComponent component) {tree.remove(component);}
    public MenuComponent getChild(int index) {return tree.get(index);}
}
&lt;/menucomponent&gt;&lt;/pre&gt;
&lt;h3&gt;
The Barmaid&lt;/h3&gt;
Also need someone that that receives the orders from the customers. So let´s define it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Barmaid {
    private MenuComponent menus;
    public Barmaid(MenuComponent menus) {
        this.menus = menus;
    }
    public void showMenus(){
        menus.showMenu();
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Finally, let´s test our brand new restaurant´s menu and see how it works in practise.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        MenuComponent meatMenu = new Menu(&quot;Carnes&quot;, &quot;Cardapio de carnes nobres&quot;);
        MenuComponent soupMenu = new Menu(&quot;Sopas&quot;, &quot;Cardapio de sopas vegetárianas&quot;);
        MenuComponent coffeeMenu = new Menu(&quot;Cafes&quot;, &quot;Cardapio de café&quot;);
        MenuComponent allMenusTogether = new Menu(&quot;Todos cardapios&quot;, &quot;Todas ofertas do nosso restaurante&quot;);
        // ADD MENU&#39;S TO THE GENERAL MENU
        allMenusTogether.add(meatMenu);
        allMenusTogether.add(soupMenu);
        allMenusTogether.add(coffeeMenu);
        // ADD FOOD TO THE MEAT MENU
        String name = &quot;Roast beef&quot;;
        String description = &quot;Delicious roast beef argentino&quot;;
        boolean vegetarian = false;
        double price = 12.99;
        final Food roastBeef = new Food(name,description,vegetarian, price);
        meatMenu.add(roastBeef);
        // ADD FOOD TO THE SOUP MENU
        name = &quot;Sopa tomate&quot;;
        description = &quot;Deliciosa sopa de tomate com oregano e creme de leite&quot;;
        vegetarian = true;
        price = 8.50;
        final Food tomatoSoup = new Food(name,description,vegetarian, price);
        soupMenu.add(tomatoSoup);
        // ORDER MENU&#39;S FROM BARMAID (BARMAID SHOWS THE AVAILABLE MENU&#39;S)
        final Barmaid barmaid = new Barmaid(allMenusTogether);
        barmaid.showMenus();
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all. Hope you like it!
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/4912545406219185848'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/4912545406219185848'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-composite.html' title='Programming Design Pattern - Composite Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEii6hZn53JlOtowZxyhlA2cVqcPSGNmo39Q4Kns10AR9c-IYpQiPxrWygNFNNC42EMeH_8SZnhiyFq8LmjnFkGDcY9z05HDZuLgtFLRu15I9DQIcMtCWycNtxzk0P_YulWCNIJoDWMoxlIa/s72-c/composite.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-5636338730406878291</id><published>2014-08-20T11:51:00.004-07:00</published><updated>2020-10-12T12:39:47.527-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="facade"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Facade Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Facade design 
pattern in action. The Facade design pattern is a very useful 
programming design pattern &lt;span style=&quot;background-color: orange;&quot;&gt;when you are trying to simplify a lot of communication and actions between a bunch of classes or objects&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Facade Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCYEMjjMOyB_bY_0ezwxfuux9JkxVOLSN18Jha5yyPNSAsJ4HbkyOiudjHJkaleivJGAjRiW_Do4APWtfxoFB7Q0GYnfqeGZ4kZverKcQaUoZ-sSwgyJ74qsC8h-37WaGSHaQvVQTmyHWS/s1600/facade.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;154&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCYEMjjMOyB_bY_0ezwxfuux9JkxVOLSN18Jha5yyPNSAsJ4HbkyOiudjHJkaleivJGAjRiW_Do4APWtfxoFB7Q0GYnfqeGZ4kZverKcQaUoZ-sSwgyJ74qsC8h-37WaGSHaQvVQTmyHWS/s1600/facade.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s1600/flyweight.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could simplify the turn ON and OFF process of a stereo home cinema. &lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span class=&quot;hps&quot;&gt;Imagine&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;you bought&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;a stereo&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;home cinema&lt;/span&gt;. &lt;span class=&quot;hps&quot;&gt;Instead of&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;connecting all your components&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;every time&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;you watch&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;a movie&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;we will&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;simplify&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;this process by using&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;a facade.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The components&lt;/h3&gt;
Typically, the facade coordinates the communication between lots of objects making it simple to use it. For that reason, let´s define a lot of components that communicates together to simulate a situation like that. What we have here are home cinema devices.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Light {
    public void on(){System.out.println(&quot;light on&quot;);}
    public void off(){System.out.println(&quot;light off&quot;);}
    public void dim(){System.out.println(&quot;dimming lights down&quot;);}
}
public class WatchScreen {
    public void down(){System.out.println(&quot;Sliding screen down&quot;);}
    public void high(){System.out.println(&quot;Sliding screen high&quot;);}
}
public class PopcornMaschine {
    public void on(){System.out.println(&quot;popcorn maschine on&quot;);}
    public void off(){System.out.println(&quot;popcorn maschine off&quot;);}
    public void start(){System.out.println(&quot;preparing popcorn...&quot;);}
}
public class CdPlayer {
    private Amplifier amplifier;
    public void on(){System.out.println(&quot;CD Player on!&quot;);}
    public void off(){System.out.println(&quot;CD Player off!&quot;);}
    public void eject(){System.out.println(&quot;Eject&quot;);}
    public void pause(){System.out.println(&quot;Pause&quot;);}
    public void play(){System.out.println(&quot;play&quot;);}
    public void stop(){System.out.println(&quot;stop&quot;);}
}
public class Tuner {
    private Amplifier amplifier;
    public void on(){System.out.println(&quot;Tuner on!&quot;);}
    public void off(){System.out.println(&quot;Tuner off!&quot;);}
    public void setFM(){System.out.println(&quot;FM set&quot;);}
    public void setAM(){System.out.println(&quot;AM set&quot;);}
    public void setChannel(double frequency){System.out.println(&quot;Channel: &quot;+frequency);}
}
public class DvdPlayer {
    public void on(){System.out.println(&quot;DVD Player on!&quot;);}
    public void off(){System.out.println(&quot;DVD Playerner off!&quot;);}
    public void eject(){System.out.println(&quot;Eject&quot;);}
    public void pause(){System.out.println(&quot;Pause&quot;);}
    public void play(){System.out.println(&quot;play&quot;);}
    public void stop(){System.out.println(&quot;stop&quot;);}
    public void setSurroundSound(){System.out.println(&quot;Surround Sound mode&quot;);}
    public void setTwoChannelSound(){System.out.println(&quot;Two channel sound mode&quot;);}
}
public class Amplifier {
    private Tuner tuner;
    private DvdPlayer dvdPlayer;
    private CdPlayer cdPlayer;
    
    public void on(){System.out.println(&quot;Amplifier on!&quot;);}
    public void off(){System.out.println(&quot;Amplifier off!&quot;);}
    public void setCD(){System.out.println(&quot;setting CD&quot;);}
    public void setDVD(){System.out.println(&quot;setting DVD&quot;);}
    public void setStereoSound(){System.out.println(&quot;setting stereo sound&quot;);}
    public void setSurroundSound(){System.out.println(&quot;settting surround sound&quot;);}
    public void setTuner(){System.out.println(&quot;setting Tuner&quot;);}
    public void setVolume(){System.out.println(&quot;setting volume&quot;);}
    
}
public class Beamer {
    private DvdPlayer dvdPlayer;
    public void on(){System.out.println(&quot;Projetor on&quot;);}
    public void off(){System.out.println(&quot;Projetor off&quot;);}
    public void tvMode(){System.out.println(&quot;TV mode&quot;);}
    public void wideScreenMode(){System.out.println(&quot;Wide screen mode&quot;);}
}
&lt;/pre&gt;
&lt;h3&gt;
The Facade&lt;/h3&gt;
Instead of switching all devices ON and OFF (in the right sequences) we use the facade. All we have to do now, is to call start and stop watching movie. All needed communication and actions behind the scene will be handled by the facade itself. We don&#39;t care about it anymore.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class HomeCinemaFacade {
    private Amplifier amplifier;
    private Tuner tuner;
    private DvdPlayer dvdPlayer;
    private CdPlayer cdPlayer;
    private Beamer beamer;
    private Light light;
    private PopcornMaschine popcorn;
    private WatchScreen screen;
    
    public HomeCinemaFacade(Amplifier amplifier, Tuner tuner, DvdPlayer dvdPlayer,
            CdPlayer cdPlayer, Beamer beamer, Light light, PopcornMaschine popcorn,
            WatchScreen screen) {
        super();
        this.amplifier = amplifier;
        this.tuner = tuner;
        this.dvdPlayer = dvdPlayer;
        this.cdPlayer = cdPlayer;
        this.beamer = beamer;
        this.light = light;
        this.popcorn = popcorn;
        this.screen = screen;
    }
    
    public void startWatchingMovie(){
        System.out.println(&quot;Se prepare! O filme ja vai começar!&quot;);
        popcorn.on();
        popcorn.start();
        light.dim();
        screen.down();
        beamer.on();
        beamer.wideScreenMode();
        amplifier.on();
        amplifier.setDVD();
        amplifier.setSurroundSound();
        amplifier.setVolume();
        dvdPlayer.on();
        dvdPlayer.play();
    }
    public void stopWatchingMovie(){
        System.out.println(&quot;Desligando Home-Cinema! aguarde...&quot;);
        popcorn.off();
        light.on();
        screen.high();
        beamer.off();
        amplifier.off();
        dvdPlayer.pause();
        dvdPlayer.eject();
        dvdPlayer.off();
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Finally, let´s watch some movies using our new facade. No more complicated turn ON and OFF processes anymore ;)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        final HomeCinemaFacade homeCinemaFacade = new HomeCinemaFacade(new Amplifier(),
                new Tuner(), new DvdPlayer(), new CdPlayer(), new Beamer(), new Light(),
                new PopcornMaschine(), new WatchScreen());
        // SIMULATING START OF A MOVIE
        homeCinemaFacade.startWatchingMovie();
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/5636338730406878291'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/5636338730406878291'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-facade.html' title='Programming Design Pattern - Facade Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCYEMjjMOyB_bY_0ezwxfuux9JkxVOLSN18Jha5yyPNSAsJ4HbkyOiudjHJkaleivJGAjRiW_Do4APWtfxoFB7Q0GYnfqeGZ4kZverKcQaUoZ-sSwgyJ74qsC8h-37WaGSHaQvVQTmyHWS/s72-c/facade.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-3024354704341986170</id><published>2014-08-20T11:28:00.004-07:00</published><updated>2020-10-12T12:34:18.337-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="abstract factory"/><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Abstract Factory Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Abstract Factory design 
pattern in action. The Abstract Factory design pattern is a very useful 
programming design pattern while &lt;span style=&quot;background-color: orange;&quot;&gt;dealing with creation of default, complex product families&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Abstract Factory Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyzwR-mwoOwLsyPEA7pvYpVOdOGLG_ZwjaPK1mALfiwg6JhQN18Li0iGtLZWEgzLz-iMKzsNaT_FJ_E5AdnJrZDbDIcEXqpVgSyucLMMsohb7Kb_CUqgDe9NOPt3jchf4PNyW6FBHn3cac/s1600/abstractFactory.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;242&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyzwR-mwoOwLsyPEA7pvYpVOdOGLG_ZwjaPK1mALfiwg6JhQN18Li0iGtLZWEgzLz-iMKzsNaT_FJ_E5AdnJrZDbDIcEXqpVgSyucLMMsohb7Kb_CUqgDe9NOPt3jchf4PNyW6FBHn3cac/s1600/abstractFactory.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s1600/flyweight.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In
this example we will build on the last post (&lt;a href=&quot;http://cleancodedevelopment-qualityseal.blogspot.com.br/2014/08/programming-design-pattern-factory.html&quot; target=&quot;_blank&quot;&gt;Factory&lt;/a&gt;) and create coffee shops. Let&#39;s imagine that our coffee shop is growing up. &lt;span id=&quot;result_box&quot; lang=&quot;en&quot;&gt;&lt;span class=&quot;hps&quot;&gt;We&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;wanna create&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;branches&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;of our company&lt;/span&gt;. &lt;span class=&quot;hps&quot;&gt;Imagine that&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;our business&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;is expanding&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;we are opening&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;branches in&lt;/span&gt; &lt;span class=&quot;hps&quot;&gt;other countries&lt;/span&gt;&lt;/span&gt;. That&#39;s what we will see here.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Abstract Factory&lt;/h3&gt;
&lt;span style=&quot;background-color: orange;&quot;&gt;Make sure you have done the example from the factory&lt;/span&gt; (see &lt;a href=&quot;http://cleancodedevelopment-qualityseal.blogspot.com.br/2014/08/programming-design-pattern-factory.html&quot; target=&quot;_blank&quot;&gt;Factory&lt;/a&gt;), because we are building up on it.
We use the same generic interface, but we pass now a CoffeeFactory to it. Pay close attention how simple and how clean it is! I love this concept.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;import java.math.BigDecimal;

/**
 * abstract coffee shop factory
 * @author Ricardo Ferreira
 * @version 1.0
 * @since 08/04/2014
 */
public enum CoffeeShopFactory implements Factory &amp;lt; Coffee, CoffeeFactory &amp;gt; {
    STARBUCKS_BRASIL, STARBUCKS_ASIA, STARBUCKS_USA, STARBUCKS_EUROPE;
    @Override
    public Coffee create(CoffeeFactory... arg) {
        Coffee coffee = validate(arg);
        switch (this) {
        case STARBUCKS_ASIA:
            return createAsiaStarbuckCoffee(coffee);
        case STARBUCKS_BRASIL:
            return createBrasilStarbuckCoffee(coffee);
        case STARBUCKS_USA:
            return createUSAStarbuckCoffee(coffee);
        case STARBUCKS_EUROPE:
            return createEuropeStarbuckCoffee(coffee);
        default:
            return new LatteMachiatto(new BigDecimal(&quot;6.0&quot;));
        }
    }

    private Coffee validate(CoffeeFactory[] arg) {
        if(!(arg.length &amp;gt; 0)){
            throw new IllegalArgumentException(&quot;CoffeeShopFactory needs a CoffeeFactory type&quot;);
        }
        return arg[0].create();
    }
    private Coffee createAsiaStarbuckCoffee(Coffee coffee) {
        coffee.addIngredient(&quot;no zugar&quot;, new BigDecimal(&quot;-1.0&quot;));
        coffee.addIngredient(&quot;marger milk&quot;, new BigDecimal(&quot;4.0&quot;));
        coffee.addIngredient(&quot;caramel&quot;, new BigDecimal(&quot;2.0&quot;));
        return coffee;
    }
    private Coffee createBrasilStarbuckCoffee(Coffee coffee) {
        coffee.addIngredient(&quot;extra zugar&quot;, new BigDecimal(&quot;2.0&quot;));
        coffee.addIngredient(&quot;no milk&quot;, new BigDecimal(&quot;-4.0&quot;));
        coffee.addIngredient(&quot;choco&quot;, new BigDecimal(&quot;3.0&quot;));
        return coffee;
    }
    private Coffee createUSAStarbuckCoffee(Coffee coffee) {
        coffee.addIngredient(&quot;sweetener&quot;, new BigDecimal(&quot;5.0&quot;));
        coffee.addIngredient(&quot;soja milk&quot;, new BigDecimal(&quot;7.0&quot;));
        coffee.addIngredient(&quot;no caramel&quot;, new BigDecimal(&quot;-2.0&quot;));
        return coffee;
    }
    private Coffee createEuropeStarbuckCoffee(Coffee coffee) {
        coffee.addIngredient(&quot;no zugar&quot;, new BigDecimal(&quot;-1.0&quot;));
        coffee.addIngredient(&quot;normal milk&quot;, new BigDecimal(&quot;2.5&quot;));
        coffee.addIngredient(&quot;chantilly&quot;, new BigDecimal(&quot;4.5&quot;));
        return coffee;
    }
}
&lt;/pre&gt;
&lt;h3&gt;
Testing it&lt;/h3&gt;
Finally, we can see the Abstract Factory in action. I love this concept. It makes things readable, simple and reusable.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        // create coffe without args
        final Coffee simpleLatteMachiato = CoffeeFactory.LATTE_MACHIATTO.create();
        simpleLatteMachiato.addIngredient(&quot;choco&quot;, new BigDecimal(&quot;0.3&quot;));
        simpleLatteMachiato.addIngredient(&quot;zugar&quot;, new BigDecimal(&quot;0.4&quot;));
        simpleLatteMachiato.addIngredient(&quot;chantilly&quot;, new BigDecimal(&quot;0.6&quot;));
        simpleLatteMachiato.removeIngredient(&quot;choco&quot;);
        System.out.println(&quot;Latte Machiatto: &quot;+simpleLatteMachiato.getTotalPrice());
        // Create coffee using args
        final Coffee simpleExpresso = CoffeeFactory.EXPRESSO.create(new BigDecimal(&quot;4.0&quot;));
        simpleExpresso.addIngredient(&quot;choco&quot;, new BigDecimal(&quot;0.3&quot;));
        simpleExpresso.addIngredient(&quot;zugar&quot;, new BigDecimal(&quot;0.2&quot;));
        simpleExpresso.addIngredient(&quot;chantilly&quot;, new BigDecimal(&quot;1.2&quot;));
        System.out.println(&quot;Expresso: &quot;+simpleExpresso.getTotalPrice());
        
        Coffee coffee;
        coffee = CoffeeShopFactory.STARBUCKS_EUROPE.create(CoffeeFactory.CAPUCCINO);
        System.out.println(&quot;STARBUCKS_EUROPE CAPPUCCINO: &quot;+coffee.getTotalPrice());
        
        coffee = CoffeeShopFactory.STARBUCKS_BRASIL.create(CoffeeFactory.CAPUCCINO);
        System.out.println(&quot;STARBUCKS_BRASIL CAPPUCCINO: &quot;+coffee.getTotalPrice());
        
        coffee = CoffeeShopFactory.STARBUCKS_USA.create(CoffeeFactory.CAPUCCINO);
        System.out.println(&quot;STARBUCKS_USA CAPPUCCINO: &quot;+coffee.getTotalPrice());
        
        coffee = CoffeeShopFactory.STARBUCKS_ASIA.create(CoffeeFactory.CAPUCCINO);
        System.out.println(&quot;STARBUCKS_ASIA CAPPUCCINO: &quot;+coffee.getTotalPrice());
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it.
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/3024354704341986170'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/3024354704341986170'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-abstract.html' title='Programming Design Pattern - Abstract Factory Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyzwR-mwoOwLsyPEA7pvYpVOdOGLG_ZwjaPK1mALfiwg6JhQN18Li0iGtLZWEgzLz-iMKzsNaT_FJ_E5AdnJrZDbDIcEXqpVgSyucLMMsohb7Kb_CUqgDe9NOPt3jchf4PNyW6FBHn3cac/s72-c/abstractFactory.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-1494365018080928763</id><published>2014-08-20T11:14:00.002-07:00</published><updated>2020-10-12T12:33:04.248-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="Factory"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="simple factory"/><title type='text'>Programming Design Pattern - Factory Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the Factory design 
pattern in action. The Factory design pattern is a very useful 
programming design pattern while &lt;span style=&quot;background-color: orange;&quot;&gt;dealing with creation of default, complex objects&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Factory Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;288&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s1600/factoryDecorator.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s1600/flyweight.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
In

 our example we will see, how we could implement a coffeshop like starbucks. We may think that&#39;s a complicated think, but in fact it is not. With the right design it can be very simple. Imagine you are grounding your coffee company. You want to offer lots of different coffee types. How could we do it? In this example we will see it.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Product&lt;/h3&gt;
Normally we will start by the products we have. That&#39;s a typical characteristic and keyword for the simple factory. All concrete coffee types will extend from it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;// DEFINE COMMON PRODUCT INTERFACE
public abstract class Coffee {
    private BigDecimal basePrice= BigDecimal.ZERO;
    private HashMap &amp;lt; String, BigDecimal &amp;gt; addOns;
    public Coffee(BigDecimal basePrice){
        addOns =  new HashMap &amp;lt; String, BigDecimal &amp;gt; ( );
        this.basePrice=basePrice;
    }
    public BigDecimal  getTotalPrice(){return this.basePrice;}
    public void addIngredient(String name, BigDecimal price){
        this.basePrice=this.basePrice.add(price);
        addOns.put(name, price);
    }
    public void removeIngredient(String name){
        final BigDecimal price = addOns.get(name);
        this.basePrice=this.basePrice.subtract(price);
        addOns.remove(name);
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Coffee types (products)&lt;/h3&gt;
Here we implement all coffees we intend to offer.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;import java.math.BigDecimal;

public class Cappuccino extends Coffee {
    public Cappuccino(BigDecimal basePrice) {
        super(basePrice);
    }
}
public class LatteMachiatto extends Coffee {
    public LatteMachiatto(BigDecimal basePrice) {
        super(basePrice);
    }
}
public class Intenso extends Coffee {
    public Intenso(BigDecimal basePrice) {
        super(basePrice);
    }
}
public class Expresso extends Coffee {
    public Expresso(BigDecimal basePrice) {
        super(basePrice);
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The factory&#39;s common interface&lt;/h3&gt;
Here we define the contract from the factory. Every factory implements it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;/**
 * Common interface to be used while creating simple factories.
 * @param P product to be created from this factory. The interface from the product.
 * @param A argument: an optional creation attribute. You may leave it empty.
 * @author Ricardo Ferreira
 * @version 1.0
 * @since 08/04/2014
 */
public interface Factory &amp;lt; P , A &amp;gt; {
   /**
    * creates a default product without argument or a specific product if you pass any arguments
    * @param argument an optional creation attribute
    * @return a default concrete product
    */
    P create(@SuppressWarnings(&quot;unchecked&quot;) A... arg);
}
&lt;/pre&gt;
&lt;h3&gt;
The Factory itself&lt;/h3&gt;
A new state of the art is to use enum&#39;s to create factories. Why? Because it is easier, cleaner and can be self-referenced as we will see.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;/**
 * Simple coffee factory
 * @author Ricardo Ferreira
 * @version 1.0
 * @since 08/04/2014
 */
public enum CoffeeFactory implements Factory &amp;lt; Coffee, BigDecimal &amp;gt; {
    CAPUCCINO, LATTE_MACHIATTO, EXPRESSO, INTENSO;
    @Override
    public Coffee create(BigDecimal... arg) {
        boolean hasArg = arg.length &amp;gt; 0;
        switch (this) {
        case LATTE_MACHIATTO:
            return hasArg?new LatteMachiatto(arg[0]):new LatteMachiatto(new BigDecimal(&quot;2.0&quot;));
        case CAPUCCINO:
            return hasArg?new Cappuccino(arg[0]):new Cappuccino(new BigDecimal(&quot;3.0&quot;));
        case EXPRESSO:
            return hasArg?new Expresso(arg[0]):new Expresso(new BigDecimal(&quot;4.0&quot;));
        case INTENSO:
            return hasArg?new Intenso(arg[0]):new Intenso(new BigDecimal(&quot;5.0&quot;));
        default:
            return new LatteMachiatto(new BigDecimal(&quot;6.0&quot;));
        }
    }
}
&lt;/pre&gt;
&lt;h3&gt;
The Test&lt;/h3&gt;
Finally, we can see how our new coffee shop factory would looks like.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        // create coffe without args
        final Coffee simpleLatteMachiato = CoffeeFactory.LATTE_MACHIATTO.create();
        simpleLatteMachiato.addIngredient(&quot;choco&quot;, new BigDecimal(&quot;0.3&quot;));
        simpleLatteMachiato.addIngredient(&quot;zugar&quot;, new BigDecimal(&quot;0.4&quot;));
        simpleLatteMachiato.addIngredient(&quot;chantilly&quot;, new BigDecimal(&quot;0.6&quot;));
        simpleLatteMachiato.removeIngredient(&quot;choco&quot;);
        System.out.println(&quot;Latte Machiatto: &quot;+simpleLatteMachiato.getTotalPrice());
        // Create coffee using args
        final Coffee simpleExpresso = CoffeeFactory.EXPRESSO.create(new BigDecimal(&quot;4.0&quot;));
        simpleExpresso.addIngredient(&quot;choco&quot;, new BigDecimal(&quot;0.3&quot;));
        simpleExpresso.addIngredient(&quot;zugar&quot;, new BigDecimal(&quot;0.2&quot;));
        simpleExpresso.addIngredient(&quot;chantilly&quot;, new BigDecimal(&quot;1.2&quot;));
        System.out.println(&quot;Expresso: &quot;+simpleExpresso.getTotalPrice());
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/1494365018080928763'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/1494365018080928763'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-factory.html' title='Programming Design Pattern - Factory Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQzf1wNCZkMF6ujarkki3nKII8QqZ6vSi30Ev1mGqz0kFbwbKm4C0ljGbfKZtZd9UAVILk6zsShGvvbf1lxACm4HrEUd974E-gbyu316jS0FziVsBTz5x92iUTxDvH8SLT0TmTd5Lwy7U/s72-c/factoryDecorator.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-911940978117917046</id><published>2014-08-20T10:53:00.002-07:00</published><updated>2020-10-12T12:40:03.484-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="flyweight"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Programming Design Pattern - Flyweight Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the flyweight design 
pattern in action. The flyweight design pattern is a very useful 
programming design pattern while &lt;span style=&quot;background-color: orange;&quot;&gt;dealing with a lot of tiny, re-usable objects&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
I&#39;m
 assuming here you already know the concepts and i&#39;ll be focusing on 
practise. The example i will provide is a nice way to show it how it 
could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Flyweight Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close 
attention, because once you understand that, everything will become 
clear and simple to understand. That&#39;s the reason I&#39;m putting always the
 UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s1600/flyweight.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;190&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s1600/flyweight.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
In
 our example we will see, how we could use the flyweight pattern while 
dealing with lots of digits. Imagine you are writing a huge text file with a single big prime number. (memory issue) This could be a document with a few pages. All you need are digits to write the big prime number in it. So, you may create an object for every single digit, or you may create only 10 (0-9) and re-use it in the whole document. That&#39;s what we will do here. Pay close attention. Imagine how many other scenarios like this you may have in your applications.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
The Flyweights&lt;/h3&gt;
Imagine that those flyweights bellow are reusable classes like the numbers 0-9. If you want to write a text or equation, you&#39;ll be able to reuse it instead of creating a new object, every time you write a digit.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public interface FlyweightField {
    public String getValue();
}
public class One implements FlyweightField {
    @Override
    public String getValue() {return &quot; 1 &quot;;}
}
public class Two implements FlyweightField {
    @Override
    public String getValue() {return &quot; 2 &quot;;}
}
public class Three implements FlyweightField {
    @Override
    public String getValue() {return &quot; 3 &quot;;}
}
public class Four implements FlyweightField {
    @Override
    public String getValue() {return &quot; 4 &quot;;}
}
public class Five implements FlyweightField {
    @Override
    public String getValue() {return &quot; 5 &quot;;}
}
public class Six implements FlyweightField {
    @Override
    public String getValue() {return &quot; 6 &quot;;}
}
public class Seven implements FlyweightField {
    @Override
    public String getValue() {return &quot; 7 &quot;;}
}
public class Eight implements FlyweightField {
    @Override
    public String getValue() {return &quot; 8 &quot;;}
}
public class Nine implements FlyweightField {
    @Override
    public String getValue() {return &quot; 9 &quot;;}
}
&lt;/pre&gt;
&lt;h3&gt;
The number types&lt;/h3&gt;
That&#39;s the class which represents the types we just defined above. It will be used in the FlyweightFactory bellow.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public enum FieldType {_1,_2,_3,_4,_5,_6,_7,_8,_9;}

public class FlyweightFieldFactory {
    private static FlyweightFieldFactory INSTANCE =  new FlyweightFieldFactory();
    private Map &amp;lt; FieldType, FlyweightField &amp;gt; fields =  new HashMap &amp;lt; FieldType, FlyweightField &amp;gt; ( );
    private FlyweightFieldFactory(){/*SINGLETON*/
        fields.put(FieldType._1, new One());
        fields.put(FieldType._2, new Two());
        fields.put(FieldType._3, new Three());
        fields.put(FieldType._4, new Four());
        fields.put(FieldType._5, new Five());
        fields.put(FieldType._6, new Six());
        fields.put(FieldType._7, new Seven());
        fields.put(FieldType._8, new Eight());
        fields.put(FieldType._9, new Nine());
    }
    public static FlyweightFieldFactory instance(){return INSTANCE;}
    public FlyweightField get(FieldType type){
        return fields.get(type);
    }
}
&lt;/pre&gt;
&lt;h3&gt;
Testing it&lt;/h3&gt;
Finally the test. Here we can see a possible usage of it. The interesting thing is the fact that we are reusing the same instances instead of creating new objects.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        FlyweightField one1 = FlyweightFieldFactory.instance().get(FieldType._1);
        FlyweightField one2 = FlyweightFieldFactory.instance().get(FieldType._1);
        FlyweightField one3 = FlyweightFieldFactory.instance().get(FieldType._1);
        FlyweightField two1 = FlyweightFieldFactory.instance().get(FieldType._2);
        FlyweightField two2 = FlyweightFieldFactory.instance().get(FieldType._3);
        FlyweightField five1 = FlyweightFieldFactory.instance().get(FieldType._5);
        FlyweightField nine1 = FlyweightFieldFactory.instance().get(FieldType._9);
        System.out.println(one1.getValue());
        System.out.println(one2.getValue());
        System.out.println(one3.getValue());
        System.out.println(two1.getValue());
        System.out.println(two2.getValue());
        System.out.println(five1.getValue());
        System.out.println(nine1.getValue());
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all. Hope you like it!
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/911940978117917046'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/911940978117917046'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-flyweight.html' title='Programming Design Pattern - Flyweight Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI5xlUV0eP8hRr7qZRBE4X49hg3GoZWb5erZ1ZoSauJWgkageKP6Qh1j337piSn1V_oZZNPePhLkNAufBORUu_LEn4pejs0ES2QLRR9WtgdLQXUx5TXT9m-knYhBWNO2SYrJ-atK9YEoWc/s72-c/flyweight.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-582121105715363092.post-4049105889493744924</id><published>2014-08-20T10:04:00.003-07:00</published><updated>2020-10-12T12:40:10.968-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="clean code"/><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="howto"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="prototype"/><title type='text'>Programming Design Pattern - Prototype Pattern Applied - Best Practise</title><content type='html'>Hi there!&lt;br /&gt;
&lt;br /&gt;
Today i&#39;m gonna show the prototype design pattern in action. The prototype design pattern is a very useful programming design pattern while dealing with games and complex objects.&lt;br /&gt;
&lt;br /&gt;
I&#39;m assuming here you already know the concepts and i&#39;ll be focusing on practise. The example i will provide is a nice way to show it how it could looks like. &lt;span style=&quot;background-color: orange;&quot;&gt;You can always come back here, take it, adapt it and use it in your applictions as you may need&lt;/span&gt;. So be sure you bookmark it or join the group here on the right side of this post subscribing it.&lt;br /&gt;
&lt;br /&gt;
First of all, let&#39;s take a look at the UML diagram of it. After that we will take the analogy for our example.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The UML Diagram of the Prototype Pattern&lt;/h3&gt;
&lt;br /&gt;
Pay close attention, because once you understand that, everything will become clear and simple to understand. That&#39;s the reason I&#39;m putting always the UML first. That way you&#39;ll get an eye for it with the time.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuLZvcQ81Ca1WqLslu2_5NaLJ5biOXZJG1LFdzwQY063EF41HzpH55wH4Chc4FvI1daE64SOwl6eT7K68L_GSikG1_5YhDHATA-5DeBADx0c-nWDk3y9wH0UX_4MgLnImgnRJcavkoEWQx/s1600/prototype.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;87&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuLZvcQ81Ca1WqLslu2_5NaLJ5biOXZJG1LFdzwQY063EF41HzpH55wH4Chc4FvI1daE64SOwl6eT7K68L_GSikG1_5YhDHATA-5DeBADx0c-nWDk3y9wH0UX_4MgLnImgnRJcavkoEWQx/s1600/prototype.png&quot; width=&quot;320&quot; /&gt;&amp;nbsp;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The example&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
In our example we will see, how we could use the prototype pattern while dealing with games. Assuming that the instances used there are very complex to create or require a lot of time. (performance issue)&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
The base cloneable&lt;/h3&gt;
That&#39;s the core of it. Note that this class extends from cloneable. That&#39;s typical for prototype.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public abstract class Caracteristica implements Cloneable {
    protected String force,power,webStorage,life,outfit,weakPoint;
    protected String velocity,skill,specialPower,combo;
    public Caracteristica(String force, String power, String webStorage, String life,
            String outfit, String combo, String weakPoint, String velocity, String skill,
            String specialPower) {
        this.force = force;this.power = power;this.webStorage = webStorage;
        this.life = life;this.outfit = outfit;this.combo = combo;
        this.weakPoint = weakPoint;this.velocity = velocity;
        this.skill = skill;this.specialPower = specialPower;
    }
    @Override
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }
}
&lt;/pre&gt;
&lt;h3&gt;
Our Heros&lt;/h3&gt;
All heros we implement, must extend from the base class.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Spiderman extends Caracteristica {
    
    public Spiderman(String force, String power, String webStorage, String life, String outfit,
            String combo, String weakPoint, String velocity, String skill, String specialPower) {
        super(force, power, webStorage, life, outfit, combo, weakPoint, velocity, skill, specialPower);
    }
    public void fire(){
        System.out.println(&quot;fire sticky web&quot;);
    }
    public void showSkills(){
        System.out.println(&quot;Skill: &quot;+ skill);
    }
    // MANY OTHER COMPLEX ACCESSES AND METHODS HERE ...
}
&lt;/pre&gt;
&lt;h3&gt;
Types of heros available&lt;/h3&gt;
Here we define which types of heros we will support in this fictitious game.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public enum Hero {
    BATMAN, SPIDERMAN, SUPERMAN, CAPITAN_AMARICA;
}
&lt;/pre&gt;
&lt;h3&gt;
The Game&lt;/h3&gt;
That&#39;s the game itself. Note that whenever we need a &quot;complex&quot; object of hero, we do not create it from scratch. We just clone it from the register.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Game {
    private static final Game INSTANCE = new Game();
    private Game() {/* SINGLETON */}
    public static Game instance() {return INSTANCE;}
    private static final Map &amp;lt; Hero, Caracteristica &amp;gt; register = new HashMap &amp;lt; Hero, Caracteristica &amp;gt; ( ) {
        private static final long serialVersionUID = 1L;
        {
            put(Hero.SPIDERMAN, new Spiderman(&quot;força&quot;, 
                    &quot;poder&quot;, &quot;armazemDeTeia&quot;, &quot;vida&quot;, &quot;roupa&quot;,
                    &quot;combo&quot;, &quot;pontoFraco&quot;, &quot;velocidade&quot;, 
                    &quot;pode subir nas paredes&quot;, &quot;poderEspecial&quot;));
        }
    };
    public Caracteristica create(Hero hero){
       return (Caracteristica) register.get(hero).clone();
    }
}
&lt;/pre&gt;
&lt;h3&gt;
Testing it&lt;/h3&gt;
Finally, the usage of it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: csharp&quot;&gt;public class Client {
    public static void main(String[] args) {
        final Spiderman spiderman = (Spiderman)Game.instance().create(Hero.SPIDERMAN);
        spiderman.fire();
        spiderman.showSkills();
    }
}
&lt;/pre&gt;
&lt;br /&gt;
That&#39;s all! Hope you like it!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;😱👇&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;IPODS PRO&lt;/span&gt;&lt;/b&gt;&amp;nbsp;😱👇&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Be sure to read, it will change your life!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Show your work by Austin Kleon&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/34NVmwx&quot;&gt;https://amzn.to/34NVmwx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is a must read - it will put you in another level! (Expert)&lt;/div&gt;&lt;div&gt;&lt;b&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/b&gt;:&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/30WQSm2&quot;&gt;https://amzn.to/30WQSm2&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write cleaner code and stand out!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Clean Code - A Handbook of Agile Software Craftsmanship:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33RvaSv&quot;&gt;https://amzn.to/33RvaSv&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book is very practical, straightforward and to the point! Worth every penny!&lt;/div&gt;&lt;div&gt;&lt;b&gt;Kotlin for Android App Development (Developer&#39;s Library):&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/33VZ6gp&quot;&gt;https://amzn.to/33VZ6gp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Needless to say, these are top right?&lt;/div&gt;&lt;div&gt;&lt;b&gt;Apple AirPods Pro:&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fcff01;&quot;&gt;&lt;a href=&quot;https://amzn.to/2GOICxy&quot;&gt;https://amzn.to/2GOICxy&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;😱👆&amp;nbsp;&lt;b&gt;PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO&lt;/b&gt;&amp;nbsp;😱👆&lt;/div&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/4049105889493744924'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/582121105715363092/posts/default/4049105889493744924'/><link rel='alternate' type='text/html' href='http://cleancodedevelopment-qualityseal.blogspot.com/2014/08/programming-design-pattern-prototype.html' title='Programming Design Pattern - Prototype Pattern Applied - Best Practise'/><author><name>Ricardo Ferreira</name><uri>http://www.blogger.com/profile/04461067609927607445</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='25' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJ4oDbtTZ_ez8mcj5tIGpgf9Oz_9j1-UGAW0E9SfgvgD_POFBR08CbctTZsUDkST2_JPoCPE5CrrZxhQZcrhA5fPUDt3tXDC8jMT5ybLg-SRKKzZb6ciX4U-qgmJgvQ/s220/ricardo.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuLZvcQ81Ca1WqLslu2_5NaLJ5biOXZJG1LFdzwQY063EF41HzpH55wH4Chc4FvI1daE64SOwl6eT7K68L_GSikG1_5YhDHATA-5DeBADx0c-nWDk3y9wH0UX_4MgLnImgnRJcavkoEWQx/s72-c/prototype.png" height="72" width="72"/></entry></feed>