<?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-2875775725551936703</id><updated>2026-07-01T21:05:24.895-07:00</updated><category term="Java"/><category term="Behavioral Design Pattern"/><category term="eclipse"/><category term="API"/><category term="JAX-WS"/><category term="SpringBoot"/><category term="Web Services"/><category term="CouchDB"/><category term="Error"/><category term="Exceptions"/><category term="Ide"/><category term="JBoss"/><category term="JSP"/><category term="REST"/><category term="Structural Design Pattern"/><category term="Android"/><category term="Angular"/><category term="Annotations"/><category term="Collections"/><category term="Design Pattern"/><category term="EJB"/><category term="Endpoint"/><category term="Example"/><category term="Geo Coding"/><category term="Hibernate"/><category term="HttpClient"/><category term="JAX-RS"/><category term="JSON"/><category term="Jackson"/><category term="Java Deadlocks"/><category term="Jersey"/><category term="Linux"/><category term="Mac"/><category term="Questions"/><category term="String Manipulation"/><category term="XML"/><category term="Xpath"/><category term="ADB"/><category term="Algorithms"/><category term="Apache2"/><category term="AsyncTask"/><category term="Blogger"/><category term="CMS"/><category term="Class Diagram"/><category term="Constructor"/><category term="Creational Design Pattern"/><category term="DB2"/><category term="Day Communique"/><category term="Dom"/><category term="DriverManager"/><category term="Educational"/><category term="GCP"/><category term="Google Map"/><category term="Google engEdu"/><category term="Guava"/><category term="Install"/><category term="Interfaces"/><category term="J2EE"/><category term="JDBC"/><category term="JDK Tools"/><category term="JDOM"/><category term="JIQ"/><category term="JMS"/><category term="JNDI"/><category term="JPA"/><category term="JVM"/><category term="Java Script"/><category term="JavaIQ"/><category term="Keytool"/><category term="Logic"/><category term="Maven"/><category term="Messaging"/><category term="MySQL"/><category term="N+1 Select"/><category term="Operators"/><category term="PHP"/><category term="PLAY"/><category term="Productivity"/><category term="Referece Page"/><category term="SSL"/><category term="Servlet"/><category term="Sitemap"/><category term="Snippets"/><category term="SpringBoot Issues"/><category term="Synchronization"/><category term="TLD"/><category term="Tags"/><category term="Template"/><category term="Terminal"/><category term="Thread"/><category term="Thymeleaf"/><category term="Tom Baeyens"/><category term="Tomcat"/><category term="Tools"/><category term="TransportPipe"/><category term="TypeScript"/><category term="UML"/><category term="Weblogic 9.1"/><category term="Windows"/><category term="base64"/><category term="jBPM"/><category term="jQuery"/><category term="wsimport"/><title type='text'>JavaExp: Java and Open Source Technology</title><subtitle type='html'>Developer and Software Architecture Blog containing code snippets and knowledge gained from development experiences.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>117</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-5630336448558789922</id><published>2020-05-26T17:24:00.002-07:00</published><updated>2020-05-26T17:24:49.170-07:00</updated><title type='text'>Different ways to initialize arrays in java</title><content type='html'>&lt;br /&gt;
&lt;article id=&quot;hansini&quot; role=&quot;article&quot;&gt; &lt;input id=&quot;lsid&quot; type=&quot;hidden&quot; value=&quot;508022&quot; /&gt; &lt;div class=&quot;summary&quot; style=&quot;display: none;&quot;&gt;Different ways to initialize array&#39;s in java&lt;/div&gt;&lt;h2 id=&quot;a&quot;&gt;Array Initialization in Java&lt;/h2&gt;&lt;div class=&quot;description&quot;&gt;Different ways to initialize array&#39;s in java&lt;/div&gt;&lt;div class=&quot;txt_desc&quot;&gt;Java Arrays can be populated in a number of ways following summarizes different options.&lt;/div&gt;&lt;div class=&quot;sscfragment&quot;&gt;&lt;div class=&quot;headerblock&quot;&gt;&lt;span class=&quot;title&quot;&gt;One item at a time using a loop&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;bodyblock&quot;&gt;&lt;div class=&quot;description&quot;&gt;Following example we are declaring rolNumms array of size 10 and adding one item at a time using a for a loop.&lt;/div&gt;&lt;pre&gt;&lt;code&gt;  //Using For loops
   int rollNums[] = new int[10];
   for (int i=0; i&lt;rollnums .length=&quot;&quot; 1=&quot;&quot; code=&quot;&quot; i=&quot;&quot; rollnums=&quot;&quot;&gt;&lt;/rollnums&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;sscfragment&quot;&gt;&lt;div class=&quot;headerblock&quot;&gt;&lt;span class=&quot;title&quot;&gt;At the Time of Declaration&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;bodyblock&quot;&gt;&lt;div class=&quot;description&quot;&gt;We can set the content of the array while declaring the array it self. Both the syntax will result in initializing the array with 10 items. Note that when we provide the initializer while declaring the size of the array cannot be specified.&lt;/div&gt;&lt;pre&gt;&lt;code&gt;int rollNums2[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};
int rollNums3[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;sscfragment&quot;&gt;&lt;div class=&quot;headerblock&quot;&gt;&lt;span class=&quot;title&quot;&gt;Arrays.setAll() and Generator function&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;bodyblock&quot;&gt;&lt;div class=&quot;description&quot;&gt;We can set the content of the array using the  Arrays.setAll() method as well.&lt;/div&gt;&lt;pre&gt;&lt;code&gt;int rollNums4 [] = new int [10];
Arrays.setAll(rollNums4, p -&amp;gt; p );&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;sscfragment&quot;&gt;&lt;div class=&quot;headerblock&quot;&gt;&lt;span class=&quot;title&quot;&gt;Copying another array&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;bodyblock&quot;&gt;&lt;div class=&quot;description&quot;&gt;We can also copy an existing array to populate a new array, using java.util.Arrays.copyOf method&lt;/div&gt;&lt;pre&gt;&lt;code&gt;int rollNums5[] = Arrays.copyOf(rollNums4, rollNums4.length);&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/article&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/5630336448558789922/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2020/05/different-ways-to-initialize-arrays-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/5630336448558789922'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/5630336448558789922'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2020/05/different-ways-to-initialize-arrays-in.html' title='Different ways to initialize arrays in java'/><author><name>Sidd Bhattacharjee</name><uri>http://www.blogger.com/profile/10537634667477180972</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-1950534160341125899</id><published>2019-10-11T18:27:00.003-07:00</published><updated>2019-10-12T12:37:16.941-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Mac"/><category scheme="http://www.blogger.com/atom/ns#" term="Productivity"/><title type='text'>Determine OS version from Command line in MacOS</title><content type='html'>Sometimes we would like to know about the installed MacOS from Terminal. Or say we are writing scripts to extract the OS related information like it&#39;s version and release name etc.&lt;br /&gt;
&lt;br /&gt;
For Mac we have couple of options&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;&lt;b&gt;System Profiler&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;Execute the following command from Terminal&lt;/span&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
system_profiler

&lt;/code&gt;&lt;/pre&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/AVvXsEiL2h6gPbuq9JL8JQNkuUvEjmkeU9Hnx7CgQdeE0Jy-y3n3dUhfR-T8Hx8VcJSEl_kuCL6p73gVfyeN-cuMf0Cbgk8z4S73-6VzS2oxaN4KFFRZwt2vf6Vbwmnj8XGntJItcOJf-Ue5tLg/s1600/Screen+Shot+2019-10-11+at+6.23.09+PM.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;510&quot; data-original-width=&quot;1600&quot; height=&quot;203&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiL2h6gPbuq9JL8JQNkuUvEjmkeU9Hnx7CgQdeE0Jy-y3n3dUhfR-T8Hx8VcJSEl_kuCL6p73gVfyeN-cuMf0Cbgk8z4S73-6VzS2oxaN4KFFRZwt2vf6Vbwmnj8XGntJItcOJf-Ue5tLg/s640/Screen+Shot+2019-10-11+at+6.23.09+PM.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;As you can see from the screenshot system_profiler will print a lot of information about the Software and Hardware of the Mac.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;This is it.....&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;b&gt;sw_vers&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;Execute the following command from Terminal&lt;/span&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
sw_vers&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&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/AVvXsEju35KFthbfHcKjoHEr-UBtQgBGvMTa7q1OstVxjRV9820K4SocFNoM4y2WJIn0u-BcF3r3FnsbOgt_e0BJh1KpOCas0E1U_cqWblVOPhh1cyvhqqviIqNfe3XaMuOS-j3h0nEK0yQ1YCk/s1600/Screen+Shot+2019-10-11+at+6.26.08+PM.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;339&quot; data-original-width=&quot;1600&quot; height=&quot;134&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEju35KFthbfHcKjoHEr-UBtQgBGvMTa7q1OstVxjRV9820K4SocFNoM4y2WJIn0u-BcF3r3FnsbOgt_e0BJh1KpOCas0E1U_cqWblVOPhh1cyvhqqviIqNfe3XaMuOS-j3h0nEK0yQ1YCk/s640/Screen+Shot+2019-10-11+at+6.26.08+PM.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
As you can see from the screenshot sw_vers will print information about the current 
installed OS Name and Version.

&lt;span style=&quot;background-color: white; color: #111111; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/1950534160341125899/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/10/determine-os-version-from-command-line.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/1950534160341125899'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/1950534160341125899'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/10/determine-os-version-from-command-line.html' title='Determine OS version from Command line in MacOS'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiL2h6gPbuq9JL8JQNkuUvEjmkeU9Hnx7CgQdeE0Jy-y3n3dUhfR-T8Hx8VcJSEl_kuCL6p73gVfyeN-cuMf0Cbgk8z4S73-6VzS2oxaN4KFFRZwt2vf6Vbwmnj8XGntJItcOJf-Ue5tLg/s72-c/Screen+Shot+2019-10-11+at+6.23.09+PM.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-1789067913621275328</id><published>2019-10-11T18:15:00.001-07:00</published><updated>2019-10-11T18:15:13.095-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Mac"/><category scheme="http://www.blogger.com/atom/ns#" term="Terminal"/><title type='text'>How to change computer name and user name in terminal command prompt in MacOS</title><content type='html'>Sometimes we want to hide the system name or username that is displayed on the Terminal.&lt;br /&gt;
&lt;br /&gt;
May be to take some screenshot and then paste it in the blog and we want to hide the System name/user Name etc.&lt;br /&gt;
&lt;br /&gt;
For Bash based terminals it can be done by setting PS1 variable.&lt;br /&gt;
&lt;br /&gt;
Example on the terminal&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
$ export PS1=&quot;&amp;gt; &quot;
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;
$ PS1=&quot;Mac:~sidd:&quot;
Mac:~sidd:
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;
$ export PS1=&quot;This is Demo Terminal: $ &quot;
This is Demo Terminal: $
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Change your prompt in your&amp;nbsp; profile file to make it permanent across all Terminals and new sessions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/1789067913621275328/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/10/how-to-change-computer-name-and-user.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/1789067913621275328'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/1789067913621275328'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/10/how-to-change-computer-name-and-user.html' title='How to change computer name and user name in terminal command prompt in MacOS'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-4329589041418265174</id><published>2019-10-10T23:08:00.001-07:00</published><updated>2019-10-10T23:08:42.867-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Apache2"/><category scheme="http://www.blogger.com/atom/ns#" term="Install"/><title type='text'>How to Install Apache 2 on Ubuntu Linux</title><content type='html'>Following are the steps to install Apache 2 on Ubuntu Linux and then start it.&lt;br /&gt;
&lt;br /&gt;
Update the System Repository&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
sudo apt update
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Install Apache2 Software with apt command&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
sudo apt install apache2
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/4329589041418265174/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/10/how-to-install-apache-2-on-ubuntu-linux.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/4329589041418265174'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/4329589041418265174'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/10/how-to-install-apache-2-on-ubuntu-linux.html' title='How to Install Apache 2 on Ubuntu Linux'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-4419046836249346219</id><published>2019-09-05T00:04:00.000-07:00</published><updated>2019-09-05T00:04:36.176-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="HttpClient"/><title type='text'>How to convert HttpResponse to String in Java</title><content type='html'>Following code snippet explains how to convert HttpResponse object to String in Java&lt;br /&gt;
&lt;br /&gt;
Steps&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Call httpclient.execute()&lt;/li&gt;
&lt;li&gt;Get the InputStream from the response&lt;/li&gt;
&lt;li&gt;Convert the InputStream to ByteArrayOutputStream&lt;/li&gt;
&lt;li&gt;Convert the ByteArrayOutputStream to String&lt;/li&gt;
&lt;li&gt;Close the request and InputStream&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
private String executeAndGetResponse(HttpRequestBase request)
      throws IOException, ClientProtocolException, Exception, UnsupportedEncodingException {
    HttpResponse response = httpClient.execute(request);
    InputStream inputStream = response.getEntity().getContent();
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
      result.write(buffer, 0, length);
    }
    String strRes = result.toString(StandardCharsets.UTF_8.name());
   
    request.releaseConnection();
    inputStream.close();
    return strRes;
  }

&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/4419046836249346219/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/09/how-to-convert-httpresponse-to-string.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/4419046836249346219'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/4419046836249346219'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/09/how-to-convert-httpresponse-to-string.html' title='How to convert HttpResponse to String in Java'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-2445617404071630473</id><published>2019-09-03T00:56:00.000-07:00</published><updated>2019-09-03T00:56:08.100-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Jackson"/><category scheme="http://www.blogger.com/atom/ns#" term="JSON"/><title type='text'>Jackson Object Mapper to conver String to JsonNode</title><content type='html'>Following are some of the code snippet for Jackson Object Mapper

&lt;br /&gt;
Convert String to JsonNode Object
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;String data;
JsonNode jsonNode = getObjectMapper().readTree(data);
&lt;/code&gt;&lt;/pre&gt;
Convert an Object to JSON String
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;String jsonString = getObjectMapper().writeValueAsString(object);
&lt;/code&gt;&lt;/pre&gt;
Convert InputStream to JsonNode Object
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;InputStream inputStream;
JsonNode jsonNode = getObjectMapper().readTree(inputStream);
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/2445617404071630473/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/09/jackson-object-mapper-to-conver-string.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2445617404071630473'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2445617404071630473'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/09/jackson-object-mapper-to-conver-string.html' title='Jackson Object Mapper to conver String to JsonNode'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-8522525191186609585</id><published>2019-08-22T00:25:00.002-07:00</published><updated>2019-08-22T00:26:06.131-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="jQuery"/><title type='text'>How to check what version of JQuery is loaded in browser</title><content type='html'>Sometimes we might want to know what version of jQuery a particular site is using. It is not always possible to know the version of jQuery we are using through the jQuery File name or reading the content of the files, specially when the jQuery is minimized.&lt;br /&gt;
&lt;br /&gt;
Following snippet can tell us the version of jQuery loaded by a site.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
if (typeof jQuery != &#39;undefined&#39;) {  
    // jQuery is loaded =&amp;gt; print the version
    alert(jQuery.fn.jquery);
}
&lt;/code&gt;
&lt;/pre&gt;
Simply go to the console of the browser, once the webpage is loaded, and then type.
the above command on the console. It should display the version as an Alert.
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/8522525191186609585/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/08/how-to-check-what-version-of-jquery-is.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/8522525191186609585'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/8522525191186609585'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/08/how-to-check-what-version-of-jquery-is.html' title='How to check what version of JQuery is loaded in browser'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-321879633825070946</id><published>2019-08-20T22:02:00.002-07:00</published><updated>2019-08-20T22:02:50.575-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Blogger"/><category scheme="http://www.blogger.com/atom/ns#" term="Sitemap"/><title type='text'>How to generate sitemap for Blogs hosted on Blogger</title><content type='html'>&lt;div style=&quot;caret-color: rgb(0, 0, 0); font-family: -webkit-standard; text-size-adjust: auto;&quot;&gt;
Blogger comes with some goodies, like the atom feed URL, which can be used to generate sitemap for bloggers.&lt;/div&gt;
&lt;div style=&quot;caret-color: rgb(0, 0, 0); font-family: -webkit-standard; text-size-adjust: auto;&quot;&gt;
&lt;ol&gt;
&lt;li&gt;Sign in to&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://search.google.com/u/1/search-console&quot;&gt;Google Search Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Select the Blog from the left hand side.&lt;/li&gt;
&lt;li&gt;Click on Sitemaps link.&lt;/li&gt;
&lt;li&gt;At the top of the page with title &quot;Add a new sitemap&quot;&lt;/li&gt;
&lt;li&gt;Add the below code in the text field.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div style=&quot;caret-color: rgb(0, 0, 0); font-family: -webkit-standard; text-size-adjust: auto;&quot;&gt;
&lt;pre&gt;&lt;code&gt;atom.xml?redirect=false&amp;amp;start-index=1&amp;amp;max-results=500&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;caret-color: rgb(0, 0, 0); font-family: -webkit-standard; text-size-adjust: auto;&quot;&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/AVvXsEgftXmJpBQgwlETN1tW6YmeliafvIeT9q1y806jPiZ7vJBLeUz8oLCqczoAzk-ZTvyABXUeREyXllnG3YV34zxS3TYYZb7TXJVVfw8epOLr-K5cTL6gmJQm7GTXbCUVizjxIfejn_m-BDw/s1600/Screen+Shot+2019-06-20+at+11.38.40+PM.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Google Search Console&quot; border=&quot;0&quot; data-original-height=&quot;789&quot; data-original-width=&quot;1600&quot; height=&quot;197&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgftXmJpBQgwlETN1tW6YmeliafvIeT9q1y806jPiZ7vJBLeUz8oLCqczoAzk-ZTvyABXUeREyXllnG3YV34zxS3TYYZb7TXJVVfw8epOLr-K5cTL6gmJQm7GTXbCUVizjxIfejn_m-BDw/s400/Screen+Shot+2019-06-20+at+11.38.40+PM.png&quot; title=&quot;Google Search Console&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;caret-color: rgb(0, 0, 0); font-family: -webkit-standard; text-size-adjust: auto;&quot;&gt;
Example :&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;pre style=&quot;caret-color: rgb(0, 0, 0); text-size-adjust: auto;&quot;&gt;&lt;code&gt;
https://webdevpedia.blogspot.com/atom.xml?redirect=false&amp;amp;start-index=1&amp;amp;max-results=500&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;caret-color: rgb(0, 0, 0); font-family: -webkit-standard; text-size-adjust: auto;&quot;&gt;
Press “Submit Sitemap” button.&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/321879633825070946/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/08/how-to-generate-sitemap-for-blogs.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/321879633825070946'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/321879633825070946'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/08/how-to-generate-sitemap-for-blogs.html' title='How to generate sitemap for Blogs hosted on Blogger'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgftXmJpBQgwlETN1tW6YmeliafvIeT9q1y806jPiZ7vJBLeUz8oLCqczoAzk-ZTvyABXUeREyXllnG3YV34zxS3TYYZb7TXJVVfw8epOLr-K5cTL6gmJQm7GTXbCUVizjxIfejn_m-BDw/s72-c/Screen+Shot+2019-06-20+at+11.38.40+PM.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-2131904437678377024</id><published>2019-08-13T23:32:00.002-07:00</published><updated>2019-08-18T16:03:19.073-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CouchDB"/><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'>Handle 409 conflict document in couchdb</title><content type='html'>&lt;h1 class=&quot;grid--cell fs-headline1 fl1 ow-break-word mb8&quot; itemprop=&quot;name&quot; style=&quot;background-color: white; border: 0px; box-sizing: inherit; color: #242729; flex: 1 1 auto !important; font-family: Arial, &amp;quot;Helvetica Neue&amp;quot;, Helvetica, sans-serif; font-size: 2.07692rem !important; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; font-weight: inherit; line-height: 1.3; margin-bottom: 8px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-wrap: break-word !important; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: #3d85c6; font-size: large;&quot;&gt;Problem Use case:&lt;/span&gt;&lt;/h1&gt;
&lt;div&gt;
&amp;nbsp;CouchDB Error 409 conflict document.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Exception Details [Truncated]:&lt;/span&gt;&lt;br /&gt;
CouchDB is throwing error 409 conflict while calling REST API to update data.&lt;br /&gt;
CouchDB will throw 409 conflict document&amp;nbsp;exception if the payload is not proper. Following are some of the cases.&lt;br /&gt;
&quot;_id&quot; : This attribute is present, but the &quot;_rev&quot; attribute is not present or is wrong.&lt;br /&gt;
CouchDB uses both the &quot;_id&quot; and &quot;_rev&quot; attribute while updating a Document. Every time we update a document the &quot;_rev&quot; attribute value gets updated.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Solution:&lt;/span&gt;&lt;br /&gt;
If we want to update data, then make sure both &quot;_id&quot; and &quot;_rev&quot; attributes are carrying proper values respectively.&lt;br /&gt;
&lt;init&gt;&lt;br /&gt;&lt;/init&gt;&lt;init&gt;&lt;br /&gt;&lt;/init&gt;&lt;init&gt;&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Referance Links related to CouchDB:&lt;/span&gt;&lt;/init&gt;&lt;br /&gt;
&lt;a href=&quot;https://webdevpedia.blogspot.com/2019/07/installing-couchdb-on-gcp-compute-engine.html&quot;&gt;https://webdevpedia.blogspot.com/2019/07/installing-couchdb-on-gcp-compute-engine.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/2131904437678377024/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/08/handle-409-conflict-document-nodejs.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2131904437678377024'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2131904437678377024'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/08/handle-409-conflict-document-nodejs.html' title='Handle 409 conflict document in couchdb'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-2345101761742451873</id><published>2019-08-13T23:26:00.001-07:00</published><updated>2019-08-13T23:26:56.544-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CouchDB"/><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'> CouchDB Error 412:Precondition Failed</title><content type='html'>&lt;style type=&quot;text/css&quot;&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}
span.s1 {font-variant-ligatures: no-common-ligatures}
&lt;/style&gt;


&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Problem Use case:&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;CouchDB Error 412:Precondition Failed&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Exception Details [Truncated]:&lt;/span&gt;&lt;br /&gt;
CouchDB is throwing error while calling REST API to update data.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Solution:&lt;/span&gt;&lt;br /&gt;
CouchDB will throw 412:Precondition Failed exception if the payload does not contain the unique identifier &quot;_id&quot; attribute or its value is missing. Check the payload for PUT or POST to make sure the &quot;_id&quot; attribute is not missing.&lt;br /&gt;
&lt;init&gt;&lt;br /&gt;&lt;/init&gt;
&lt;init&gt;&lt;br /&gt;&lt;/init&gt;
&lt;init&gt;&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Referance Links related to CouchDB:&lt;/span&gt;&lt;/init&gt;&lt;br /&gt;
&lt;a href=&quot;https://webdevpedia.blogspot.com/2019/07/installing-couchdb-on-gcp-compute-engine.html&quot;&gt;https://webdevpedia.blogspot.com/2019/07/installing-couchdb-on-gcp-compute-engine.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/2345101761742451873/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/08/couchdb-error-412precondition-failed.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2345101761742451873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2345101761742451873'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/08/couchdb-error-412precondition-failed.html' title=' CouchDB Error 412:Precondition Failed'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-9530800885645814</id><published>2019-04-30T23:42:00.000-07:00</published><updated>2019-06-13T03:02:30.152-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Snippets"/><title type='text'>Starting  Spring Boot Application in Debug mode</title><content type='html'>Start the Spring boot application with the following jvm argument.
&lt;pre&gt;&lt;code&gt;
mvn spring-boot:run -Drun.jvmArguments=&quot;-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/9530800885645814/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/04/starting-spring-boot-application-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/9530800885645814'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/9530800885645814'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/04/starting-spring-boot-application-in.html' title='Starting  Spring Boot Application in Debug mode'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-851687739956999450</id><published>2019-04-20T20:34:00.001-07:00</published><updated>2019-04-20T21:05:52.002-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="eclipse"/><category scheme="http://www.blogger.com/atom/ns#" term="Maven"/><title type='text'>Maven attach source code for external Jar</title><content type='html'>For Java project that uses Maven for dependency and building we can attach the third party library source. By default the source is not available, using the following commands we can down load the associated source and java doc from maven repository by reading the project pom file.&lt;br /&gt;
&lt;br /&gt;
Maven will not be able to download sources and docs if those were not published for a particular library.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;//to download source for only one library&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;mvn dependency:sources -DincludeArtifactIds=spring-boot


&lt;/code&gt;&lt;/pre&gt;
Now after executing the above two commands, maven will download the respective sources and java docs.
Now we can attach the source in the IDE.

For eclipse specially it is better to use the Maven Eclipse plugin

&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
mvn eclipse:eclipse -DdownloadSources=true
mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=false
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Executing the above commands will also download the sources and it also updates the .classpath file. Now refreshing the Project in side eclipse should be able to map the corresponding sources for the external jars.
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/851687739956999450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/04/maven-attach-source-code-for-external.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/851687739956999450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/851687739956999450'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/04/maven-attach-source-code-for-external.html' title='Maven attach source code for external Jar'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-2596304463894524350</id><published>2019-04-13T00:15:00.004-07:00</published><updated>2019-08-13T23:23:33.932-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><category scheme="http://www.blogger.com/atom/ns#" term="SpringBoot"/><category scheme="http://www.blogger.com/atom/ns#" term="SpringBoot Issues"/><title type='text'>Failed to instantiate [java.util.List]: Specified class is an interface in HTTP controller handler?</title><content type='html'>&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;Problem Use case:&lt;/span&gt;&lt;br /&gt;
Following SpringBoot Rest Controller is not able to map the request payload&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
@Controller
@RequestMapping(&quot;/api&quot;)
  public class BlogController {  
    @RequestMapping(value = &quot;/blogstories&quot;, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
       public ResponseEntity&amp;lt;List&amp;lt;BlogStoryObject&amp;gt;&amp;gt; addUpdateblog(List&amp;lt;BlogStoryObject&amp;gt; blogItems) {
          List&amp;lt;BlogStoryObject&amp;gt; result = new ArrayList&amp;lt;BlogStoryObject&amp;gt;();
    //TODO:: Call Service to update/save objects
    return new ResponseEntity&amp;lt;List&amp;lt;RootBlogStoryObject&amp;gt;&amp;gt;(result, HttpStatus.OK);
 }
}
&lt;/code&gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Exception Details [Truncated]:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
And the error thrown at runtime is Failed to instantiate [java.util.List]: Specified class is an interface.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Solution&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Solution to the problem is to @RequestBody annotation to the method definition, so the following will work.

&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
public ResponseEntity&amp;lt;List&amp;lt;RootBlogStoryObject&amp;gt;&amp;gt; addUpdateblog(
  @RequestBody List&amp;lt;RootBlogStoryObject&amp;gt; blogItems) {
  /////implementation goes here

}
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/2596304463894524350/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/04/failed-to-instantiate-javautillist.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2596304463894524350'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2596304463894524350'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/04/failed-to-instantiate-javautillist.html' title='Failed to instantiate [java.util.List]: Specified class is an interface in HTTP controller handler?'/><author><name>Sidd Bhattacharjee</name><uri>http://www.blogger.com/profile/10537634667477180972</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-7984424389911462071</id><published>2019-04-12T18:32:00.000-07:00</published><updated>2019-04-20T20:24:55.062-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Exceptions"/><category scheme="http://www.blogger.com/atom/ns#" term="SpringBoot"/><title type='text'>Spring Boot Error : Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory</title><content type='html'>&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Problem Use case:&lt;/span&gt;&lt;br /&gt;
While starting a Spring Boot Application using&lt;br /&gt;
&lt;br /&gt;
mvn spring-boot:run&lt;br /&gt;
&lt;br /&gt;
The Application fails to start&lt;br /&gt;
Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Exception Details [Truncated]:&lt;/span&gt;&lt;br /&gt;
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.9.RELEASE:run (default-cli) on project Javaexp-springboot-rest: An exception occurred while running. null: InvocationTargetException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. -&amp;gt; [Help 1]&lt;br /&gt;
[ERROR]&lt;br /&gt;
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.&lt;br /&gt;
[ERROR] Re-run Maven using the -X switch to enable full debug logging.&lt;br /&gt;
[ERROR]&lt;br /&gt;
[ERROR] For more information about the errors and possible solutions, please read the following articles:&lt;br /&gt;
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6; font-size: large;&quot;&gt;Solution&lt;/span&gt;&lt;br /&gt;
Update pom.xml and use the spring-boot-starter-web
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-xml&quot;&gt;
&amp;lt;dependency&amp;gt;
   &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
   &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;
   &amp;lt;version&amp;gt;1.5.9.RELEASE&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;&lt;/code&gt;&lt;/pre&gt;

Now do a new build and the application should be able to start.
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/7984424389911462071/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/04/spring-boot-unable-to-start.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/7984424389911462071'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/7984424389911462071'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/04/spring-boot-unable-to-start.html' title='Spring Boot Error : Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-6723829479686594554</id><published>2019-04-05T11:01:00.002-07:00</published><updated>2019-04-05T11:01:27.816-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SpringBoot"/><title type='text'>Spring Boot CommandLineRunner</title><content type='html'>CommandLineRunner is an interface which allows us to write code that would be executed when the SpringBoot Application is initialized and before the startup is finished.&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;
Technology used&lt;/h2&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Java 8&lt;/li&gt;
&lt;li&gt;Spring boot 1.5.9&lt;/li&gt;
&lt;li&gt;Logback 1.1.1.1&lt;/li&gt;
&lt;li&gt;Maven 3.5.0&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2&gt;
Implementation&lt;/h2&gt;
&lt;div&gt;
This project uses Maven to build the project.&amp;nbsp; It shows a very simple example of a Spring boot Application and two Classes implementing&amp;nbsp;CommandLineRunner.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;u&gt;High level steps as described bellow with sample code&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
Define the pom.xml file&lt;/div&gt;
&lt;div&gt;
Write Spring boot app class&lt;/div&gt;
&lt;div&gt;
Write Spring boot CommandLineRunner&amp;nbsp;class&lt;br /&gt;
Change the order with Order annotation&lt;br /&gt;
&lt;br /&gt;
For simplicity the pom file is not provided here, but it can be found on the git repo here&amp;nbsp;&lt;a href=&quot;https://github.com/siddharthagit/spring-boot-references/blob/master/spingboot-resttemplate/pom.xml&quot; target=&quot;_blank&quot;&gt;https://github.com/siddharthagit/spring-boot-references/blob/master/spingboot-resttemplate/pom.xml&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
SpringBoot Application Class&lt;/div&gt;
&lt;div class=&quot;filename&quot; style=&quot;margin-bottom: -20px;&quot;&gt;
ResttemplateApplication.java&lt;/div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;@SpringBootApplication
public class ResttemplateApplication {

  private static final Logger log = LoggerFactory.getLogger(ResttemplateApplication.class);

  public static void main(String args[]) {
    log.info(&quot;about to call SpringApplication.run()&quot;);
    SpringApplication.run(ResttemplateApplication.class, args);
    log.info(&quot;completeed executing SpringApplication.run()&quot;);
  }

}&lt;/code&gt;&lt;/pre&gt;
Next we will provide two Classes which implements CommandLineRunner interface.


&lt;br /&gt;
&lt;br /&gt;
LoadAllEndpoint loads all the available endpoints by calling&amp;nbsp; github&#39;s REST API&lt;br /&gt;
&lt;div class=&quot;filename&quot; style=&quot;margin-bottom: -20px;&quot;&gt;
LoadAllEndpoint.java&lt;/div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
@Component
@Order(1)
public class LoadAllEndpoint implements CommandLineRunner {

  private static final Logger log = LoggerFactory.getLogger(ResttemplateApplication.class);

  @Override
  public void run(String... args) throws Exception {
    log.info(&quot;about to call LoadGithubRepo.run()&quot;);
    RestTemplate restTemplate = new RestTemplateBuilder().build();
    ResponseEntity&lt;jsonnode&gt; apis =
        restTemplate.getForEntity(&quot;https://api.github.com&quot;, JsonNode.class);
    
    //log.info(apis.toString());
  }
}&lt;/jsonnode&gt;&lt;/code&gt;&lt;/pre&gt;
LoadGithubRepo returns all the available in github for an account by calling&amp;nbsp; github&#39;s REST API
&lt;br /&gt;
&lt;div class=&quot;filename&quot; style=&quot;font-family: Times; margin-bottom: -20px; white-space: normal;&quot;&gt;
LoadGithubRepo.java&lt;/div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
@Component
@Order(2)
public class LoadGithubRepo implements CommandLineRunner {

  private static final Logger log = LoggerFactory.getLogger(ResttemplateApplication.class);

  @Override
  public void run(String... args) throws Exception {
    log.info(&quot;about to call LoadGithubRepo.run()&quot;);
    RestTemplate restTemplate = new RestTemplateBuilder().build();
    ResponseEntity&lt;jsonnode&gt; repos = restTemplate
        .getForEntity(&quot;https://api.github.com/users/siddharthagit/repos&quot;, JsonNode.class);
    //log.info(repos.toString());
  }
}
&lt;/jsonnode&gt;&lt;/code&gt;&lt;/pre&gt;
Run the application
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;
mvn spring-boot:run
&lt;/code&gt;&lt;/pre&gt;
Console Output (some part)
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;
AnnotationMBeanExporter:431 - Registering beans for JMX exposure on startup
ResttemplateApplication:21 - about to call LoadAllEndpoint.run()
ResttemplateApplication:21 - about to call LoadGithubRepo.run()
ResttemplateApplication:57 - Started ResttemplateApplication in 2.07 seconds
ResttemplateApplication:17 - completed executing ResttemplateApplication.run()
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;
Summary&lt;/h2&gt;
&lt;div&gt;
CommandLineRunner is useful when we need to execute code before the Applicaton startup completes&lt;/div&gt;
&lt;div&gt;
We can have multiple implementations of CommandLineRunner&lt;/div&gt;
&lt;div&gt;
We can arrange the order of execution between different implementations using Order annotation&lt;/div&gt;
&lt;h2&gt;
Download and Run
&lt;/h2&gt;
&lt;div class=&quot;github&quot;&gt;
git clone&amp;nbsp;&lt;a href=&quot;https://github.com/siddharthagit/spring-boot-references.git&quot;&gt;https://github.com/siddharthagit/spring-boot-references.git&lt;/a&gt;&lt;br /&gt;
cd&amp;nbsp;spingboot-resttemplate&lt;br /&gt;
mvn spring-boot:run&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/6723829479686594554/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/04/spring-boot-commandlinerunner.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/6723829479686594554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/6723829479686594554'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/04/spring-boot-commandlinerunner.html' title='Spring Boot CommandLineRunner'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-206699290975382814</id><published>2019-03-05T20:47:00.001-08:00</published><updated>2019-04-02T21:13:50.429-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SpringBoot"/><category scheme="http://www.blogger.com/atom/ns#" term="Template"/><category scheme="http://www.blogger.com/atom/ns#" term="Thymeleaf"/><title type='text'>Spring boot with Thymeleaf example </title><content type='html'>This blog shows how to use Thymeleaf as a template engine with Spring boot.&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/AVvXsEhVkrbdB8CXVjREsjg80YTjgqMw3Tj-UITt9_qhwG4Ya2sIKLv41czd5dBNWIuC4WJZyOEYWSIfvL0nQ25hvdEZbCWOLAd6xB2nqwAH8RxhuPd2KyJl9RB5zz0AfwMGrdFdT4cfmsgHCZg/s1600/thymeleaf.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1023&quot; data-original-width=&quot;1021&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVkrbdB8CXVjREsjg80YTjgqMw3Tj-UITt9_qhwG4Ya2sIKLv41czd5dBNWIuC4WJZyOEYWSIfvL0nQ25hvdEZbCWOLAd6xB2nqwAH8RxhuPd2KyJl9RB5zz0AfwMGrdFdT4cfmsgHCZg/s320/thymeleaf.png&quot; width=&quot;319&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h2&gt;
Technologies Used&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Java 8&lt;/li&gt;
&lt;li&gt;Spring framework&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;monaco&amp;quot;; font-size: 11px;&quot;&gt;1.5.9&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Spring boot 1.5.9&lt;/li&gt;
&lt;li&gt;Thymeleaf 2.1.6&lt;/li&gt;
&lt;li&gt;Tomcat embed 8.5.23&lt;/li&gt;
&lt;li&gt;Logback 1.1.1.1&lt;/li&gt;
&lt;li&gt;Junit 4.12&lt;/li&gt;
&lt;li&gt;Bootstrap 4&lt;/li&gt;
&lt;li&gt;Maven 3.5.0&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
Implementation&lt;/h2&gt;
&lt;div&gt;
This project uses Maven to build the project.&amp;nbsp; It shows a very simple example of a login page with corresponding Spring boot controller.&lt;br /&gt;
The goal of this project is to have a login page rendered using Thymeleaf template.&lt;br /&gt;
The template it self uses the bootstrap css for styling.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;u&gt;High level steps as described bellow with sample code&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
Define the pom.xml file&lt;/div&gt;
&lt;div&gt;
Write Spring boot app class&lt;/div&gt;
&lt;div&gt;
Write Spring boot controller class&lt;/div&gt;
&lt;div&gt;
Write&amp;nbsp; Thymeleaf Template for the controller class&lt;/div&gt;
&lt;div&gt;
Write the CSS referred by Template file&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;filename&quot; style=&quot;margin-bottom: -20px;&quot;&gt;
pom.xml&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-xml&quot;&gt;

&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&amp;gt;
  &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
  &amp;lt;artifactId&amp;gt;Javaexp.blogspot.com-thymeleaf&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;1.0.0-SNAPSHOT&amp;lt;/version&amp;gt;
  &amp;lt;packaging&amp;gt;war&amp;lt;/packaging&amp;gt;
  &amp;lt;name&amp;gt;Javaexp.blogspot.com Thymeleaf&amp;lt;/name&amp;gt;
  &amp;lt;description&amp;gt;Javaexp Thymeleaf Example&amp;lt;/description&amp;gt;
  &amp;lt;parent&amp;gt;
    &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;spring-boot-starter-parent&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;1.5.9.RELEASE&amp;lt;/version&amp;gt;
    &amp;lt;relativePath /&amp;gt;
  &amp;lt;/parent&amp;gt;
  &amp;lt;properties&amp;gt;
    &amp;lt;project.build.sourceEncoding&amp;gt;UTF-8&amp;lt;/project.build.sourceEncoding&amp;gt;
    &amp;lt;project.reporting.outputEncoding&amp;gt;UTF-8&amp;lt;/project.reporting.outputEncoding&amp;gt;
    &amp;lt;java.version&amp;gt;1.8&amp;lt;/java.version&amp;gt;
  &amp;lt;/properties&amp;gt;

  &amp;lt;dependencies&amp;gt;
  
    &amp;lt;!-- Springboot --&amp;gt;
    &amp;lt;dependency&amp;gt;
      &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;
      &amp;lt;version&amp;gt;1.5.9.RELEASE&amp;lt;/version&amp;gt;
    &amp;lt;/dependency&amp;gt;

    &amp;lt;!-- test --&amp;gt;
    &amp;lt;dependency&amp;gt;
      &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;spring-boot-starter-test&amp;lt;/artifactId&amp;gt;
      &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
    &amp;lt;/dependency&amp;gt;

    &amp;lt;!-- for Thymeleaf --&amp;gt;
    &amp;lt;dependency&amp;gt;
      &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;spring-boot-starter-thymeleaf&amp;lt;/artifactId&amp;gt;
    &amp;lt;/dependency&amp;gt;

    &amp;lt;!-- hot swapping, disable cache for template, enable live reload --&amp;gt;
    &amp;lt;dependency&amp;gt;
      &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;spring-boot-devtools&amp;lt;/artifactId&amp;gt;
      &amp;lt;optional&amp;gt;true&amp;lt;/optional&amp;gt;
      &amp;lt;scope&amp;gt;runtime&amp;lt;/scope&amp;gt;
    &amp;lt;/dependency&amp;gt;

  &amp;lt;/dependencies&amp;gt;
  &amp;lt;build&amp;gt;
    &amp;lt;plugins&amp;gt;
      &amp;lt;plugin&amp;gt;
        &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;spring-boot-maven-plugin&amp;lt;/artifactId&amp;gt;
        &amp;lt;configuration&amp;gt;
          &amp;lt;addResources&amp;gt;true&amp;lt;/addResources&amp;gt;
        &amp;lt;/configuration&amp;gt;
        &amp;lt;executions&amp;gt;
          &amp;lt;execution&amp;gt;
            &amp;lt;goals&amp;gt;
              &amp;lt;goal&amp;gt;repackage&amp;lt;/goal&amp;gt;
            &amp;lt;/goals&amp;gt;
          &amp;lt;/execution&amp;gt;
        &amp;lt;/executions&amp;gt;
      &amp;lt;/plugin&amp;gt;
      &amp;lt;plugin&amp;gt;
        &amp;lt;artifactId&amp;gt;maven-war-plugin&amp;lt;/artifactId&amp;gt;
        &amp;lt;configuration&amp;gt;
          &amp;lt;webResources&amp;gt;
            &amp;lt;resource&amp;gt;
              &amp;lt;directory&amp;gt;dist/&amp;lt;/directory&amp;gt;
            &amp;lt;/resource&amp;gt;
          &amp;lt;/webResources&amp;gt;
        &amp;lt;/configuration&amp;gt;
      &amp;lt;/plugin&amp;gt;
    &amp;lt;/plugins&amp;gt;
  &amp;lt;/build&amp;gt;
&amp;lt;/project&amp;gt;


&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;div class=&quot;filename&quot; style=&quot;margin-bottom: -20px;&quot;&gt;
StartWebApplication.java&lt;/div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
package com.blogspot.blogger;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartWebApplication {

  public static void main(String[] args) {
    SpringApplication.run(StartWebApplication.class, args);
  }

}

&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;filename&quot; style=&quot;margin-bottom: -20px;&quot;&gt;
WebLoginController.java&lt;/div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
package com.blogspot.blogger;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class WebLoginController {

  @GetMapping(&quot;/login&quot;)
  public String login(Model model) {
    return &quot;login&quot;;
  }

}


&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
For the Thymeleaf template file put it under&amp;nbsp;src/main/resources/templates/login.html&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;filename&quot; style=&quot;margin-bottom: -20px;&quot;&gt;
login.html&lt;/div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta charset=&quot;utf-8&quot;&amp;gt;&amp;lt;/meta&amp;gt;
&amp;lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{~/css/bootstrap.min.css}&quot; /&amp;gt;
&amp;lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{~/css/style.default.css}&quot; /&amp;gt;
&amp;lt;title&amp;gt;Javaexp.blogspot.com Thymeleaf Example&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;div class=&quot;container&quot;&amp;gt;
    &amp;lt;div class=&quot;modal-dialog&quot;&amp;gt;
      &amp;lt;div class=&quot;loginmodal-container&quot;&amp;gt;
        &amp;lt;h1&amp;gt;Login to Your Account&amp;lt;/h1&amp;gt;
        &amp;lt;form&amp;gt;
          &amp;lt;input type=&quot;text&quot; name=&quot;user&quot; placeholder=&quot;Username&quot;&amp;gt;&amp;lt;/input&amp;gt; &amp;lt;input
            type=&quot;password&quot; name=&quot;pass&quot; placeholder=&quot;Password&quot;&amp;gt;&amp;lt;/input&amp;gt; &amp;lt;input
            type=&quot;submit&quot; name=&quot;login&quot; class=&quot;login loginmodal-submit&quot;
            value=&quot;Login&quot;&amp;gt;&amp;lt;/input&amp;gt;
        &amp;lt;/form&amp;gt;
        &amp;lt;div class=&quot;login-help&quot;&amp;gt;
          &amp;lt;a href=&quot;#&quot;&amp;gt;Register&amp;lt;/a&amp;gt; - &amp;lt;a href=&quot;#&quot;&amp;gt;Forgot Password&amp;lt;/a&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;filename&quot; style=&quot;margin-bottom: -20px;&quot;&gt;
application.properties&lt;/div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-xml&quot;&gt;
#SPRING BOOT MVC
spring.mvc.view.prefix=/static/
spring.mvc.view.suffix=.html
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h2&gt;
Run the Spring boot Application&lt;/h2&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-xml&quot;&gt;$ mvn spring-boot:run

INFO restartedMain o.a.c.h.Http11NioProtocol:179 - Starting 
ProtocolHandler [&quot;http-nio-8080&quot;]
INFO restartedMain o.a.t.u.n.NioSelectorPool:179 - Using a shared selector for servlet write/read
INFO restartedMain o.s.b.c.e.t.TomcatEmbeddedServletContainer:201 - Tomcat started on port(s): 8080 (http)
INFO restartedMain c.b.b.StartWebApplication:57 - Started StartWebApplication in 2.337 seconds (JVM running for 2.735)
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h2&gt;
Demo&lt;/h2&gt;
&lt;div&gt;
Goto :&amp;nbsp;http://localhost:8080/login&lt;/div&gt;
&lt;div&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/AVvXsEgEAmor-XFipcUWidMAI5gc127BnqFWuG-CdRgnoXHzO0LI-H_7oy0y9ETB6jk2l6loen4js_yTtFKjDkv42ima95goguJQ9OOwKGHYcnB0iQRl95sKfvBGJk7WTXUEoMoDK2czwQwyFL0/s1600/Screen+Shot+2019-03-04+at+9.11.00+PM.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;568&quot; data-original-width=&quot;1178&quot; height=&quot;192&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEAmor-XFipcUWidMAI5gc127BnqFWuG-CdRgnoXHzO0LI-H_7oy0y9ETB6jk2l6loen4js_yTtFKjDkv42ima95goguJQ9OOwKGHYcnB0iQRl95sKfvBGJk7WTXUEoMoDK2czwQwyFL0/s400/Screen+Shot+2019-03-04+at+9.11.00+PM.png&quot; width=&quot;400&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;h2&gt;
Download and Run&lt;/h2&gt;
&lt;div class=&quot;github&quot;&gt;
git clone&amp;nbsp;&lt;a href=&quot;https://github.com/siddharthagit/spring-boot-references.git&quot;&gt;https://github.com/siddharthagit/spring-boot-references.git&lt;/a&gt;&lt;br /&gt;
cd&amp;nbsp;spingboot-web-thymeleaf&lt;br /&gt;
mvn spring-boot:run&lt;/div&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/206699290975382814/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/03/spring-boot-with-thymeleaf-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/206699290975382814'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/206699290975382814'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/03/spring-boot-with-thymeleaf-example.html' title='Spring boot with Thymeleaf example '/><author><name>Sidd Bhattacharjee</name><uri>http://www.blogger.com/profile/10537634667477180972</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVkrbdB8CXVjREsjg80YTjgqMw3Tj-UITt9_qhwG4Ya2sIKLv41czd5dBNWIuC4WJZyOEYWSIfvL0nQ25hvdEZbCWOLAd6xB2nqwAH8RxhuPd2KyJl9RB5zz0AfwMGrdFdT4cfmsgHCZg/s72-c/thymeleaf.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-1580242175426540596</id><published>2019-03-01T00:40:00.000-08:00</published><updated>2019-04-17T23:28:45.494-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CouchDB"/><category scheme="http://www.blogger.com/atom/ns#" term="GCP"/><title type='text'>Installing Couchdb on GCP Compute Engine</title><content type='html'>This article covers the steps that needed to install Apache Couchdb on Google Cloud Platform.&lt;br /&gt;
&lt;br /&gt;
There are various way of how to achieve this, that is having a Compute Engine (VM) installed with Couchdb.&lt;br /&gt;
&lt;br /&gt;
One option is to use the the Marketplace in GCP which offers quick option to deploy on Cloud Platform.&lt;br /&gt;
Login to GCP and click on the&amp;nbsp; Marketplace Icon and then search for CouchDB. Although we would still need to perform some additional steps like Creating Firewall Rule etc so that we can access the instance after getting VM with CouchDB installed from Marketplace.&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/AVvXsEglGUhyS85MPXG4rPH6tD2rmVEBUffo6gDH_F0RgMDP01NjRrSe9fvXBtCkUjHj08StDVvqjvKIqm6pgPtpiaksDMJ_QKGdLMCleWFdKRjVtqE6Jl25tZ9ulPeHI4jsLYhR6gAEnG4qoB0/s1600/Screen+Shot+2019-03-09+at+10.30.36+PM.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;707&quot; data-original-width=&quot;1600&quot; height=&quot;282&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglGUhyS85MPXG4rPH6tD2rmVEBUffo6gDH_F0RgMDP01NjRrSe9fvXBtCkUjHj08StDVvqjvKIqm6pgPtpiaksDMJ_QKGdLMCleWFdKRjVtqE6Jl25tZ9ulPeHI4jsLYhR6gAEnG4qoB0/s640/Screen+Shot+2019-03-09+at+10.30.36+PM.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Rather than using the Marketplace, we would do everything manually on the VM Instance.&lt;br /&gt;
Following are the steps in details that i had to take to lunch an instance of CouchDB, do configuration update and create Firewall Rules to allow access to the instance.&lt;br /&gt;
&lt;br /&gt;
High level steps as covered bellow are&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Create an Instance of Google Cloud Engine Instance&lt;/li&gt;
&lt;li&gt;Install Apache CouchDB in the VM&lt;/li&gt;
&lt;li&gt;Configure the Database Instance&lt;/li&gt;
&lt;li&gt;Configure Firewall Access&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;h3&gt;
Create Google Cloud compute Engine instance.&lt;/h3&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/AVvXsEh0DJwzAv7MaF-BMOwtovA8KGzKbkDxZpeAd1FidRqfPZEg83Un6CP8qOqbVWPyu10QMFB3XoefFHgjB4xYDKKKkG_DzmWMOiZmxMvnoQcRJlf7Zhcob2quqmUcH0NNaR_ZYCfa0Od5HKI/s1600/Screen+Shot+2019-03-10+at+1.35.51+PM.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1262&quot; data-original-width=&quot;1600&quot; height=&quot;504&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0DJwzAv7MaF-BMOwtovA8KGzKbkDxZpeAd1FidRqfPZEg83Un6CP8qOqbVWPyu10QMFB3XoefFHgjB4xYDKKKkG_DzmWMOiZmxMvnoQcRJlf7Zhcob2quqmUcH0NNaR_ZYCfa0Od5HKI/s640/Screen+Shot+2019-03-10+at+1.35.51+PM.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
SSH to the VM Instance.&lt;/h3&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;Before installing the Apache CouchDB we will install some useful software that we would need like nano editor.&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;pre class=&quot;command&quot;&gt;sudo apt-get install nano&lt;/pre&gt;
&lt;h3&gt;
Install CouchDB&lt;/h3&gt;
&lt;pre class=&quot;command&quot;&gt;&lt;div&gt;
sudo apt update&lt;/div&gt;
&lt;div&gt;
sudo apt-get install apache2 couchdb&lt;/div&gt;
&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
Check Status of CouchDB&lt;/h3&gt;
&lt;div&gt;
Following commands are useful to check the status of CouchDB instance, start it and stop it etc.&lt;/div&gt;
&lt;div&gt;
&lt;pre class=&quot;command&quot;&gt;&lt;div&gt;
sudo systemctl stop couchdb.service
&lt;/div&gt;
&lt;div&gt;
sudo systemctl start couchdb.service
&lt;/div&gt;
&lt;div&gt;
sudo systemctl enable couchdb.service
&lt;/div&gt;
&lt;div&gt;
sudo systemctl status couchdb.service
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h3&gt;
Change the bind address of CouchDB&lt;/h3&gt;
&lt;/div&gt;
Normally the CouchDB listens to port 127.0.0.1 which is localhost.&lt;br /&gt;
When we deploy the CouchDB on VM on GCP we would like it to be accessed from outside
the VM also, for that we need to modify the bind address. We do that by updating CouchDB configuration file&lt;br /&gt;
&lt;pre class=&quot;command&quot;&gt;nano /etc/couchdb/default.ini
&lt;/pre&gt;
&lt;pre class=&quot;command&quot;&gt;port = 5984
bind_address = 0.0.0.0
&lt;/pre&gt;
&lt;div&gt;
&lt;h3&gt;
Add Firewall rule for the VM instance&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
We will now add a new Firewall rule to allow access to the VM on which we have CouchDB installed.&lt;br /&gt;
For that go to &lt;b&gt;Navigation Menu -&amp;gt; VPC Networks -&amp;gt; Firewall Rule&lt;/b&gt; and click on&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
Create Firewall rule&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Add the necessary details like follows,&lt;br /&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/AVvXsEh2qK2gy-c_uUpJwkTLXTSO6j_Vilx3GpncZcWG5lGIz95zm0QjIUmoGHXqNtta3D0OPmDuysFSDqG0YJjTTT9FoFrMqdR_QWpNdcoI8osMkJMbU9akFbFqCRB3xxns6avaYcTcBkOxDy8/s1600/Screen+Shot+2019-03-10+at+6.51.09+PM.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;807&quot; data-original-width=&quot;1600&quot; height=&quot;322&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2qK2gy-c_uUpJwkTLXTSO6j_Vilx3GpncZcWG5lGIz95zm0QjIUmoGHXqNtta3D0OPmDuysFSDqG0YJjTTT9FoFrMqdR_QWpNdcoI8osMkJMbU9akFbFqCRB3xxns6avaYcTcBkOxDy8/s640/Screen+Shot+2019-03-10+at+6.51.09+PM.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
Now we we should be able to access the instance over internet&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Access the Instance&lt;/h3&gt;
&lt;div&gt;
Open a browser and go to&amp;nbsp;external-ip:port/_util/, we should be able to see a page like as bellow&lt;/div&gt;
&lt;div&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/AVvXsEhDxwylEzBB9CG4kRylT6IDM1GxJpF93m31y887KQWhmg8V7a9YdOwj4zoE53281kVur4wPgMammvSzpdO6Sy5z7PRdMMiYQ65vqYXBlcksBOKZQmIniU74K_jZkQzJ6o10nTK1DAk53zA/s1600/Screen+Shot+2019-03-10+at+6.58.35+PM.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;425&quot; data-original-width=&quot;1600&quot; height=&quot;170&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDxwylEzBB9CG4kRylT6IDM1GxJpF93m31y887KQWhmg8V7a9YdOwj4zoE53281kVur4wPgMammvSzpdO6Sy5z7PRdMMiYQ65vqYXBlcksBOKZQmIniU74K_jZkQzJ6o10nTK1DAk53zA/s640/Screen+Shot+2019-03-10+at+6.58.35+PM.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/1580242175426540596/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/03/installing-couchdb-on-gcp-compute-engine.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/1580242175426540596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/1580242175426540596'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/03/installing-couchdb-on-gcp-compute-engine.html' title='Installing Couchdb on GCP Compute Engine'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglGUhyS85MPXG4rPH6tD2rmVEBUffo6gDH_F0RgMDP01NjRrSe9fvXBtCkUjHj08StDVvqjvKIqm6pgPtpiaksDMJ_QKGdLMCleWFdKRjVtqE6Jl25tZ9ulPeHI4jsLYhR6gAEnG4qoB0/s72-c/Screen+Shot+2019-03-09+at+10.30.36+PM.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-2287410503081412551</id><published>2019-02-17T14:12:00.000-08:00</published><updated>2019-02-17T15:07:10.959-08:00</updated><title type='text'>Testing private method in Java</title><content type='html'>Private method in java is not accessible outside the Class which defines it.&lt;br /&gt;
Since they cannot be accessed we cannot call them from any Test Class directly to test them.&lt;br /&gt;
&lt;br /&gt;
Following are some of the options to test the private method&lt;br /&gt;
&lt;br /&gt;
1) Change the visibility of the private method to package or protected.&lt;br /&gt;
2) Create a public method in the Class and call the private method from the public method&lt;br /&gt;
&lt;br /&gt;
Above two solutions requires us to change the original Class. Some times we cannot change the original Class but still would like to have a Test Class for the same method.&lt;br /&gt;
&lt;br /&gt;
The other option is to use Reflection api to invoke the private method.&lt;br /&gt;
&lt;br /&gt;
For example lets try to test a private method multiple on Class CreditcardMask.java&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
Class&lt;creditcardmask&gt; targetClass =
(Class&lt;creditcardmask&gt;) Class.forName(CreditcardMask.class.getName());

// get the method multiply
Method testMethod = targetClass.getDeclaredMethod(&quot;multiply&quot;, Integer.class, Integer.class);

Assert.assertNotNull(&quot;testMethod is Null&quot;, testMethod);

// set visibility to true for the private method
testMethod.setAccessible(true);

// get the constructor
Constructor&lt;creditcardmask&gt; constructor = targetClass.getDeclaredConstructor();
Object instance = constructor.newInstance(); // create a new instance

Integer param1 = 10;
Integer param2 = 10;

// invoke the method
Object testMethodResult = testMethod.invoke(instance, param1, param2);

Assert.assertEquals(100, testMethodResult);

&lt;/creditcardmask&gt;&lt;/creditcardmask&gt;&lt;/creditcardmask&gt;&lt;/code&gt;
&lt;/pre&gt;
Full source code can be found on&lt;br /&gt;
Github&amp;nbsp;&lt;a href=&quot;https://github.com/siddharthagit/javaexpcodes/blob/master/core-java/test/TestPrivateMethod.java&quot;&gt;https://github.com/siddharthagit/javaexpcodes/blob/master/core-java/test/TestPrivateMethod.java&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/2287410503081412551/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/02/testing-private-method-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2287410503081412551'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/2287410503081412551'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/02/testing-private-method-in-java.html' title='Testing private method in Java'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-5767012385339561868</id><published>2019-02-01T10:20:00.001-08:00</published><updated>2019-02-01T10:20:52.199-08:00</updated><title type='text'>Proxy Design Pattern In java</title><content type='html'>&lt;h3 class=&quot;post-title entry-title&quot; itemprop=&quot;name&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;Trebuchet MS&amp;quot;, Trebuchet, Verdana, sans-serif; font-size: 22px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: normal; line-height: normal; margin: 0.75em 0px 0px; position: relative;&quot;&gt;
Type : Structural Design Pattern&lt;/h3&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Summary&lt;/h2&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
Proxy Design pattern lets a Proxy Object rather then the real object to interact with Client. By doing so the proxy can control access to the real object or delay the creation of real object unless it is is really needed.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;Description&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
In Proxy Design pattern the Client is served with a Proxy Object rather than the real object. Benefit of doing so is that, we have greater control in when to create the real object. It would help us in providing in lazy loading the real object, providing extra security checking etc.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
Example&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
For example lets consider a service called UserDetails Service which the Client calls to get UserDetails Object. Now say&amp;nbsp;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;UserDetails is a complex object, and we need to real the database to populate it. Also we would like to have a security authorization check by which we will not show the details at all or show only few attributes from UserDetails Object.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;Returning a Proxy rather than original object help us in achieving our goals. In our example&amp;nbsp;UserDetailsProxy either returns a proxy object or real object based on the parameter(caller id). Say if &quot;admin&quot; user is calling than it returns the actual object otherwise it returns the proxy object.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;
Class Diagram&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&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/AVvXsEjvMxIYX0LQq6Vxu246IFwuXQb3ELYA5KU4gLbudEnXV8VEL4SBnsnSxpsBsyoRNcRKVYPtQEpgsYAs7wZwQoDR7lg7xg0-CmCUvrzYu2FiV9Bwbp6f0KSJyfaAF_1k-QPo2d6U4CvgDVM/s1600/proxy.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;362&quot; data-original-width=&quot;523&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvMxIYX0LQq6Vxu246IFwuXQb3ELYA5KU4gLbudEnXV8VEL4SBnsnSxpsBsyoRNcRKVYPtQEpgsYAs7wZwQoDR7lg7xg0-CmCUvrzYu2FiV9Bwbp6f0KSJyfaAF_1k-QPo2d6U4CvgDVM/s1600/proxy.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;
Sample Code&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;public interface UserDetails {

  public UserDetails getUserDetails(String callerId, String uid);

  public Double getTotalBalance(String uid);
}

// actual Object which contains UserDetails
public class UserDetailsImpl implements UserDetails {

  private String userId;
  private String bankAccountNo;
  private String socialSecurityNo;
  private String zipCode;
  private Double totalBalance;

  @Override
  public UserDetails getUserDetails(String callerId, String uid) {
    return UserDetailsService.retrieveUserById(uid);
  }

  public UserDetailsImpl() {

  }

  @Override
  public String toString() {
    return &quot;userId=&quot; + this.userId + &quot; bankAccountNo=&quot; + this.bankAccountNo + &quot; socialsecurity=&quot;
        + socialSecurityNo;
  }

  @Override
  public Double getTotalBalance(String uid) {
    return totalBalance;
  }

}


// proxy Object for UserDetails
public class UserDetailsProxy implements UserDetails {

  UserDetailsImpl realUserDetails = null;

  @Override
  public UserDetails getUserDetails(String callerId, String uId) {
    if (callerId.equalsIgnoreCase(&quot;admin&quot;)) {
      return UserDetailsService.retrieveUserById(uId);
    } else {
      return new UserDetailsProxy();
    }
  }


  @Override
  public String toString() {
    return &quot;userId=NA bankAccountNo=NA socialsecurity=NA&quot;;
  }


  @Override
  public Double getTotalBalance(String uid) {
    if (realUserDetails == null) {
      realUserDetails = UserDetailsService.retrieveUserById(uid);
    }

    return realUserDetails.getTotalBalance();
  }

}


//UserDetails Service which returns the Object
public class UserDetailsService {

  public static UserDetailsImpl retrieveUserById(String uId) {
    UserDetailsImpl userDetail = new UserDetailsImpl();
    userDetail.setUserId(uId);
    userDetail.setBankAccountNo(&quot;swissbank_11228899&quot;);
    userDetail.setSocialSecurityNo(&quot;888 11 777&quot;);
    userDetail.setZipCode(&quot;95011&quot;);
    userDetail.setTotalBalance(99.9);
    return userDetail;
  }
}&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;
Github Code&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;
Advantages&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
Proxies can help increase performance by delaying creation of the real object.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;It can improve security by wrapping the real object by proxy object and returning real object when allowed.&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;
Disadvantages&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;Proxy pattern introduces new Proxy objects.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;Proxy objects might need to refactored if the Original object also changes.&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;
References&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;https://en.wikipedia.org/wiki/Proxy_pattern&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/5767012385339561868/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/02/proxy-design-pattern-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/5767012385339561868'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/5767012385339561868'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/02/proxy-design-pattern-in-java.html' title='Proxy Design Pattern In java'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvMxIYX0LQq6Vxu246IFwuXQb3ELYA5KU4gLbudEnXV8VEL4SBnsnSxpsBsyoRNcRKVYPtQEpgsYAs7wZwQoDR7lg7xg0-CmCUvrzYu2FiV9Bwbp6f0KSJyfaAF_1k-QPo2d6U4CvgDVM/s72-c/proxy.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-3240057111949226714</id><published>2019-02-01T10:20:00.000-08:00</published><updated>2019-02-01T10:20:23.207-08:00</updated><title type='text'>Flyweight Pattern in Java</title><content type='html'>&lt;h3 class=&quot;post-title entry-title&quot; itemprop=&quot;name&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;Trebuchet MS&amp;quot;, Trebuchet, Verdana, sans-serif; font-size: 22px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: normal; line-height: normal; margin: 0.75em 0px 0px; position: relative;&quot;&gt;
Type : Structural Design Pattern&lt;/h3&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Summary&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Flyweight design pattern deals with sharing some of the internal details between different object instances, so that we can save memory by basically using less duplication of the attributes across its instances.&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Details&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
In Flyweight design pattern the Object attributes are categorized into its &lt;b&gt;extrinsic&lt;/b&gt; and &lt;b&gt;intrinsic&lt;/b&gt; attributes.&amp;nbsp;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;Extrinsic&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;attributes&lt;/span&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;&amp;nbsp;are the attributes that can be considered as sharable across other instances.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;Intrinsic&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;attributes&lt;/span&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;&amp;nbsp;are the attributes that are unique for each object, like the primary key, unique fields etc.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;In Flyweight design we can improve the memory footprint by reusing the extrinsic properties in multiple instances of the object.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;Flyweight can use Cache to store the different Extrinsic states. Factory method can be used to create the instances of the Extrinsic object and stored in cache.&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Example&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
For instance say we are designing a Vehicle in OOP. Now each instance of the Vehicle will have some unique characteristics like its serial number, registration number.&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Also we can notice that each vehicle instances will share something very similar like the Body Type, Engine etc. across multiple instances.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
We will consider the Engine to be extrinsic property which can be shared across different instances of Vehicle&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
We will consider the serial number as intrinsic property.&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;b&gt;Vehicle&lt;/b&gt; is the interface representing vehicle&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Vehicle contains an instance fo Engine&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;b&gt;Engine&lt;/b&gt; is an interface&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
We have two type of Engine implemented by&amp;nbsp;&lt;b&gt;ElectricEngine&lt;/b&gt; and &lt;b&gt;GasolineEngine&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;b&gt;EngineFactory&lt;/b&gt; is a cache holding different types of Engine.&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;b&gt;CarImpl&lt;/b&gt; implements Vehicle. It has a reference to Engine object.&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-size: 13.2px; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;/h2&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Class Diagram&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-size: 13.2px; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVYHg98xl5-4DZbhJ42RvtTQdS0xlgAO2bmEDNFNoUHdvnHSga4FndJgC3ypL8H4Y7t5AJr-v5j6-fXBzXBMSRgGed28-1GnHTjeXNLKMlaheYoGaU1gwwHyhrhNB_-RJLYLVFA7-uGUo/s1600/Flyweight.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;509&quot; data-original-width=&quot;658&quot; height=&quot;247&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVYHg98xl5-4DZbhJ42RvtTQdS0xlgAO2bmEDNFNoUHdvnHSga4FndJgC3ypL8H4Y7t5AJr-v5j6-fXBzXBMSRgGed28-1GnHTjeXNLKMlaheYoGaU1gwwHyhrhNB_-RJLYLVFA7-uGUo/s320/Flyweight.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Sample Code&lt;/span&gt;&lt;/h2&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
public interface Engine {
  
  public String getEngineName();

  public String getType();

  public int getHorsePower();

  public String getMaxSpeed();
}



public interface Vehicle {

  public String getModelName();

  public String getYear();

  Engine getEngine();

}

public class ElectricEngine implements Engine {

  private String engineName;
  private String engineType;

  public ElectricEngine(String name) {
    this.engineName = name;
    this.engineType = &quot;Electric&quot;;
  }

  @Override
  public String getEngineName() {
    return engineName;
  }

  @Override
  public int getHorsePower() {
    return 200;
  }

  @Override
  public String getMaxSpeed() {
    return &quot;110 MPH&quot;;
  }
  
  public String getEngineType() {
    return engineType;
  }

  @Override
  public String getType() {
    return this.engineType;
  }

}

public class GasolineEngine implements Engine {

  private String engineName;
  private String engineType;

  public GasolineEngine(String name) {
    this.engineName = name;
    this.engineType = &quot;Gasoline&quot;;
  }

  @Override
  public String getEngineName() {
    return this.engineName;
  }

  @Override
  public int getHorsePower() {
    return 200;
  }

  @Override
  public String getMaxSpeed() {
    return &quot;170 MPH&quot;;
  }

  public String getEngineType() {
    return engineType;
  }
  
  @Override
  public String getType() {
    return this.engineType;
  }

}



public class EngineFactory {
  private static Map&lt;string engine=&quot;&quot;&gt; engineCache = new HashMap&lt;string engine=&quot;&quot;&gt;();

  public static Engine getEngine(String engineName) {
    if (engineCache.containsKey(engineName)) {
      return engineCache.get(engineName);
    } else {
      if (engineName.startsWith(&quot;Electric&quot;))
        return getElectricEngine(engineName);
      else
        return getGasEngine(engineName);
    }
  }

  public static Engine getGasEngine(String engineName) {
    GasolineEngine gasEngine = new GasolineEngine(engineName);
    engineCache.put(engineName, gasEngine);
    return gasEngine;
  }

  public static Engine getElectricEngine(String engineName) {
    ElectricEngine gasEngine = new ElectricEngine(engineName);
    engineCache.put(engineName, gasEngine);
    return gasEngine;
  }

  public static int getTotalEnginesMade() {
    return engineCache.size();
  }

}

public class CarImpl implements Vehicle {

  private String model;
  private String year;
  private String serialNumber;
  private Engine engine;

  public CarImpl(String model, String year, String slNumber, String type) {
    this.model = model;
    this.year = year;
    this.serialNumber = slNumber;
    this.engine = EngineFactory.getEngine(type);
  }

  @Override
  public String getModelName() {
    return this.model;
  }

  @Override
  public String getYear() {
    return this.year;
  }

  @Override
  public Engine getEngine() {
    return this.engine;
  }

  public String getSerialNumber() {
    return this.serialNumber;
  }

  @Override
  public String toString() {
    return &quot;model= &quot; + this.model + &quot; year=&quot; + this.year + &quot; slNo=&quot; + this.serialNumber
        + &quot; engineType=&quot; + this.engine.getType();
  }
}
&lt;/string&gt;&lt;/string&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Advantages&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Memory consumption can be reduced by sharing Extrinsic states across objects.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Disadvantages&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;Flyweight weight increases the complexity of Object structure.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/3240057111949226714/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/02/flyweight-pattern-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/3240057111949226714'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/3240057111949226714'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/02/flyweight-pattern-in-java.html' title='Flyweight Pattern in Java'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVYHg98xl5-4DZbhJ42RvtTQdS0xlgAO2bmEDNFNoUHdvnHSga4FndJgC3ypL8H4Y7t5AJr-v5j6-fXBzXBMSRgGed28-1GnHTjeXNLKMlaheYoGaU1gwwHyhrhNB_-RJLYLVFA7-uGUo/s72-c/Flyweight.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-8851385081535996605</id><published>2019-02-01T10:19:00.002-08:00</published><updated>2019-02-01T10:19:50.418-08:00</updated><title type='text'>Adapter Design Pattern in Java</title><content type='html'>&lt;h3 class=&quot;post-title entry-title&quot; itemprop=&quot;name&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;Trebuchet MS&amp;quot;, Trebuchet, Verdana, sans-serif; font-size: 22px; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: normal; line-height: normal; margin: 0.75em 0px 0px; position: relative;&quot;&gt;
Type : Structural Design Pattern&lt;/h3&gt;
&lt;div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Summary&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
Adapter design pattern is a Structural design pattern. It is used to provides compatible interface for and Class so that can be used by Client. Adapter pattern is used to provide compatibility for Classes which already exists and cannot be modified.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;
Details&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
Need for adapter design pattern is very obvious. It is also very common in real world as well as in Software Engineering. For instance if we have any electrical appliances which runs on 110 Volt and then we travel to a country where the electricity supply is 220 V. We cannot change or modify the electric connection to provide us 110 V. Rather we can use a adapter to convert the 220 V from outlet to 110V.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
Adapter pattern is similar to Bridge pattern because both provide mechanism to connect incompatible Classes.&lt;br /&gt;
&lt;br /&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Advantages&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Incompatible Object or Class can be used using an Adapter for it.&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Disadvantages&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;color: black; font-family: Times; font-size: medium;&quot;&gt;
Increases number of Classes&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Times; font-size: medium;&quot;&gt;
Adapter Class needs to be modified if the Target Classes changes.&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;
Example&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;For instance if we consider a TV set which has input connector as USB. We can connect any device which has USB port.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;Now say a new device launched &quot;Blue Ray DVD&quot; which has HDMI as output. We cannot connect this two devices as they are not compatible .&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;We make them work together by providing an adapter&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;monaco&amp;quot;;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;HDMIToUSBAdapter&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif; font-size: 13.2px;&quot;&gt;which converts HDMI to USB and vice versa&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
Class Diagram&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-size: 13.2px; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK6cU1_n32YroMGUhZH2Czkr0Lt0fy_ITepL1fne-sfzGgg8RY3GcExueDluDAm-ewrZJxgjac6r0bXagAM072nY24ee1p6du6smaNVs5eVa7cng66lYWvaYEjic2_qF6gRpKJuemVU9c/s1600/adapter.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;296&quot; data-original-width=&quot;649&quot; height=&quot;145&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK6cU1_n32YroMGUhZH2Czkr0Lt0fy_ITepL1fne-sfzGgg8RY3GcExueDluDAm-ewrZJxgjac6r0bXagAM072nY24ee1p6du6smaNVs5eVa7cng66lYWvaYEjic2_qF6gRpKJuemVU9c/s320/adapter.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;
Sample Code&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint&quot; style=&quot;font-size: 13.2px;&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;
// TV which connects though USB port
public class TV {

  private USBConnectorPlayer connect;

  public void connectTVAndPlay(USBConnectorPlayer conn) {
    this.connect(conn);
    this.connect.renderContent();
  }

  public void connect(USBConnectorPlayer connect) {
    this.connect = connect;
  }
}


// USB connector player
public class USBConnectorPlayer {
   private String content;
   public USBConnectorPlayer(String content) {
     this.content = content;
  }
   public void renderContent() {
       System.out.println(this.content);
   }
}

// a player that supports HDMI connection
public class HDMIConnectorPlayer {

  private StringBuffer content;

  public HDMIConnectorPlayer(StringBuffer content) {
    this.content = content;
  }

  public void renderContent() {
    System.out.println(this.content);
  }

  public StringBuffer getContent() {
    return content;
  }
}

package blog.javaexp.structural.adapter;

// Adapter Class to convert HDMI to USB
public class HDMIToUSBAdapter {

  public USBConnectorPlayer convertToUSB(HDMIConnectorPlayer hdmi) {
    String str = hdmi.getContent().toString();
    USBConnectorPlayer ret = new USBConnectorPlayer(str);
    return ret;
  }

}

&lt;/code&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;
Github Code&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;References&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;/h2&gt;
&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;Trebuchet MS&amp;quot;, Trebuchet, Verdana, sans-serif; font-size: 13.2px; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/8851385081535996605/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/02/adapter-design-pattern-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/8851385081535996605'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/8851385081535996605'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/02/adapter-design-pattern-in-java.html' title='Adapter Design Pattern in Java'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK6cU1_n32YroMGUhZH2Czkr0Lt0fy_ITepL1fne-sfzGgg8RY3GcExueDluDAm-ewrZJxgjac6r0bXagAM072nY24ee1p6du6smaNVs5eVa7cng66lYWvaYEjic2_qF6gRpKJuemVU9c/s72-c/adapter.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-1538403455786128889</id><published>2019-02-01T10:18:00.001-08:00</published><updated>2019-02-01T10:18:50.510-08:00</updated><title type='text'>Abstract Factory Design Pattern in Java</title><content type='html'>&lt;h3 class=&quot;post-title entry-title&quot; itemprop=&quot;name&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 22px; font-stretch: normal; font-weight: normal; line-height: normal; margin: 0.75em 0px 0px; position: relative;&quot;&gt;
Type:&amp;nbsp; Creational Design Pattern&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
Summary&lt;/h2&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;h2 style=&quot;color: black; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;div style=&quot;font-family: Times; font-size: 24px; margin: 0px;&quot;&gt;
&lt;span style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 13.2px; font-weight: 400;&quot;&gt;Provide an indirect way to create object that abstracts the creation of families of related objects without directly specifying their concrete Classes. The Factory owns the responsibility create objects. Client is not aware of how to create the object.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px; font-weight: 400; margin: 0px;&quot;&gt;
&lt;/div&gt;
&lt;/h2&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
Details&lt;/h2&gt;
&lt;h2 style=&quot;font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; line-height: 1.4; position: relative; text-indent: 0px; width: 586px;&quot;&gt;
&lt;div style=&quot;margin: 0px;&quot;&gt;
&lt;span style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif; font-size: 13.2px; font-weight: 400;&quot;&gt;It is similar&amp;nbsp;to Factory method design pattern. Only difference is that rather than returning instance of Subclasses, Abstract factory method can create object of Subclasses having different inheritance hierarchy. Generally Abstract Factory will return different Factories, which in turn can be used to Objects&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/h2&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;color: #444444;&quot;&gt;Abstract Factory is used when we want to create objects by Clients and control which instance should be returned. It is used in DAO (Data Access Object).&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;It can be used to design Platform dependent&amp;nbsp;software solution, by providing platform specific implementations.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
Example&lt;/h2&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;div class=&quot;post-body entry-content&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-style: normal; font-weight: 400; letter-spacing: normal; line-height: 1.4; position: relative; text-indent: 0px; text-transform: none; white-space: normal; width: 586px; word-spacing: 0px;&quot;&gt;
&lt;div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
We will extend the example of Sales Man and Different types of Car (SUV, Sedan) to have different type of Car&#39;s in terms of its engine (Electric, Gasoline). Now rather than giving an option to create instance of concrete Class like SUV and Sedan, we have an Abstract Factory Class which can return Factory for either ElectricCar or GasolineCar.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/h2&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
Class Diagram&lt;/h2&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;div class=&quot;post-body entry-content&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-style: normal; letter-spacing: normal; line-height: 1.4; position: relative; text-indent: 0px; text-transform: none; white-space: normal; width: 586px; word-spacing: 0px;&quot;&gt;
&lt;div&gt;
&lt;div style=&quot;font-weight: 400;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: 400; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyqN67lVSFOS0jnZRABbBFNSFw_iee18ESwKMhOddJYHZq2RTK_4_ScpJziCH0doFDFm2daXa5HzcjqLF6hDYJlatz4xOTSMWqRwZpfP52nNs7r24-CNVyomJwcyC2gBA-Lp0Qk6PTC64/s1600/abstractfactory.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;491&quot; data-original-width=&quot;695&quot; height=&quot;225&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyqN67lVSFOS0jnZRABbBFNSFw_iee18ESwKMhOddJYHZq2RTK_4_ScpJziCH0doFDFm2daXa5HzcjqLF6hDYJlatz4xOTSMWqRwZpfP52nNs7r24-CNVyomJwcyC2gBA-Lp0Qk6PTC64/s320/abstractfactory.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-weight: 400;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: #666666;&quot;&gt;
&lt;/h2&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/h2&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
Sample Code&lt;/h2&gt;
&lt;div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;

// interface for Engine Factory
public interface EngineProvider {

  public Car getSedan();

  public Car getSuv();
}

// electric Car Factory
public class ElectricCarFactory implements EngineProvider {

  @Override
  public Car getSedan() {
    return new ElectricSedan();
  }

  @Override
  public Car getSuv() {
    return new ElectricSUV();
  }
}


//gasoline Car Factory
public class GasolineCarFactory implements EngineProvider{
 
  @Override
  public Car getSedan() {
    return new GasolineSedan();
  }

  @Override
  public Car getSuv() {
    return new GasolineSUV();
  }

}


// Car Factory Class which interacts with Clients
public class CarFactory {

  private static EngineProvider factory;

  public static EngineProvider getProvider(String providerName) {
    switch (providerName) {
      case &quot;ELECTRIC&quot;:
        factory = new ElectricCarFactory();
        break;
      case &quot;GASOLINE&quot;:
        factory = new GasolineCarFactory();
        break;

    }
    return factory;
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Github Code&amp;nbsp;&lt;/h2&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
Advantages&lt;/h2&gt;
&lt;h2 style=&quot;color: black; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;div class=&quot;post-body entry-content&quot; itemprop=&quot;description articleBody&quot; style=&quot;color: #666666; font-size: 13.2px; font-weight: 400; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
Provide a standard interface by which family of objects can be created&lt;br /&gt;
Internally in the Abstract factory we can use other design patterns like Flyweight or Proxy or Prototype pattern as per the need.&lt;/div&gt;
&lt;/h2&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
Disadvantages&lt;/h2&gt;
&lt;div&gt;
Sometime supporting for new Object type might lead to changing the interface and refactoring the existing implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif;&quot;&gt;
References&lt;/h2&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;https://en.wikipedia.org/wiki/Abstract_factory_pattern&quot;&gt;https://en.wikipedia.org/wiki/Abstract_factory_pattern&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/1538403455786128889/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/02/abstract-factory-design-pattern-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/1538403455786128889'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/1538403455786128889'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/02/abstract-factory-design-pattern-in-java.html' title='Abstract Factory Design Pattern in Java'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyqN67lVSFOS0jnZRABbBFNSFw_iee18ESwKMhOddJYHZq2RTK_4_ScpJziCH0doFDFm2daXa5HzcjqLF6hDYJlatz4xOTSMWqRwZpfP52nNs7r24-CNVyomJwcyC2gBA-Lp0Qk6PTC64/s72-c/abstractfactory.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-5352069133396875440</id><published>2019-02-01T10:18:00.000-08:00</published><updated>2019-02-01T10:18:23.759-08:00</updated><title type='text'>Factory Design Pattern in Java</title><content type='html'>&lt;h3 class=&quot;post-title entry-title&quot; itemprop=&quot;name&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 22px; font-stretch: normal; font-weight: normal; line-height: normal; margin: 0.75em 0px 0px; position: relative;&quot;&gt;
Type:&amp;nbsp; Creational Design Pattern&lt;/h3&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Summary&lt;/h2&gt;
&lt;br /&gt;
Factory or Factory method Design pattern is a creational design pattern. It helps in loose coupling between client and the actual object that the Client needs. Rather than Client calling the constructor using new operator it calls the Factory and the factory returns the respective object.&lt;br /&gt;
&lt;br /&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Details&lt;/h2&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;Factory method design pattern encapsulate the complexities around creating an object and also uses Inheritance by letting factory method to decide which Subclass instance is needed to be returned.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
Factory method pattern solves the issue of creating object without exposing internal details by&lt;br /&gt;
Provide an interface (Could be an Interface or Abstract Class) and a factory method&lt;br /&gt;
let the Client create the appropriate object by calling the factory method.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Example&lt;/h2&gt;
&lt;br /&gt;
Let&#39;s say we have a company ABC Cars Corp, which manufactures different type&#39;s of Cars (Sedan, SUV etc).&amp;nbsp; Now we want to provide a way to the Sales man to be able to get a Car (object instance) based on Customer preference, and can demo those cars to Customers.&lt;br /&gt;
One option is to provide constructor for those Cars. So that Client can call the constructors and create cars.&lt;br /&gt;
&lt;br /&gt;
That exposes a lot of technical details to the Sales Man. Better would be have a factory/or room which has all available cars. Then the Sales Man can could quickly turn around with available Car to the Customer. That way the learning curve for the Sales Man is very minimum.&lt;br /&gt;
&lt;br /&gt;
Now say down the lane, ABC Cars Corp also starting manufacturing Sports Car. Now the problem is, all the Sales Team needs to know what parameters needed to be sent to create a sample Sports cars.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
Factory method does that by utilizing inheritance and based on the requirement it creates a specific instance of one of the SubClass.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;b&gt;GasolineCarFactory&lt;/b&gt; is the Factory that implements a factory method.&lt;br /&gt;
&lt;b&gt;Car&lt;/b&gt; is extended by two Subclasses &lt;b&gt;SUVCar&lt;/b&gt; and &lt;b&gt;CompactSedan&lt;/b&gt; Car&lt;/div&gt;
&lt;h2 style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Class Diagram&lt;/h2&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&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/AVvXsEhhfky03kEKJd2_fdCA9fZyOw8lkBzroHAfJNJu5rPJfLbIGSIyWgyr65gdXN9WEjF0glSWznc_2kZETEIZIsKrd8cNdi7hvZyNoBW9-R_SdtCKnmab1ug-9VDVzmncL5wkKs-DPfMveIs/s1600/factorymethod.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;424&quot; data-original-width=&quot;621&quot; height=&quot;218&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhfky03kEKJd2_fdCA9fZyOw8lkBzroHAfJNJu5rPJfLbIGSIyWgyr65gdXN9WEjF0glSWznc_2kZETEIZIsKrd8cNdi7hvZyNoBW9-R_SdtCKnmab1ug-9VDVzmncL5wkKs-DPfMveIs/s320/factorymethod.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px; font-weight: 400;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px; font-weight: 400;&quot;&gt;
&lt;/div&gt;
&lt;/h2&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Sample Code&amp;nbsp;&lt;/h2&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;

// subclass of Car
public class CompactSedanCar extends Car {

  @Override
  public String getModel() {
    return &quot;Compact4person&quot;;
  }

  @Override
  public String getPrice() {
    return &quot;20k&quot;;
  }

  @Override
  public String getType() {
    return &quot;COMPACT&quot;;
  }
}

// subclass of Car
public class SUVCar extends Car {

  @Override
  public String getModel() {
    return &quot;SUV2019&quot;;
  }

  @Override
  public String getPrice() {
    return &quot;40k&quot;;
  }

  @Override
  public String getType() {
    return &quot;SUV&quot;;
  }
}

// factory method Class which the Client interact with
public class GasolineCarFactory {

  public Car getCarType(int i) {
    switch (i) {
      case 1:
        return new CompactSedanCar();
      case 2:
        return new SUVCar();
      default:
        return new CompactSedanCar();
    }

  }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Github Code&amp;nbsp;&lt;/h2&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Advantages&lt;/h2&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Centralized interface to create object.&lt;br /&gt;
Easy of refactoring and code reuse.&lt;br /&gt;
Easy discovery for developers.&lt;br /&gt;
Better testable codes.&lt;br /&gt;
Internally the factory method can utilize Prototype design pattern or Flyweight design pattern as per the situation.&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: black; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
Disadvantages&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
Factory method design pattern adds a layer of attraction between Client and real Object.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
when a new SubClass is introduced the factory method needs to update to take that into consideration.&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;
&lt;div style=&quot;-webkit-text-stroke-width: 0px; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;
&lt;/div&gt;
&lt;/h2&gt;
&lt;h2 style=&quot;-webkit-text-stroke-width: 0px; color: black; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-stretch: normal; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: normal; line-height: normal; margin: 1em 0px 0px; orphans: 2; position: relative; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;
When to Use&lt;/h2&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;span style=&quot;font-size: 13.2px;&quot;&gt;&lt;span style=&quot;color: #444444;&quot;&gt;When we want to provide a centralized or common interface to create multiple objects (subclasses)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif; font-size: 13.2px;&quot;&gt;&lt;span style=&quot;color: #444444;&quot;&gt;When we want to hide the complexities of creating instances of those object.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;When we want to control how an object instance should be created.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: &amp;quot;trebuchet ms&amp;quot; , &amp;quot;trebuchet&amp;quot; , &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;If we have complex business logic while creating objects, this design pattern is very useful&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 13.2px;&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;h2&gt;
References&lt;/h2&gt;
&lt;div&gt;
&lt;a href=&quot;https://en.wikipedia.org/wiki/Factory_method_pattern&quot;&gt;https://en.wikipedia.org/wiki/Factory_method_pattern&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/5352069133396875440/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/02/factory-design-pattern-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/5352069133396875440'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/5352069133396875440'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/02/factory-design-pattern-in-java.html' title='Factory Design Pattern in Java'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhfky03kEKJd2_fdCA9fZyOw8lkBzroHAfJNJu5rPJfLbIGSIyWgyr65gdXN9WEjF0glSWznc_2kZETEIZIsKrd8cNdi7hvZyNoBW9-R_SdtCKnmab1ug-9VDVzmncL5wkKs-DPfMveIs/s72-c/factorymethod.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-7162189854380831819</id><published>2019-02-01T10:17:00.002-08:00</published><updated>2019-02-27T23:58:45.276-08:00</updated><title type='text'>Singleton Design Pattern</title><content type='html'>&lt;h3 class=&quot;post-title entry-title&quot; itemprop=&quot;name&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 22px; font-stretch: normal; font-weight: normal; line-height: normal; margin: 0.75em 0px 0px; position: relative;&quot;&gt;
Creational Design Pattern&lt;/h3&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Summary&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
Singleton Design pattern is used to provide a mechanism so that a Class can have only one instance. So when Client try&#39;s to use an instance of Singleton Class it gets the same instance.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
Singleton.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Details&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
Singleton Design pattern make sure that Class which implement this pattern can have only one instance. In case of Java, only one instance in a single JVM.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
For instance if we want only one instance of Cache Class which would store temporary values in memory.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
Singleton pattern is also used in Logger Classes. Logger provide single instance for logging which is used by application code.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
Singleton design pattern is implemented by following mechanism&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
1) Making the constructor private. So that Client cannot create different instances&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
2) Provide a static method, which the client will interact to get the instance of the class&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
3) Do not offer any setter method for the object&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
4) If any getter for individual property returns any object reference, it should return a copy of it&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
5) Taking care of reflection api, to do this we can throw exception in the constructor if it the instance is not initialized.&lt;/div&gt;
&lt;div style=&quot;color: #666666;&quot;&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Advantages&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Provide an option to make sure the instance is single, and if we want to create multiple copies of the object still they will be same.&lt;br /&gt;
&lt;br /&gt;
Useful when we want only one instance of a Class like Service, shared resource etc.&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
Disadvantages&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Depending on the need, DI/IOC can be better suitable.&lt;/div&gt;
&lt;/div&gt;
&lt;h2 style=&quot;font-size: 13.2px;&quot;&gt;
Possible ways to break&lt;/h2&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
Following points need to be thought about possible ways to break the Singleton Class to get more than one instance.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
In java one can use the Reflection API to alter the constructor (private) of the Singleton Class to create different instances. This can be taken care by throwing exception from the private constructor.&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
In Java one can use the clone. It can be achieved by overriding clone() method and throwing Exception.&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
&lt;br /&gt;
Multiple thread can break the Singleton Class. It needs to be taken care as well.&lt;br /&gt;
&lt;br /&gt;
In Java Serialization can cause breaking this contract. It can be handled by returning the instance from&amp;nbsp;readResolve() method.&lt;/div&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Example&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-size: 13.2px;&quot;&gt;
Let&#39;s take example of a House Key. We want only one key for the whole house. We do not want any way to temper the key. We want only one identical key to be available. If any one needs a key, essentially they will get the same single key instance. We can utilize the Singleton pattern to make sure that the key once generated is not modifiable.&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; color: #666666; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
Here the Class &lt;b&gt;HouseKey&lt;/b&gt;&amp;nbsp;implements Singleton pattern, and that Class make sure only one instance of the key exists.&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
Class Diagram&lt;/span&gt;&lt;/span&gt;&lt;/div&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/AVvXsEh1nmdx8fWIB2D_oo-IWeVnWo1Mh6vXr-HSEbe4NOL5htzSXNWestvNrV7UsyLiB2QNlStwalOZFaCTLSRHbnkwFioz-FppG47BwhOLCTMGvnvc8jBrDyN-olGFP8rENbTekQnVvcmguLI/s1600/Singleton.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;240&quot; data-original-width=&quot;456&quot; height=&quot;210&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1nmdx8fWIB2D_oo-IWeVnWo1Mh6vXr-HSEbe4NOL5htzSXNWestvNrV7UsyLiB2QNlStwalOZFaCTLSRHbnkwFioz-FppG47BwhOLCTMGvnvc8jBrDyN-olGFP8rENbTekQnVvcmguLI/s400/Singleton.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;
Sample Code&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;
public class HouseKey implements Serializable {
  private static final long serialVersionUID = 1L;
  private String keyPattern;
  private Long keyDate;

  private static HouseKey instance;

  // private constructor
  private HouseKey() {

  }

  // public get instance method
  public static HouseKey getInstance() {
    if (instance == null) {
      synchronized (HouseKey.class) {
        instance = new HouseKey();
        instance.keyPattern = &quot;key 12345&quot;;
        instance.keyDate = new Date().getTime();
      }
    }
    return instance;
  }


  // safeguard against Clone
  protected Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
  }


  // safeguard against Serialization
  public Object readResolve() {
    return HouseKey.getInstance();
  }

  @Override
  public String toString() {
    return &quot;HouseKey &quot; + this.keyPattern + &quot;  &quot; + this.keyDate;
  }
}&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
Github Code&lt;/span&gt;&lt;span style=&quot;font-size: 13.2px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;color: black; font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;
References&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;font-size: 13.2px;&quot;&gt;
&lt;a href=&quot;https://en.wikipedia.org/wiki/Singleton_pattern&quot;&gt;https://en.wikipedia.org/wiki/Singleton_pattern&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/7162189854380831819/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/02/singleton-design-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/7162189854380831819'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/7162189854380831819'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/02/singleton-design-pattern.html' title='Singleton Design Pattern'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1nmdx8fWIB2D_oo-IWeVnWo1Mh6vXr-HSEbe4NOL5htzSXNWestvNrV7UsyLiB2QNlStwalOZFaCTLSRHbnkwFioz-FppG47BwhOLCTMGvnvc8jBrDyN-olGFP8rENbTekQnVvcmguLI/s72-c/Singleton.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2875775725551936703.post-4441428248097965813</id><published>2019-02-01T10:16:00.000-08:00</published><updated>2019-02-01T10:16:20.450-08:00</updated><title type='text'>Chain of Responsibility Design Pattern in Java</title><content type='html'>&lt;h3 class=&quot;post-title entry-title&quot; itemprop=&quot;name&quot; style=&quot;background-color: white; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; font-size: 22px; font-stretch: normal; font-weight: normal; line-height: normal; margin: 0.75em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Type : Behavioral Design Pattern&lt;/span&gt;&lt;/h3&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;h2 style=&quot;font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;color: #444444; font-size: large; font-weight: normal;&quot;&gt;
Summary&lt;/span&gt;&lt;/h2&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;font-size: 13.2px; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;font-size: 13.2px; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Chain of Responsibility deals with how best we can design a sender - receivers without tight coupling the sender and receivers. Where the sender&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;font-size: 13.2px; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;sends an object to a set of receivers. And one or more than one receiver work on the object that the sender sent.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #444444; font-size: large; font-weight: normal;&quot;&gt;Details&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;As the name suggests, rather than the Client deciding how to process an request object( Say by calling specific method from specific Object), the request is sent though a pipe or chain of different Objects which might be able to process the request.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;The request enters from one end and pass on the request to the next Handler until the request processor is done.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Following are the participants in this pattern&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Handler&lt;/b&gt; : An Interface that Concrete Handlers implements&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Concrete Handler&lt;/b&gt; : Handler implementation that processes the request, or pass to the next handler in the chain.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Client&lt;/b&gt; : Client that utilizes the chain of handlers by invoking first handler.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 style=&quot;font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;color: #444444; font-size: large; font-weight: normal;&quot;&gt;
Advantages&lt;/span&gt;&lt;/h2&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Sender and receiver (handler) can go independently&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Follows plug in like architecture, where we can add more handler without changing existing behaviour.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Easy to maintenance and more testable code.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;post-body entry-content&quot; id=&quot;post-body-4707856467122623620&quot; itemprop=&quot;description articleBody&quot; style=&quot;background-color: white; font-family: &amp;quot;trebuchet ms&amp;quot;, trebuchet, verdana, sans-serif; line-height: 1.4; position: relative; width: 586px;&quot;&gt;
&lt;h2 style=&quot;font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;color: #444444; font-size: large; font-weight: normal;&quot;&gt;Disadvantages&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;If the handlers are not implemented properly it could result in broken processing the request.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Based on the logic wrong order of the Handler in the chain could cause issue.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;span style=&quot;font-size: large; font-weight: normal;&quot;&gt;Example&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Let&#39;s take example of a software system which processes customer request. Broadly we have divided the customer request as of 3 types for now Account, Billing and Technical support related. When client sends the request we loop though all the respective handler and process the request. We also have a Default handler which works when the customer that is asking for something we donot support.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;color: #444444; font-size: large; font-weight: normal;&quot;&gt;Class Diagram&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444; font-size: large; font-weight: normal;&quot;&gt;&lt;br /&gt;&lt;/span&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/AVvXsEjyszkkZZ-HaniNAGeEFuajriNSTyNkMaSGVVxi_XqNIm6A4q76S2-KgenunXe1vvvc76HOixcH_siQ84rwlVTeFAHm4wceNFnNMyHPNLGVllwhROoWV3LZkbdvxELZ6O0VlyINwM953qM/s1600/chain_of_responsibility.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;426&quot; data-original-width=&quot;837&quot; height=&quot;162&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyszkkZZ-HaniNAGeEFuajriNSTyNkMaSGVVxi_XqNIm6A4q76S2-KgenunXe1vvvc76HOixcH_siQ84rwlVTeFAHm4wceNFnNMyHPNLGVllwhROoWV3LZkbdvxELZ6O0VlyINwM953qM/s320/chain_of_responsibility.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h2 style=&quot;font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;color: #444444; font-size: large; font-weight: normal;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;h2 style=&quot;font-stretch: normal; line-height: normal; margin: 1em 0px 0px; position: relative;&quot;&gt;
&lt;span style=&quot;color: #444444; font-size: large; font-weight: normal;&quot;&gt;
Sample Code&amp;nbsp;&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;

// interface to represent a request
public interface Request {

  public String [] getRequestDetails(); //PasswordReset, UnlockAccount, GetAccountDetails
  
  public void setRequestDetails(String [] news); //PasswordReset, UnlockAccount, GetAccountDetails
}

// customer support request object containging request details
public class CustomerSupportRequest implements Request {

  private String[] requestDetails;

  public CustomerSupportRequest(String[] reqType) {
    this.requestDetails = reqType;
  }

}

// abstract request handler
public abstract class RequestHandler {

  RequestHandler nextHandler;

  public void setNextHandler(RequestHandler handler) {
    this.nextHandler = handler;
  }

  public void handleRequest(Request req) {
    processRequest(req);
    if (this.nextHandler != null) {
      this.nextHandler.handleRequest(req);
    }
  }

  public abstract void processRequest(Request req);
  
}


// account related request handler
public class AccountRequestHandler extends RequestHandler {

  @Override
  public void processRequest(Request req) {
    System.out.println(&quot;ACCOUNT DEPARTMENT HANDLED!&quot;);
  }

}

//billing related request handler
public class BillingRequestHandler extends RequestHandler {

  @Override
  public void processRequest(Request req) {

    System.out.println(&quot;BILLING DEPARTMENT HANDLED!&quot;);

  }

}

//technical related request handler
public class TechnicalRequestHandler extends RequestHandler {

  @Override
  public void processRequest(Request req) {
   
    System.out.println(&quot;TECH DEPARTMENT HANDLED!&quot;);
    
  }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;From http://javaexp.blogspot.com/
Posted By Sidd.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javaexp.blogspot.com/feeds/4441428248097965813/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javaexp.blogspot.com/2019/02/chain-of-responsibility-design-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/4441428248097965813'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2875775725551936703/posts/default/4441428248097965813'/><link rel='alternate' type='text/html' href='http://javaexp.blogspot.com/2019/02/chain-of-responsibility-design-pattern.html' title='Chain of Responsibility Design Pattern in Java'/><author><name>Siddhartha B</name><uri>http://www.blogger.com/profile/14614719465205614426</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyszkkZZ-HaniNAGeEFuajriNSTyNkMaSGVVxi_XqNIm6A4q76S2-KgenunXe1vvvc76HOixcH_siQ84rwlVTeFAHm4wceNFnNMyHPNLGVllwhROoWV3LZkbdvxELZ6O0VlyINwM953qM/s72-c/chain_of_responsibility.png" height="72" width="72"/><thr:total>0</thr:total></entry></feed>