<?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-1306415718092887935</id><updated>2024-10-25T01:12:14.623-07:00</updated><category term="Software Testing"/><title type='text'>  Geek Apprentice</title><subtitle type='html'>when the need for something becomes essential, you are forced to find ways achieving it.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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>10</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1306415718092887935.post-9179131524488552198</id><published>2018-02-15T02:38:00.002-08:00</published><updated>2018-02-15T02:38:55.737-08:00</updated><title type='text'>Java 8 Lamdas</title><content type='html'>Java 8 has introduced functional programming.Before java 8 there is nothing we can not not do with java7,but with functional programming we can write better code ,more readable and maintanable code.Lets look at what kind of problems functional programming solves.&lt;br /&gt;
&lt;br /&gt;
In object Oriented programming everything is class and object ,we can not have any code in isolation.Each and every line of code must belong to class.This can be a problem sometime, not always this is problem but there are some situations where we need some block of code to pass as a method argument or we need to return some block of code from method which is not possible with pure object oriented programming.In order to do that we used to pass an object which has that block of code instead of that block of code.&lt;br /&gt;
&lt;br /&gt;
We use inline values in our day to day activities like,&lt;br /&gt;
int a = 10;&lt;br /&gt;
double pi = 3.14;&lt;br /&gt;
&lt;br /&gt;
In functional programming we are assignning code to a variable like&lt;br /&gt;
&lt;br /&gt;
aBlockOfCode = {&lt;br /&gt;
/*&lt;br /&gt;
*code goes here&lt;br /&gt;
*/&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Lets see an example of EMI calculator.We have base interface as EMIcalculator and its concrete implementation as HomeLoanEMICalculator.&lt;br /&gt;
&lt;br /&gt;
EMICalculator interface :&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;interface&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;EMICalculator&lt;/span&gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;calculateEMI&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; principalAmount,&lt;span style=&quot;color: #fbde2d;&quot;&gt;int&lt;/span&gt; noOfMonths);

}

&lt;/pre&gt;
&lt;br /&gt;
Concrete&amp;nbsp;HomeLoanEMICalculator class:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;HomeLoanEMICalculator&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #ff6400; font-style: italic;&quot;&gt;EMICalculator&lt;/span&gt; {

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;calculateEMI&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; principalAmount, &lt;span style=&quot;color: #fbde2d;&quot;&gt;int&lt;/span&gt; noOfMonths) {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; (principalAmount&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;pow(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;9&lt;/span&gt;,noOfMonths))&lt;span style=&quot;color: #fbde2d;&quot;&gt;/&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;pow(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;9&lt;/span&gt;,noOfMonths)&lt;span style=&quot;color: #fbde2d;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;);
    }

}

&lt;/pre&gt;
&lt;br /&gt;
Client Programme:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background:#0c1021;color:#f8f8f8&quot;&gt;
&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;Client&lt;/span&gt; {
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;processrequest&lt;/span&gt;(&lt;span style=&quot;color:#fbde2d&quot;&gt;EMICalculator&lt;/span&gt; emiCalculator,&lt;span style=&quot;color:#fbde2d&quot;&gt;double&lt;/span&gt; principalAmount,&lt;span style=&quot;color:#fbde2d&quot;&gt;int&lt;/span&gt; noOfMonths){
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; emiCalculator&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;calculateEMI(principalAmount, noOfMonths);
    }
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt;[] args) {
        &lt;span style=&quot;color:#fbde2d&quot;&gt;Client&lt;/span&gt; client &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;Client&lt;/span&gt;();
        &lt;span style=&quot;color:#fbde2d&quot;&gt;EMICalculator&lt;/span&gt; emiCalculator &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;HomeLoanEMICalculator&lt;/span&gt;();
        client&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;processrequest(emiCalculator, &lt;span style=&quot;color:#d8fa3c&quot;&gt;500000&lt;/span&gt;, &lt;span style=&quot;color:#d8fa3c&quot;&gt;12&lt;/span&gt;);
        
    }

}

&lt;/pre&gt;
&lt;br /&gt;
This is the pure Object oriented Style of writing code.Here we need pass just behaviour of calculateEMI method but for that we have created HomeLoanEmiCalculator object and pass that object to processRequest() method.See below image,

&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/AVvXsEiUa5esVck6iwxmQZ-QaP7Mk7hd8rqKXebpbUOQL4oxH2vSRWmYvkSiBqDWf8MYspKp7n2AUv32EQLkIoJYc6JpGUm_ur5qGDaWT0JvhbYWgKHw1AapEnRD3WXNYv5NYYCA4OISFMZBJisT/s1600/Capture.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;436&quot; data-original-width=&quot;1033&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUa5esVck6iwxmQZ-QaP7Mk7hd8rqKXebpbUOQL4oxH2vSRWmYvkSiBqDWf8MYspKp7n2AUv32EQLkIoJYc6JpGUm_ur5qGDaWT0JvhbYWgKHw1AapEnRD3WXNYv5NYYCA4OISFMZBJisT/s1600/Capture.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Instead in functional style programming just pass block of code as a argument to processRequest method() like below,i have copied the method signature as it is in blow image.

We just need to remove acccess specifier as values do not have access specifiers and remove method name and return type as java compiler is smart enough to identify return type.After doing changes it looks like below,

&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/AVvXsEjRIam8QEjKHTEVuJUTH3jDX4m1LRh5hon7XPjGUvotuDjMSWnhOcemxBcHXlCJmoAJXdhM-REIGdAwH1ft-iakJ3WJefIC9QHRAYtuiq9JdyjOhUgI8L_s3PJpdfXt0KDqdnwOtcUmjL-3/s1600/Capture1.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;413&quot; data-original-width=&quot;914&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRIam8QEjKHTEVuJUTH3jDX4m1LRh5hon7XPjGUvotuDjMSWnhOcemxBcHXlCJmoAJXdhM-REIGdAwH1ft-iakJ3WJefIC9QHRAYtuiq9JdyjOhUgI8L_s3PJpdfXt0KDqdnwOtcUmjL-3/s1600/Capture1.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
See instead of passing HomeLoanEMICalculator&#39;s object who has calculateEMI() behaviour here i am directly passing passing behaviour.So instead of passing thing which has behaviour pass the behaviour itself is what functional programing does.



&lt;br /&gt;
&lt;div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/9179131524488552198/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/java-8-lamdas.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/9179131524488552198'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/9179131524488552198'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/java-8-lamdas.html' title='Java 8 Lamdas'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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/AVvXsEiUa5esVck6iwxmQZ-QaP7Mk7hd8rqKXebpbUOQL4oxH2vSRWmYvkSiBqDWf8MYspKp7n2AUv32EQLkIoJYc6JpGUm_ur5qGDaWT0JvhbYWgKHw1AapEnRD3WXNYv5NYYCA4OISFMZBJisT/s72-c/Capture.PNG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1306415718092887935.post-4882700315572161395</id><published>2018-02-14T23:14:00.002-08:00</published><updated>2018-02-14T23:14:26.659-08:00</updated><title type='text'>Replace Conditional with Polymorphism</title><content type='html'>&lt;div&gt;
&lt;span style=&quot;color: #444444; font-family: &amp;quot;pt sans&amp;quot; , &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;Replace Conditional with Polymorphism:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: &amp;quot;pt sans&amp;quot; , &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;ul style=&quot;background-color: white; box-sizing: inherit; color: #444444; font-family: &amp;quot;PT Sans&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, Helvetica, Arial, sans-serif; font-size: 16px; margin: 0px 0px 1.5rem; padding: 0px 0px 0px 21px;&quot;&gt;
&lt;li style=&quot;box-sizing: inherit; margin: 0px 0px 1.5rem;&quot;&gt;This technique adheres to the&amp;nbsp;&lt;em style=&quot;box-sizing: inherit;&quot;&gt;Tell-Don&#39;t-Ask&lt;/em&gt;&amp;nbsp;principle: instead of asking an object about its state and then performing actions based on this, it is much easier to simply tell the object what it needs to do and let it decide for itself how to do that.&lt;/li&gt;
&lt;li style=&quot;box-sizing: inherit; margin: 1.5rem 0px;&quot;&gt;Removes duplicate code. You get rid of many almost identical conditionals.&lt;/li&gt;
&lt;li style=&quot;box-sizing: inherit; margin: 1.5rem 0px 0px;&quot;&gt;If you need to add a new execution variant, all you need to do is add a new subclass without touching the existing code (&lt;em style=&quot;box-sizing: inherit;&quot;&gt;Open/Closed Principle&lt;/em&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444; font-family: &amp;quot;pt sans&amp;quot; , &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;Lets see the EMI calculator example with traditional switch case :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #444444; font-family: &amp;quot;pt sans&amp;quot; , &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.refactor;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;EMICalculatorTraditional&lt;/span&gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; double getEMI(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;type&lt;/span&gt;,double amount,&lt;span style=&quot;color: #8da6ce;&quot;&gt;int&lt;/span&gt; periodInMonths){
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;switch&lt;/span&gt;(&lt;span style=&quot;color: #8da6ce;&quot;&gt;type&lt;/span&gt;){
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;HomeLoan&quot;&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;:&lt;/span&gt;
            &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; (amount&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #8da6ce;&quot;&gt;Math&lt;/span&gt;.&lt;span style=&quot;color: #8da6ce;&quot;&gt;pow&lt;/span&gt;(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;9&lt;/span&gt;,periodInMonths))&lt;span style=&quot;color: #fbde2d;&quot;&gt;/&lt;/span&gt;(&lt;span style=&quot;color: #8da6ce;&quot;&gt;Math&lt;/span&gt;.&lt;span style=&quot;color: #8da6ce;&quot;&gt;pow&lt;/span&gt;(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;9&lt;/span&gt;,periodInMonths)&lt;span style=&quot;color: #fbde2d;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;);
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;CarLoan&quot;&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;:&lt;/span&gt;
            &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; (amount&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #8da6ce;&quot;&gt;Math&lt;/span&gt;.&lt;span style=&quot;color: #8da6ce;&quot;&gt;pow&lt;/span&gt;(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;10&lt;/span&gt;,periodInMonths))&lt;span style=&quot;color: #fbde2d;&quot;&gt;/&lt;/span&gt;(&lt;span style=&quot;color: #8da6ce;&quot;&gt;Math&lt;/span&gt;.&lt;span style=&quot;color: #8da6ce;&quot;&gt;pow&lt;/span&gt;(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;10&lt;/span&gt;,periodInMonths)&lt;span style=&quot;color: #fbde2d;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;);
        }
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;0&lt;/span&gt;;
        
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;[] args) {
        EMICalculatorTraditional emiCalculatorTraditional &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; EMICalculatorTraditional();
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.out.println(emiCalculatorTraditional.getEMI(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;HomeLoan&quot;&lt;/span&gt;,&lt;span style=&quot;color: #d8fa3c;&quot;&gt;50000&lt;/span&gt;, &lt;span style=&quot;color: #d8fa3c;&quot;&gt;12&lt;/span&gt;));
    }

&lt;/pre&gt;
&lt;br /&gt;
Now what if i need to add one more new EMICalculator that is PersonalLoanEMI calculator.So what i need to do is check over all the code and look where do i need to make changes and again need to test whole application to check if anything else is not affected by my code change.And also this breaks open &lt;b&gt;Close principle of Object Oriented Programming,&lt;/b&gt;as we are modifying existing classes.So instead of this create base interface for common action and add concrete classes for all brances of the switch case.And in future if you want to add new branch just add new concrete class implementing base interface.So by doing that you do not need to test whole application,you just need to test your added functionality as you have not touched the existing functionality.This is somewhat similar to strategy design pattern.&lt;br /&gt;
See the below modified code:&lt;br /&gt;
&lt;br /&gt;
Base Interface:

&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.refactor;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;interface&lt;/span&gt; EMICalculator {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; double calculateLoanInterest(double amount,&lt;span style=&quot;color: #8da6ce;&quot;&gt;int&lt;/span&gt; periodInMonths);

}

&lt;/pre&gt;
&lt;br /&gt;
Concrete Classes:&lt;br /&gt;
1.HomeLoanEMICalculator:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;com.dp.refactor&lt;/span&gt;;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;HomeLoanEMICalculator&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #ff6400; font-style: italic;&quot;&gt;EMICalculator&lt;/span&gt; {

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; rate &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;9&lt;/span&gt;;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;calculateLoanInterest&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; amount, &lt;span style=&quot;color: #fbde2d;&quot;&gt;int&lt;/span&gt; periodInMonths) {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; (amount&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;rate&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;pow(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;rate,periodInMonths))&lt;span style=&quot;color: #fbde2d;&quot;&gt;/&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;pow(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;rate,periodInMonths)&lt;span style=&quot;color: #fbde2d;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;);
    }

}

&lt;/pre&gt;
&lt;br /&gt;
2.CarLoanEMIcalculator:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;com.dp.refactor&lt;/span&gt;;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;CarLoanEMICalculator&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #ff6400; font-style: italic;&quot;&gt;EMICalculator&lt;/span&gt; {

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; rate &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;10&lt;/span&gt;;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;calculateLoanInterest&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;double&lt;/span&gt; amount, &lt;span style=&quot;color: #fbde2d;&quot;&gt;int&lt;/span&gt; periodInMonths) {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; (amount&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;rate&lt;span style=&quot;color: #fbde2d;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;pow(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;rate,periodInMonths))&lt;span style=&quot;color: #fbde2d;&quot;&gt;/&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;pow(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;rate,periodInMonths)&lt;span style=&quot;color: #fbde2d;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;);
    }

}

&lt;/pre&gt;
&lt;br /&gt;
Client :
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.refactor;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;Client&lt;/span&gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;[] args) {
        
        EMICalculator emiCalculator &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; HomeLoanEMICalculator();
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.out.println(emiCalculator.calculateLoanInterest(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;50000&lt;/span&gt;, &lt;span style=&quot;color: #d8fa3c;&quot;&gt;12&lt;/span&gt;));
    }

}

&lt;/pre&gt;
Now if you want to add new EMICalculator type as PersonalLoanEMIcalculator you just need to add one more concrete PersonalLoanEMICalculator class implementingEMICalculator base interface.&lt;br /&gt;
Thats all&lt;br /&gt;
Thanks</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/4882700315572161395/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/replace-conditional-with-polymorphism.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/4882700315572161395'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/4882700315572161395'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/replace-conditional-with-polymorphism.html' title='Replace Conditional with Polymorphism'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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-1306415718092887935.post-3543129044808747307</id><published>2018-02-14T01:52:00.000-08:00</published><updated>2018-02-14T01:52:28.651-08:00</updated><title type='text'>Java coding standards</title><content type='html'>&lt;b&gt;1.Synchronized classes Vector,Hashtable,Stack and StringBuffer should not be used:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
Early classes of Java API ,such as Vector,hashtable,StringBuffer&amp;nbsp; were synchronized to make&amp;nbsp; them thread safe.Unfortunately ,synchronization has a big negative impact on performance,even when using these collections from single threaded environment.It is better to use their non synchronized replacements.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;ArrayList or LinkedList instead of Vector&lt;/li&gt;
&lt;li&gt;Deque instead of stack&lt;/li&gt;
&lt;li&gt;&amp;nbsp;HashMap instead of HashTable&lt;/li&gt;
&lt;li&gt;StringBuilder instead of StringBuffer&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;2.Collection.isEmpty should be used&amp;nbsp; to test for emptiness:&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Using Collection.size() to test for emptiness works but it has significant performance issue.Instead of Collection.size(),we should go for Collection.isEmpty() as its time complexity is O(1) whereas Collction.size() has complexity of O(n).&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;3.Empty array or collections should be returned instead of null:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Returning null instead of actual array or collection forces callers of the method to explicitly test for null ,making them more complex and less readable.&lt;/div&gt;
&lt;div&gt;
So instead of this below code,&lt;/div&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;public static List&amp;lt;Result&amp;gt; getResults() {
  return null;                             // Noncompliant
}

&lt;/pre&gt;
use this one,
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;public static List&amp;lt;Result&amp;gt; getResults() {
  return Collections.emptyList();          // Compliant
}

&lt;/pre&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;4.Return of boolean expressions should be wrapped into if then else statement:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Instead of below code ,
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #ff6400;&quot;&gt;if&lt;/span&gt;(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;age&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;18&lt;/span&gt;){
&lt;span style=&quot;color: #d8fa3c;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;true&lt;/span&gt;;
}&lt;span style=&quot;color: #d8fa3c;&quot;&gt;else&lt;/span&gt;{
&lt;span style=&quot;color: #d8fa3c;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;false&lt;/span&gt;;
}
&lt;/pre&gt;
Preferable one is,
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #ff6400;&quot;&gt;return&lt;/span&gt; (&lt;span style=&quot;color: #d8fa3c;&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #d8fa3c;&quot;&gt;18&lt;/span&gt;)
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;5.Method parameters,caught exceptions and foreach variables should not reassigned:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
While it is technically correct to assign to parameters from method bodies .it reduces code readability .Instead all parameters should be treated as final.&lt;/div&gt;
Instead of below code,
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;public &lt;span style=&quot;color: #fbde2d;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;add&lt;/span&gt;(int a, int b) {
    a = a + b;                      // Noncompliant
    /* additional logic */
    return a;                       // Seems like the parameter is returned as is, what is the point?
  }

&lt;/pre&gt;
Below one is preferable,
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt; public &lt;span style=&quot;color: #fbde2d;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;add&lt;/span&gt;(int a, int b) {
    return a + b;                   // Compliant
  }

&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/3543129044808747307/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/java-coding-standards.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/3543129044808747307'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/3543129044808747307'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/java-coding-standards.html' title='Java coding standards'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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-1306415718092887935.post-596014855998430957</id><published>2018-02-13T01:28:00.000-08:00</published><updated>2018-02-13T02:14:52.705-08:00</updated><title type='text'>Observer Design Pattern</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; line-height: 115%;&quot;&gt;
&lt;div style=&quot;background-color: white; text-align: left;&quot;&gt;
The observer pattern is a software design pattern in which an object,called the subject maintains a list of its dependents,called observers and notifies them automatically of any state changes,usually by calling one of their methods.Define a one to many dependency between objects so that when one object changes,all its dependents are notified and updated automatically.&lt;/div&gt;
&lt;div style=&quot;background-color: white; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Lets take an example where we have social media post and there are multiple subscribers to that post such that if any new user add comments or like the post,all the subscribers should know about this activity.So lets implement the same with observer pattern.Here Post will act like Subject and Users will act like observers so that whenever any comment is added to post or it is liked ,all the users(observers) gets notified.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;  package com.dp.observer;

  &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;interface&lt;/span&gt; Observer {
    
    &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; update(&lt;span style=&quot;color: #8da6ce;&quot;&gt;Object&lt;/span&gt; object);

  }

&lt;/pre&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;com.dp.observer&lt;/span&gt;;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;java.util.ArrayList&lt;/span&gt;;
&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;java.util.List&lt;/span&gt;;
&lt;span style=&quot;color: #aeaeae;&quot;&gt;/*
 * Observer
 */&lt;/span&gt;
&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;User&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #ff6400; font-style: italic;&quot;&gt;Observer&lt;/span&gt;{
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;List&amp;lt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;&amp;gt;&lt;/span&gt; notifications &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;ArrayList&amp;lt;&amp;gt;&lt;/span&gt;();
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; name;

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;update&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;Object&lt;/span&gt; object) {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;println(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Hey ,&quot;&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;name&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;object&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;toString());
        notifications&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;add(object&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;toString());
    }

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;List&amp;lt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;getNotifications&lt;/span&gt;() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; notifications;
    }

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;setNotifications&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;List&amp;lt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;&amp;gt;&lt;/span&gt; notifications) {
        this&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;notifications &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; notifications;
    }

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;getName&lt;/span&gt;() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; name;
    }

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;setName&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; name) {
        this&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;name &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; name;
    }
    

}

&lt;/pre&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.observer;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; java.util.ArrayList;
&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; java.util.&lt;span style=&quot;color: #8da6ce;&quot;&gt;List&lt;/span&gt;;
&lt;span style=&quot;color: #aeaeae;&quot;&gt;/*
 * This is the subject
 */&lt;/span&gt;
&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;Post&lt;/span&gt; {
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; postContents;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;List&lt;/span&gt;&amp;lt;User&amp;gt; subscribers &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; ArrayList&amp;lt;&amp;gt;();
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;&amp;gt; comments &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; ArrayList&amp;lt;&amp;gt;();
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;int&lt;/span&gt; likes;
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; getPostContents() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; postContents;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; setPostContents(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; postContents) {
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;this&lt;/span&gt;.postContents &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; postContents;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;List&lt;/span&gt;&amp;lt;User&amp;gt; getSubscribers() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; subscribers;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; setSubscribers(&lt;span style=&quot;color: #8da6ce;&quot;&gt;List&lt;/span&gt;&amp;lt;User&amp;gt; subscribers) {
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;this&lt;/span&gt;.subscribers &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; subscribers;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;&amp;gt; getComments() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; comments;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; setComments(&lt;span style=&quot;color: #8da6ce;&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;&amp;gt; comments) {
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;this&lt;/span&gt;.comments &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; comments;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;int&lt;/span&gt; getLikes() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; likes;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; setLikes(&lt;span style=&quot;color: #8da6ce;&quot;&gt;int&lt;/span&gt; likes) {
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;this&lt;/span&gt;.likes &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; likes;
    }
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; incrementLike(){
        likes&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;;
        notifyObservers(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Post like is inscremented by 1&quot;&lt;/span&gt;);
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; decrementLikes(){
        likes&lt;span style=&quot;color: #fbde2d;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;-&lt;/span&gt;;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; addComment(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;comment&lt;/span&gt;){
        comments.&lt;span style=&quot;color: #8da6ce;&quot;&gt;add&lt;/span&gt;(&lt;span style=&quot;color: #8da6ce;&quot;&gt;comment&lt;/span&gt;);
        notifyObservers(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;New comment is added for the post as:&quot;&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #8da6ce;&quot;&gt;comment&lt;/span&gt;);
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; addSubscriber(User user){
        subscribers.&lt;span style=&quot;color: #8da6ce;&quot;&gt;add&lt;/span&gt;(user);
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; notifyObservers(&lt;span style=&quot;color: #8da6ce;&quot;&gt;Object&lt;/span&gt; object){
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;for&lt;/span&gt; (Observer observer &lt;span style=&quot;color: #fbde2d;&quot;&gt;:&lt;/span&gt; subscribers) {
            observer.update(object);
        }
    }

}

&lt;/pre&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.observer;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;Client&lt;/span&gt; {
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;[] args) {
        
        Post post &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; Post();
        post.setPostContents(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Going to manali&quot;&lt;/span&gt;);
        post.setLikes(&lt;span style=&quot;color: #d8fa3c;&quot;&gt;10&lt;/span&gt;);
        
        User user &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; User();
        user.setName(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Nitin&quot;&lt;/span&gt;);
        post.addSubscriber(user);
        
        user &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; User();
        user.setName(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Sagar&quot;&lt;/span&gt;);
        post.addSubscriber(user);
        
        post.incrementLike();
        post.addComment(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Happy journey&quot;&lt;/span&gt;);
        
        
    }

}

&lt;/pre&gt;
Output:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;Hey ,Nitin Post like &lt;span style=&quot;color: #fbde2d;&quot;&gt;is&lt;/span&gt; inscremented &lt;span style=&quot;color: #fbde2d;&quot;&gt;by&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;
Hey ,Sagar Post like &lt;span style=&quot;color: #fbde2d;&quot;&gt;is&lt;/span&gt; inscremented &lt;span style=&quot;color: #fbde2d;&quot;&gt;by&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;1&lt;/span&gt;
Hey ,Nitin New comment &lt;span style=&quot;color: #fbde2d;&quot;&gt;is&lt;/span&gt; added &lt;span style=&quot;color: #fbde2d;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;the&lt;/span&gt; post &lt;span style=&quot;color: #fbde2d;&quot;&gt;as&lt;/span&gt;:Happy journey
Hey ,Sagar New comment &lt;span style=&quot;color: #fbde2d;&quot;&gt;is&lt;/span&gt; added &lt;span style=&quot;color: #fbde2d;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;the&lt;/span&gt; post &lt;span style=&quot;color: #fbde2d;&quot;&gt;as&lt;/span&gt;:Happy journey
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/596014855998430957/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/observer-design-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/596014855998430957'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/596014855998430957'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/observer-design-pattern.html' title='Observer Design Pattern'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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-1306415718092887935.post-2805077269845791080</id><published>2018-02-06T22:26:00.000-08:00</published><updated>2018-02-06T22:26:08.142-08:00</updated><title type='text'>Builder Design Pattern</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0in; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, serif; font-size: 12pt; line-height: 150%;&quot;&gt;Builder pattern is a
creational design pattern used to assemble complex object.&lt;/span&gt;&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 12.0pt; line-height: 150%; mso-bidi-font-family: Arial; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;he intent of the Builder design pattern is to
separate the construction of a complex object from its representation. By doing
so the same construction process can create different representations.&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, serif; font-size: 12pt; line-height: 150%;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0in; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 12.0pt; line-height: 150%; mso-bidi-font-family: Arial; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Lets think we have
to create invoice object ,inside invoice we can have invoice date and
amount.What isf invoice has purchase order attributes associated with it then
we need to add attributes associated with purchase order in invoice class.So
for that either we can extend base invoice class to add new parameters for
purchase order attributes but this leads to creating more number of subclasses
as the new parameters come in future.Another alternative tho this is add
parameters in the same class and add new constructor to create the object.Again
same thing more number of attributes&amp;nbsp;and combination of those
attributes&amp;nbsp;leads to more number of constructors .So solution to this is
Builder pattern..&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, serif; font-size: 12pt; line-height: 150%;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0in; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 12.0pt; line-height: 150%; mso-bidi-font-family: Arial; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0in; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 12.0pt; line-height: 150%; mso-bidi-font-family: Arial; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;The Builder design
pattern describes how to solve such problems:&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, serif; font-size: 12pt; line-height: 150%;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 1.2pt; margin-left: 19.2pt; mso-list: l0 level1 lfo1; mso-margin-top-alt: auto; tab-stops: list .5in; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;font-family: Symbol; font-size: 10pt; line-height: 150%;&quot;&gt;·&lt;/span&gt;&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 12.0pt; line-height: 150%; mso-bidi-font-family: Arial; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Encapsulate creating
and assembling the parts of a complex object in separate&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background: rgb(248, 249, 250); font-family: Georgia, serif; font-size: 12pt; line-height: 150%;&quot;&gt;builder&lt;/span&gt;&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 12.0pt; line-height: 150%; mso-bidi-font-family: Arial; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;object.&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, serif; font-size: 12pt; line-height: 150%;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white; line-height: 150%; margin-bottom: 1.2pt; margin-left: 19.2pt; mso-list: l0 level1 lfo1; mso-margin-top-alt: auto; tab-stops: list .5in; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: #222222; font-family: Symbol; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;&quot;&gt;·&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 12.0pt; line-height: 150%; mso-bidi-font-family: Arial; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;A class delegates object creation to
a builder&amp;nbsp;object instead of creating the objects directly.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0in; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0in; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, serif; font-size: 12pt; line-height: 150%;&quot;&gt;A class can delegate object
creation process to builder.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;


&lt;pre style=&quot;background:#0c1021;color:#f8f8f8&quot;&gt;package com.dp.builder;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;interface&lt;/span&gt; InvoiceBuilder {
    
    Invoice build();

}

&lt;/pre&gt;
&lt;br/&gt;
&lt;pre style=&quot;background:#0c1021;color:#f8f8f8&quot;&gt;package com.dp.builder;

&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.sql.&lt;span style=&quot;color:#8da6ce&quot;&gt;Date&lt;/span&gt;;
&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.util.&lt;span style=&quot;color:#8da6ce&quot;&gt;List&lt;/span&gt;;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;Invoice&lt;/span&gt; {
    
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;name&lt;/span&gt;;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; double amount;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;Date&lt;/span&gt; purchaseOrderDate;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; purchaseOrderNumber;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt;&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;items&lt;/span&gt;;
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; getName() {
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;name&lt;/span&gt;;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; setName(&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;name&lt;/span&gt;) {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color:#8da6ce&quot;&gt;name&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;name&lt;/span&gt;;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; double getAmount() {
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; amount;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; setAmount(double amount) {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;this&lt;/span&gt;.amount &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; amount;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;Date&lt;/span&gt; getPurchaseOrderDate() {
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; purchaseOrderDate;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; setPurchaseOrderDate(&lt;span style=&quot;color:#8da6ce&quot;&gt;Date&lt;/span&gt; purchaseOrderDate) {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;this&lt;/span&gt;.purchaseOrderDate &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; purchaseOrderDate;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; getPurchaseOrderNumber() {
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; purchaseOrderNumber;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; setPurchaseOrderNumber(&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; purchaseOrderNumber) {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;this&lt;/span&gt;.purchaseOrderNumber &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; purchaseOrderNumber;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt;&gt; getItems() {
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;items&lt;/span&gt;;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; setItems(&lt;span style=&quot;color:#8da6ce&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt;&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;items&lt;/span&gt;) {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color:#8da6ce&quot;&gt;items&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;items&lt;/span&gt;;
    }
    @Override
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;toString&lt;/span&gt;() {
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;Invoice [name=&quot;&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;name&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;, amount=&quot;&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; amount &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;, purchaseOrderDate=&quot;&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; purchaseOrderDate
                &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;, purchaseOrderNumber=&quot;&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; purchaseOrderNumber &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;, items=&quot;&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;items&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;]&quot;&lt;/span&gt;;
    }
    
    
    
    

}

&lt;/pre&gt;
&lt;br/&gt;
&lt;pre style=&quot;background:#0c1021;color:#f8f8f8&quot;&gt;&lt;span style=&quot;color:#fbde2d&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;com.dp.builder&lt;/span&gt;;

&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;java.sql.Date&lt;/span&gt;;
&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;java.util.List&lt;/span&gt;;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;InvoiceBuilderImpl&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color:#ff6400;font-style:italic&quot;&gt;InvoiceBuilder&lt;/span&gt;{
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; name;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;double&lt;/span&gt; amount;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;Date&lt;/span&gt; purchaseOrderDate;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; purchaseOrderNumber;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;List&amp;lt;&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt;&gt;&lt;/span&gt; items;
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;InvoiceBuilderImpl&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;invoiceName&lt;/span&gt;(&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; name){
        this&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;name &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; name;
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; this;
    }
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;InvoiceBuilderImpl&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;invoiceAmount&lt;/span&gt;(&lt;span style=&quot;color:#fbde2d&quot;&gt;double&lt;/span&gt; amount){
        this&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;amount &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; amount;
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; this;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;InvoiceBuilderImpl&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;purchaseOrderDate&lt;/span&gt;(&lt;span style=&quot;color:#fbde2d&quot;&gt;Date&lt;/span&gt; purchaseOrderDate){
        this&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;purchaseOrderDate &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; purchaseOrderDate;
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; this;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;InvoiceBuilderImpl&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;invoiceAmount&lt;/span&gt;(&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt; purchaseOrderNumber){
        this&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;purchaseOrderNumber &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; purchaseOrderNumber;
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; this;
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;InvoiceBuilderImpl&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;invoiceItems&lt;/span&gt;(&lt;span style=&quot;color:#fbde2d&quot;&gt;List&amp;lt;&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt;&gt;&lt;/span&gt; items){
        this&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;items &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; items;
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; this;
    }

    &lt;span style=&quot;color:#fbde2d&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;Invoice&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;build&lt;/span&gt;() {
            &lt;span style=&quot;color:#fbde2d&quot;&gt;Invoice&lt;/span&gt; invoice &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;Invoice&lt;/span&gt;();
            invoice&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;setName(name);
            invoice&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;setAmount(amount);
            invoice&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;setPurchaseOrderDate(purchaseOrderDate);
            invoice&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;setPurchaseOrderNumber(purchaseOrderNumber);
            invoice&lt;span style=&quot;color:#fbde2d&quot;&gt;.&lt;/span&gt;setItems(items);
            &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; invoice;
    }
    

}

&lt;/pre&gt;
&lt;br/&gt;
&lt;pre style=&quot;background:#0c1021;color:#f8f8f8&quot;&gt;package com.dp.builder;

&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.sql.&lt;span style=&quot;color:#8da6ce&quot;&gt;Date&lt;/span&gt;;
&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.util.Arrays;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;Client&lt;/span&gt; {
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt;[] args) {
        
        &lt;span style=&quot;color:#aeaeae&quot;&gt;//create purchase order invoice &lt;/span&gt;
        Invoice poInvoice &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; InvoiceBuilderImpl().invoiceName(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;poInvocie&quot;&lt;/span&gt;).invoiceAmount(&lt;span style=&quot;color:#d8fa3c&quot;&gt;5000&lt;/span&gt;).invoiceItems(Arrays.asList(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;product1&quot;&lt;/span&gt;,&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;product2&quot;&lt;/span&gt;)).purchaseOrderDate(&lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;Date&lt;/span&gt;(&lt;span style=&quot;color:#d8fa3c&quot;&gt;12&lt;/span&gt;, &lt;span style=&quot;color:#d8fa3c&quot;&gt;12&lt;/span&gt;, &lt;span style=&quot;color:#d8fa3c&quot;&gt;12&lt;/span&gt;)).build();
        
        &lt;span style=&quot;color:#aeaeae&quot;&gt;//create non purchase order invoice&lt;/span&gt;
        Invoice nonPoInvoice &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; InvoiceBuilderImpl().invoiceName(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;nonPoInvoce&quot;&lt;/span&gt;).invoiceAmount(&lt;span style=&quot;color:#d8fa3c&quot;&gt;10000&lt;/span&gt;).build();
    
    }

}

&lt;/pre&gt;



&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/2805077269845791080/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/builder-design-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/2805077269845791080'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/2805077269845791080'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/builder-design-pattern.html' title='Builder Design Pattern'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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-1306415718092887935.post-2709095936030573782</id><published>2018-02-04T22:31:00.003-08:00</published><updated>2018-02-04T22:31:59.788-08:00</updated><title type='text'>Object Pool Design Pattern</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;margin-bottom: .0001pt; margin: 0in; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;Object pool is
a software creational design pattern which keeps pool of objects and returns
object from there only.&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, serif; font-size: 14pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: .0001pt; margin: 0in; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;When
it is necessary to work with a large number of objects that are particularly
expensive to instantiate and each object is only needed for a short period of
time, the performance of an entire application may be adversely affected. An
object pool design pattern may be deemed desirable in cases such as these.Object
pooling can offer a significant performance boost in situations where the cost
of initializing a class instance is high and the rate of instantiation and
destruction of a class is high – in this case objects can frequently be reused,
and each reuse saves a significant amount of time.The object pool design
pattern creates a set of objects that may be reused.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;Object pool can solves
object creation problems in two ways.&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, serif; font-size: 14pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;In
first way,whenever an object is requested ,if object is available then it is
returned and if object is not available new object is created.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;In second way,whenever
an object is requested,it is checked if an object is available,if available
then object is returned if not exception may be thrown or application needs to
wait for object to get free.In first case pool size is not fixed,and in second
case pool size is fixed and it can behave like singleton as we are creating
fixed no of objects and we will be using only those throughout the application.&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, serif; font-size: 14pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: .0001pt; margin: 0in; text-align: justify;&quot;&gt;





&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;Following
is the code for case one:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;&lt;b&gt;Abstract Object Pool:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0in 0in 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Georgia&amp;quot;,serif; font-size: 14.0pt; mso-bidi-font-family: &amp;quot;Calibri Light&amp;quot;; mso-bidi-theme-font: major-latin;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.objectpool;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; java.util.Enumeration;
&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; java.util.concurrent.ConcurrentHashMap;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; abstract &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;ObjectPoolAbstract&lt;/span&gt;&amp;lt;T&amp;gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;int&lt;/span&gt; exipreTime;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; ConcurrentHashMap&amp;lt;T,Long&amp;gt; locked,available;
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; ObjectPoolAbstract() {
        &lt;span style=&quot;color: #aeaeae;&quot;&gt;//object that is created will be available for 40 seconds after that it will be not used any more.&lt;/span&gt;
        exipreTime &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;4000&lt;/span&gt;;
        locked &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; ConcurrentHashMap&amp;lt;&amp;gt;();
        available &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; ConcurrentHashMap&amp;lt;&amp;gt;();
    }

    &lt;span style=&quot;color: #aeaeae;&quot;&gt;//these methods are specific to concrete implementation and hence are kept as abstract&lt;/span&gt;
    protected abstract T create();
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; abstract boolean &lt;span style=&quot;color: #8da6ce;&quot;&gt;validate&lt;/span&gt;(T t);
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; abstract &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; expire(T t);
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; synchronized &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; checkIn(T t){
        locked.remove(t);
        available.put(t, &lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.currentTimeMillis());
    }
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; synchronized T checkOut(){
         T t &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;null&lt;/span&gt;;
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;if&lt;/span&gt;(available.&lt;span style=&quot;color: #8da6ce;&quot;&gt;size&lt;/span&gt;() &amp;gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;0&lt;/span&gt;){
             Enumeration&amp;lt;T&amp;gt; e &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; available.keys();
              &lt;span style=&quot;color: #fbde2d;&quot;&gt;while&lt;/span&gt; (e.hasMoreElements()) {
                  t &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; e.nextElement();
                    &lt;span style=&quot;color: #fbde2d;&quot;&gt;if&lt;/span&gt; ((&lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.currentTimeMillis() &lt;span style=&quot;color: #fbde2d;&quot;&gt;-&lt;/span&gt; available.&lt;span style=&quot;color: #8da6ce;&quot;&gt;get&lt;/span&gt;(t)) &amp;gt; exipreTime) {
                      &lt;span style=&quot;color: #aeaeae;&quot;&gt;// object has expired and will be removed from available map also because expire time has reach&lt;/span&gt;
                        &lt;span style=&quot;color: #aeaeae;&quot;&gt;//objects will remain in available map till expire time is not reached from last reference to the that objects&lt;/span&gt;
                      available.remove(t);
                      expire(t);
                      t &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;null&lt;/span&gt;;
                    } &lt;span style=&quot;color: #fbde2d;&quot;&gt;else&lt;/span&gt; {
                      &lt;span style=&quot;color: #fbde2d;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: #8da6ce;&quot;&gt;validate&lt;/span&gt;(t)) {
                        available.remove(t);
                        locked.put(t, &lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.currentTimeMillis());
                        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; (t);
                      } 
                      &lt;span style=&quot;color: #fbde2d;&quot;&gt;else&lt;/span&gt; {
                          &lt;span style=&quot;color: #aeaeae;&quot;&gt;// object failed validation&lt;/span&gt;
                          available.remove(t);
                          expire(t);
                          t &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;null&lt;/span&gt;;
                        }
                    }
            
              }
        }   
            t &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; create();
            locked.put(t, &lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.currentTimeMillis());
            &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; t;
    }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;div style=&quot;margin-bottom: .0001pt; margin: 0in; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-size: 13.5pt;&quot;&gt;&lt;b&gt;2.Concrete Implmentation of ObjectPool:&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: #222222; font-family: &amp;quot;Calibri Light&amp;quot;,sans-serif; font-size: 10.5pt; mso-ascii-theme-font: major-latin; mso-bidi-theme-font: major-latin; mso-hansi-theme-font: major-latin;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;com.dp.objectpool&lt;/span&gt;;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;java.io.IOException&lt;/span&gt;;
&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;java.net.Socket&lt;/span&gt;;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;SocketPoolConcrete&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;extends&lt;/span&gt; &lt;span style=&quot;color: #ff6400; font-style: italic;&quot;&gt;ObjectPoolAbstract&amp;lt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;Socket&lt;/span&gt;&amp;gt;&lt;/span&gt;{
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; server;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;int&lt;/span&gt; portNo;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;SocketPoolConcrete&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt; server,&lt;span style=&quot;color: #fbde2d;&quot;&gt;int&lt;/span&gt; portNo) {
        this&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;server &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; server;
        this&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;portNo &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; portNo;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;Socket&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;create&lt;/span&gt;() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;Socket&lt;/span&gt; socket &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;null&lt;/span&gt;;
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;try&lt;/span&gt; {
            socket &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;Socket&lt;/span&gt;(server,portNo);
        } &lt;span style=&quot;color: #fbde2d;&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #fbde2d;&quot;&gt;IOException&lt;/span&gt; e) {
            &lt;span style=&quot;color: #aeaeae;&quot;&gt;// TODO Auto-generated catch block&lt;/span&gt;
            e&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;printStackTrace();
        } 
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; socket;
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;boolean&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;validate&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;Socket&lt;/span&gt; t) {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;!&lt;/span&gt;((&lt;span style=&quot;color: #fbde2d;&quot;&gt;Socket&lt;/span&gt;)t)&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;isClosed();
    }
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;expire&lt;/span&gt;(&lt;span style=&quot;color: #fbde2d;&quot;&gt;Socket&lt;/span&gt; t) {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;try&lt;/span&gt; {
            ((&lt;span style=&quot;color: #fbde2d;&quot;&gt;Socket&lt;/span&gt;)t)&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;close();
        } &lt;span style=&quot;color: #fbde2d;&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #fbde2d;&quot;&gt;IOException&lt;/span&gt; e) {
            &lt;span style=&quot;color: #aeaeae;&quot;&gt;// TODO Auto-generated catch block&lt;/span&gt;
            e&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;printStackTrace();
        }
        
    }

}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Georgia, serif; font-size: 13.5pt; line-height: 107%;&quot;&gt;&lt;b&gt;3. Client&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Georgia&amp;quot;,serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.objectpool;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;import&lt;/span&gt; java.net.Socket;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;Client&lt;/span&gt; {

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;[] args) {
        &lt;span style=&quot;color: #aeaeae;&quot;&gt;//get client socket object to communicate with server at localhost:8080(to run this server socket must listens at localhost:8080)&lt;/span&gt;
        SocketPoolConcrete socketPool &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; SocketPoolConcrete(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Localhost&quot;&lt;/span&gt;,&lt;span style=&quot;color: #d8fa3c;&quot;&gt;8080&lt;/span&gt;);
        Socket socket &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; socketPool.checkOut();
    
        &lt;span style=&quot;color: #aeaeae;&quot;&gt;/*.
         * 
         * do your task
        .*/&lt;/span&gt;
        
        socketPool.checkIn(socket);
    }
}

&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #222222; font-family: sans-serif; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/2709095936030573782/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/object-pool-design-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/2709095936030573782'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/2709095936030573782'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/object-pool-design-pattern.html' title='Object Pool Design Pattern'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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-1306415718092887935.post-3892162616096382420</id><published>2018-02-01T23:07:00.002-08:00</published><updated>2018-02-02T01:17:30.067-08:00</updated><title type='text'>Factory Design Pattern</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Factory design pattern is one of the creational design pattern for creating objects with respect to Object Oriented systems.What is the most usual method of creating object,most people think that using new keyword.But creating object with new keyword makes components tightly coupled,if we need to change object creation process then we need to change it in all places in our programme. So in order to make components loosely coupled Factory pattern is one solution.Loose coupling is achieved in Factory pattern by implenting at interfaces and not at concrete classes.&lt;br /&gt;
&lt;br /&gt;
The intent of factory pattern is:&lt;br /&gt;
&lt;br /&gt;
1.Creates the object without exposing object creation logic to client&lt;br /&gt;
2.refers to newly created object through common interface(through factory)&lt;br /&gt;
&lt;br /&gt;
Lets take look at example where we need various DB connectors like &lt;b&gt;MsSQLConnector&lt;/b&gt;,&lt;b&gt;OracleConnector &lt;/b&gt;and &lt;b&gt;SQLServerConnector&lt;/b&gt;,we can use factory design pattern.&lt;br /&gt;
&lt;br /&gt;
1.Connector Type enum:
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.factory;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; enum ConnectorType {
    MsSQlConnector, OracleConnector, SQLserverConnector
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
2.Connector interface:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.factory;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;interface&lt;/span&gt; Connector {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;connect&lt;/span&gt;();

}

&lt;/pre&gt;
&lt;br /&gt;
3.Concrete MsSQLConnector class:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;com.dp.factory&lt;/span&gt;;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;MsSQLConnector&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #ff6400; font-style: italic;&quot;&gt;Connector&lt;/span&gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;MsSQLConnector&lt;/span&gt;() {
        connect();
    }

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;connect&lt;/span&gt;() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;println(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Connecting to MsSQLDatabase.!!!!!!!!&quot;&lt;/span&gt;);

    }

}

&lt;/pre&gt;
&lt;br /&gt;
3.Concrete OracleConnector class:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;com.dp.factory&lt;/span&gt;;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;OracleConector&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #ff6400; font-style: italic;&quot;&gt;Connector&lt;/span&gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;OracleConector&lt;/span&gt;() {
        connect();
    }

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;connect&lt;/span&gt;() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;println(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Connecting to oracle database.!!!!!!!!!&quot;&lt;/span&gt;);

    }

}

&lt;/pre&gt;
&lt;br /&gt;
3.Concrete SQLServerConnector class:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;com.dp.factory&lt;/span&gt;;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;SQLServerConnector&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #ff6400; font-style: italic;&quot;&gt;Connector&lt;/span&gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;SQLServerConnector&lt;/span&gt;() {
        connect();
    }

    &lt;span style=&quot;color: #fbde2d;&quot;&gt;@Override&lt;/span&gt;
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;connect&lt;/span&gt;() {
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #fbde2d;&quot;&gt;.&lt;/span&gt;println(&lt;span style=&quot;color: #61ce3c;&quot;&gt;&quot;Connecting to SQLConnector database.!!&quot;&lt;/span&gt;);
        
    }

}

&lt;/pre&gt;
&lt;br /&gt;
4.Factory class to get connector type:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.factory;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;DBConnectorFactory&lt;/span&gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;static&lt;/span&gt; Connector getDBConnector(ConnectorType &lt;span style=&quot;color: #8da6ce;&quot;&gt;type&lt;/span&gt;){
        Connector connector &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;null&lt;/span&gt;;
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;switch&lt;/span&gt;(&lt;span style=&quot;color: #8da6ce;&quot;&gt;type&lt;/span&gt;){
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;case&lt;/span&gt; MsSQlConnector &lt;span style=&quot;color: #fbde2d;&quot;&gt;:&lt;/span&gt;
            connector &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; MsSQLConnector();
            &lt;span style=&quot;color: #fbde2d;&quot;&gt;break&lt;/span&gt;;
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;case&lt;/span&gt; OracleConnector &lt;span style=&quot;color: #fbde2d;&quot;&gt;:&lt;/span&gt;
            connector &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; OracleConector();
            &lt;span style=&quot;color: #fbde2d;&quot;&gt;break&lt;/span&gt;;
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;case&lt;/span&gt; SQLserverConnector &lt;span style=&quot;color: #fbde2d;&quot;&gt;:&lt;/span&gt; 
            connector &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;new&lt;/span&gt; SQLServerConnector();
            &lt;span style=&quot;color: #fbde2d;&quot;&gt;break&lt;/span&gt;;
        }
        &lt;span style=&quot;color: #fbde2d;&quot;&gt;return&lt;/span&gt; connector;
    }

}

&lt;/pre&gt;
&lt;br /&gt;
5.Client:
&lt;br /&gt;
&lt;pre style=&quot;background: #0c1021; color: #f8f8f8;&quot;&gt;package com.dp.factory;

&lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #ff6400;&quot;&gt;Client&lt;/span&gt; {
    
    &lt;span style=&quot;color: #fbde2d;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #fbde2d;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #8da6ce;&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color: #fbde2d;&quot;&gt;String&lt;/span&gt;[] args) {
        Connector connector &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #d8fa3c;&quot;&gt;null&lt;/span&gt;;
        connector &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; DBConnectorFactory.getDBConnector(ConnectorType.MsSQlConnector);
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.out.println(connector.getClass().getName());
        connector  &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt;DBConnectorFactory.getDBConnector(ConnectorType.OracleConnector);
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.out.println(connector.getClass().getName());
        connector &lt;span style=&quot;color: #fbde2d;&quot;&gt;=&lt;/span&gt; DBConnectorFactory.getDBConnector(ConnectorType.SQLserverConnector);
        &lt;span style=&quot;color: #8da6ce;&quot;&gt;System&lt;/span&gt;.out.println(connector.getClass().getName());
    }

}

&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the simple and intutive implmentaion for factory pattern.What if we need to add one more concrete DBConnector as DB2Connector?...we need to modify factory class to add one more switch case for DB2Connector type of objects.But this is against Open Close principle of Object oriented programming.&lt;br /&gt;
&lt;br /&gt;
Need to change Factory and client and Concrete classes as well:&lt;br /&gt;
So the code looks like:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background:#0c1021;color:#f8f8f8&quot;&gt;package com.dp.factory;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; enum ConnectorType {
    MsSQlConnector, OracleConnector, SQLserverConnector
}


package com.dp.factory;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;interface&lt;/span&gt; Connector {
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;connect&lt;/span&gt;();

}


package com.dp.factory;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;MsSQLConnector&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;implements&lt;/span&gt; Connector {
    
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt;{
        DBConnectorFactory.getInstacne().addConnector(ConnectorType.MsSQlConnector,  MsSQLConnector.class);
    }
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; MsSQLConnector() {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;connect&lt;/span&gt;();
    }

    @Override
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;connect&lt;/span&gt;() {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;System&lt;/span&gt;.out.println(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;Connecting to MsSQLDatabase.!!!!!!!!&quot;&lt;/span&gt;);

    }

}


package com.dp.factory;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;OracleConector&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;implements&lt;/span&gt; Connector {
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt;{
        DBConnectorFactory.getInstacne().addConnector(ConnectorType.OracleConnector,  MsSQLConnector.class);
    }
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; OracleConector() {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;connect&lt;/span&gt;();
    }

    @Override
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;connect&lt;/span&gt;() {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;System&lt;/span&gt;.out.println(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;Connecting to oracle databse.!!!!!!!!!&quot;&lt;/span&gt;);

    }

}




package com.dp.factory;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;SQLServerConnector&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;implements&lt;/span&gt; Connector {
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt;{
        DBConnectorFactory.getInstacne().addConnector(ConnectorType.SQLserverConnector,  MsSQLConnector.class);
    }
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; SQLServerConnector() {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;connect&lt;/span&gt;();
    }

    @Override
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;connect&lt;/span&gt;() {
        &lt;span style=&quot;color:#8da6ce&quot;&gt;System&lt;/span&gt;.out.println(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;Connecting to SQLConnector database.!!&quot;&lt;/span&gt;);
        
    }

}


package com.dp.factory;

&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.lang.reflect.Constructor;
&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.lang.reflect.InvocationTargetException;
&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.util.HashMap;
&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.util.Map;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;DBConnectorFactory&lt;/span&gt; {
    &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt; DBConnectorFactory factory &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#d8fa3c&quot;&gt;null&lt;/span&gt;;
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; DBConnectorFactory(){
        
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;private&lt;/span&gt; Map registeredConnectors &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; HashMap&amp;lt;&gt;();
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; addConnector(ConnectorType connectorType,Class connectortypeClass){
        registeredConnectors.put(connectorType, connectortypeClass);
    }
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; Connector getDBConnectorFromMap(ConnectorType &lt;span style=&quot;color:#8da6ce&quot;&gt;type&lt;/span&gt;) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Class connectorClass &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; (Class)registeredConnectors.&lt;span style=&quot;color:#8da6ce&quot;&gt;get&lt;/span&gt;(&lt;span style=&quot;color:#8da6ce&quot;&gt;type&lt;/span&gt;);
        Constructor[] connectorConstructor &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; connectorClass.getConstructors();
        &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; (Connector)connectorConstructor[&lt;span style=&quot;color:#d8fa3c&quot;&gt;0&lt;/span&gt;].newInstance(&lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;Object&lt;/span&gt;[] { });
    }
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt; DBConnectorFactory getInstacne(){
        
        &lt;span style=&quot;color:#fbde2d&quot;&gt;if&lt;/span&gt;(factory &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#d8fa3c&quot;&gt;null&lt;/span&gt;){
            factory &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;new&lt;/span&gt; DBConnectorFactory();
            &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; factory;
        }
        &lt;span style=&quot;color:#fbde2d&quot;&gt;else&lt;/span&gt;{
            &lt;span style=&quot;color:#fbde2d&quot;&gt;return&lt;/span&gt; factory;
        }
    }

}


package com.dp.factory;

&lt;span style=&quot;color:#fbde2d&quot;&gt;import&lt;/span&gt; java.lang.reflect.InvocationTargetException;

&lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color:#ff6400&quot;&gt;Client&lt;/span&gt; {
    
    
    &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt;{
        &lt;span style=&quot;color:#8da6ce&quot;&gt;try&lt;/span&gt;
        {
            Class.forName(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;com.dp.factory.MsSQLConnector&quot;&lt;/span&gt;);
            Class.forName(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;com.dp.factory.OracleConector&quot;&lt;/span&gt;);
            Class.forName(&lt;span style=&quot;color:#61ce3c&quot;&gt;&quot;com.dp.factory.SQLServerConnector&quot;&lt;/span&gt;);
        }
        &lt;span style=&quot;color:#8da6ce&quot;&gt;catch&lt;/span&gt; (ClassNotFoundException any)
        {
            any.printStackTrace();
        }
    }
    &lt;span style=&quot;color:#fbde2d&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color:#fbde2d&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color:#8da6ce&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color:#fbde2d&quot;&gt;String&lt;/span&gt;[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        
        
        Connector connector &lt;span style=&quot;color:#fbde2d&quot;&gt;=&lt;/span&gt; DBConnectorFactory.getInstacne().getDBConnectorFromMap(ConnectorType.MsSQlConnector);
    }

}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/3892162616096382420/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/factory-design-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/3892162616096382420'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/3892162616096382420'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/factory-design-pattern.html' title='Factory Design Pattern'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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-1306415718092887935.post-1702494065582430256</id><published>2018-02-01T02:02:00.002-08:00</published><updated>2018-02-01T03:15:49.953-08:00</updated><title type='text'>RESTful API Designing guidelines — The best practices</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
What are RESTful web services:&lt;br /&gt;
&lt;br /&gt;
REST is an architectural style which provides direction for building distributed ans loosely coupled systems.REST is not linked to any particular platform or its an idea to develop services to function similar to the web.&lt;br /&gt;
&lt;br /&gt;
REST is nothing but REpresentational State Transfer.It describes how one system can communicate with another over web.We can compare REST services as methods in our programme,in order achieve certain tasks in our programme we are calling some methods from our programme,in the same way we are creating REST web services.I would rather say REST web services and methods on web that can be consumed by calle(clients like browser,mobile apps or other consumers.).So two systems can comminucate over web with the help&amp;nbsp; REST web services so there must by some data that will be exchanged between two parties.That data should be having some state at particular instance and has some form of representaion,so this is nothing but REpresentation state transfer.For example,we have an emplopyee object having id,name and address as attributes.So curently that object has id=1,name=&#39;xyz&#39; and address=&#39;India&#39; as current state and we pass it as JSON.So this is what as REpresentaional state transfer.&lt;br /&gt;
&lt;br /&gt;
Most people think that REST is HTTP but REST is not HTTP because in its most general form REST exists to assist the concept of verb against an arbitary collection of nouns.&lt;br /&gt;
&lt;br /&gt;
One most important point is what is the difference between website and a web service.A website is consumed by humans whereas web services are consumed by programmed clients.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
REST web services rests on three mail things and those are Resources,Representation and Verbs.URL in web services is a sentence where resources are nouns and verbs are HTTP methods.&lt;br /&gt;
&lt;br /&gt;
Resources:Resources are the endpoints of API.They may be business entities like student,employee and so on.While working on rest web services ,the first task is to identify resources and how they link with each other.&lt;br /&gt;
&lt;br /&gt;
Representation: Representation is nothing but a way to showcase resources to clients.REST supports most of the representation formats including but not limited to json,xml.&lt;br /&gt;
&lt;br /&gt;
Verbs:Verbs decides what actions to perform an given resource.Verbs can be GET,PUT,POST and DELETE etc.&lt;br /&gt;
&lt;br /&gt;
Guidelines for designing REST web services:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;1)API Endpoint:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Lets say we have resource as employee and we need to write REST API&#39;s for that,so we can start as:&lt;br /&gt;
&lt;br /&gt;
employee/createEmploye&lt;br /&gt;
employee/deleteEmployee&lt;br /&gt;
employee/updateEmployee&lt;br /&gt;
employee/createEmployee&lt;br /&gt;
&lt;br /&gt;
This will results in tons of API&#39;s and it can duplicate the code and increases maintance cost associated with the API&#39;s.So what&#39;s wrong with the above way to create API&#39;s.URL&amp;nbsp; should only contain resources and not verbs,let the HTTP Verbs do the job and do not include those verbs in URL.&lt;br /&gt;
&lt;br /&gt;
So the API endpoints should look like,&lt;br /&gt;
&lt;br /&gt;
URL : employee/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;HTTP verb:POST&amp;nbsp; &amp;nbsp; &amp;nbsp; will create employee resource&lt;br /&gt;
URL: employee/{id}&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;HTTP verb:GET&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;will get employee specified by id&lt;br /&gt;
URL: employee/{id}&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;HTTP verb:DELETE&amp;nbsp; will delete employee specified by id&lt;br /&gt;
URL:employee/{id}&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; HTTP verb:PUT&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;will update employee specified by id&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets say we have resources under resource like we have employee under department we can have,&lt;br /&gt;
&lt;br /&gt;
URL : department/{id}/employee&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; HTTP verb:POST&amp;nbsp; &amp;nbsp; &amp;nbsp;create employee under department specified by id&lt;br /&gt;
URL:&amp;nbsp; department/{deptId}/employee/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; HTTP verb:GET&amp;nbsp; &amp;nbsp; &amp;nbsp; get all employee under department specified by deptId&lt;br /&gt;
URL: department/{deptId}/employee/{empId}&amp;nbsp; &amp;nbsp; HTTP verb:GET&amp;nbsp; &amp;nbsp; get employee with id empId under department specified by deptId&lt;br /&gt;
URL: department/{deptId}/employee/{empId}&amp;nbsp; &amp;nbsp; HTTP DELETE&amp;nbsp; &amp;nbsp; delete employee with id empId under department specified by deptId&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;2)HTTP methods:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
1.GET : retrieve an existing resource.&lt;br /&gt;
2.POST : Create new entry for resource.POST is non idempotent method which means every time a request is made it has different effect.&lt;br /&gt;
3.PUT : Modify an existing resource.PUT is idempotent method which means every time request is made it has same effect.&lt;br /&gt;
4.DELETE : Delete an existing resource.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;3)HTTP response codes:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Whenever a request is made to server it must return some response either failed or success,so that client can aware of it.For successful operation it should return &lt;b&gt;2xx &lt;/b&gt;series status codes.For client request error &lt;b&gt;4xx &lt;/b&gt;series status codes and for server side error &lt;b&gt;5xx &lt;/b&gt;status codes should be return&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
4)Versioning:&lt;br /&gt;
&lt;br /&gt;
Versioning should be considered when the servers are unable to maintain the compatibility.Versioning is done if some clients wants different behaviuor than others.Versioning can be accomplish using version number in URL or it can be achieved using accept and content type with version numbers instead of specifying in the URL.&lt;br /&gt;
Lets say we have Order as resource and we need to create two versions of this resource where in one API order resource should not include items within it which is V1 and other API which includes items within the order then we can have,&lt;br /&gt;
&lt;br /&gt;
Example of version number in URL:&lt;br /&gt;
&lt;br /&gt;
v1/order/{id}&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; HTTP verb:GET&amp;nbsp; &amp;nbsp; &amp;nbsp; get order specified by id(does not include items in order)&lt;br /&gt;
v2/order/{id}&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; HTTP verb:GET&amp;nbsp; &amp;nbsp; &amp;nbsp; get order specified by id(include items in order)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of version number in header:&lt;br /&gt;
&lt;br /&gt;
order/{id} Accept:application/json;version=1&amp;nbsp; &amp;nbsp;get order specified by id(does not include items in order)&lt;br /&gt;
order/{id} Accept:application/json;version=2&amp;nbsp; &amp;nbsp;get order specified by id(include items in order)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
These are my own opinions on designing RESTful web services,your views are also welcome.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/1702494065582430256/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/restful-api-designing-guidelinesthe.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/1702494065582430256'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/1702494065582430256'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/02/restful-api-designing-guidelinesthe.html' title='RESTful API Designing guidelines — The best practices'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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-1306415718092887935.post-3297872116205942515</id><published>2018-01-25T05:01:00.002-08:00</published><updated>2018-01-25T06:34:56.723-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Software Testing"/><title type='text'>Rest Assured</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.6em; font-weight: 400; margin: 20px 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
REST ASSURED : Testing API endpoints&lt;/h3&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: arimo, sans-serif; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: #545454; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;With the increase popularity of RESTful services, there is a need for fast and lightweight tool for REST webservices&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #6a6a6a; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;testing&lt;/span&gt;&lt;span style=&quot;color: #545454; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;automation. One of the most popular choice is&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #6a6a6a; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;Rest&lt;/span&gt;&lt;span style=&quot;color: #545454; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #6a6a6a; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;Assured&lt;/span&gt;&lt;span style=&quot;color: #545454; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;framework from Jayway. It introduces simplicity of&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #6a6a6a; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;testing&lt;/span&gt;&lt;span style=&quot;color: #545454; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;web services from dynamic languages like groovy or ruby to java&lt;/span&gt;&lt;span style=&quot;color: #545454; font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: x-small;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-size: 1.05em;&quot;&gt;&amp;nbsp;REST-assured provides a lot of nice features,such as&lt;/span&gt;&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;DSL-like syntax, XPath-Validation, Specification Reuse, easy file uploads&lt;/span&gt;&lt;span style=&quot;font-size: 1.05em;&quot;&gt;&amp;nbsp;and with those features we will handle automated API testing much&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;skimlinks-unlinked&quot; style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;easier.the&lt;/span&gt;&lt;span style=&quot;font-size: 1.05em;&quot;&gt;&amp;nbsp;preferred and current way is to use the newer BDD (Behavior Driven Development) like given-when-then&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;skimlinks-unlinked&quot; style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;syntax.When&lt;/span&gt;&lt;span style=&quot;font-size: 1.05em;&quot;&gt;&amp;nbsp;you create BDD features, the majority of its scenarios will be made up of three main Gherkin keywords: Given, When and Then. Each one has a particular function:&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-size: 1.05em;&quot;&gt;
&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Given&lt;/span&gt;&amp;nbsp;=&amp;nbsp;&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;a context/Getting a system in a particular state&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 1.05em;&quot;&gt;
&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;When&lt;/span&gt;&amp;nbsp;=&amp;nbsp;&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;something happens/Poke it&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-size: 1.05em;&quot;&gt;
&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Then&lt;/span&gt;&amp;nbsp;=&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;we expect some outcome/examine the new state&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.6em; font-weight: 400; margin: 20px 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 28.392px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Why REST-assured?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
Let’s look at what is expected from a tool that helps in writing automated tests for REST APIs and how REST-assured lives upto these expectations.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Easy HTTP Request Building and Execution :&lt;/span&gt;&amp;nbsp;Requesting building comprises of defining many things like query params, cookies, headers, path params and request body. Using it’s DSL, REST-assured hides away the complexity of building and executing an HTTP request behind a fluent interface. We will see this in action in the next section.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Response Assertions :&amp;nbsp;&lt;/span&gt;REST-assured also handles, within its library, the parsing of responses. It provides many constructs for making assertions on cookies, response headers and response body. For doing assertions on response body it provides JsonPath for assertions on JSON responses and XmlPath for assertions on XML responses. It also also uses Java Hamcrest Matchers to make readable assertions.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;border: 0px; box-sizing: border-box; font-family: inherit; font-size: 18.6322px; font-style: inherit; font-weight: 600; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Ability to write Clean Code :&lt;/span&gt;&amp;nbsp;The real value of automated tests is realized when they are easy to understand and takes very less effort to maintain them. In the dictionary of REST-assured’s DSL, you can also find constructs for writing in&lt;br /&gt;
Given-When-Then style. Using this style helps in specifying pre-conditions under the Given section, behavior under test under the When section and verifications under the Then section. This helps in maintaining a clear separation of concerns within a test, thus leading to a very readable test.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
Lets’ take a look at examples:&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
1.I want to get list of all countries.The example is as:&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;i&gt;RequestSpecification request=new RequestSpecBuilder().build();&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;ResponseSpecification response=new ResponseSpecBuilder().build();&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;i&gt;@Test(enabled=true)&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;public void getAllCountries()&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;{&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;given()&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.spec(request)&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.get(API_ENDPOINT_FOR_GETTING_ALL_CONTRIES)&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.then()&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.statusCode(200);&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;}&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
Here we are making get request to API_ENDPOINT and checking for statuscode 200.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;font-size: 1.05em;&quot;&gt;Now lets see how to pass queryparams in get request.Suppose i want to pass ContryCode and get all cities.The&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: 1.05em;&quot;&gt;request looks like:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; box-sizing: border-box; color: #555555; font-family: Arimo, sans-serif; font-size: 1.05em; line-height: 1.6; margin-bottom: 1.2em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;i&gt;@Test(enabled=true)&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;public void getAllCities()&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;{&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;given()&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.spec(request)&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.queryParam(“countryId”, “AR”)&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.get(API_ENDPOINT_FOR_GETTING_CITIES_BY_COUNTYID)&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.then()&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;.statusCode(200);&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;}&lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/3297872116205942515/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/01/rest-assured-testing-api-endpoints-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/3297872116205942515'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/3297872116205942515'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/01/rest-assured-testing-api-endpoints-in.html' title='Rest Assured'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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-1306415718092887935.post-8262858463920895426</id><published>2018-01-25T04:40:00.001-08:00</published><updated>2018-01-25T06:34:25.262-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Software Testing"/><title type='text'>TDD (Test Driven Devlopment)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
TDD-Test Driven Development&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Dark secret behind TDD is - TDD has nothing to do with testing its all about requirements and design driven development.&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
TDD is an approach for software development where developers write unit tests before start working on actual implementation.Here our tests represents the requirements that we will be implementing.So unit tests in TDD drives the design and development of code.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Whenever new feature is to be developed,unit tests are written first.At first all tests must fail because the implementation for those tests is not there.If your tests are pass then the functionality you will be developing is already present there so no need to duplicate the code.Once your tests fails then you start developing the code which will pass those tests.This will result in avoiding writing unecesary code which is not required.Once all the tests are passed you can refactor the code to make it upto the standards.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
The simple concept of TDD is to write and correct the failed tests before writing new code (before development). This helps to avoid duplication of code as we write a small amount of code at a time in order to pass tests.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Before TDD&lt;/b&gt; people follows the traditional approach as People read the requirements or get requirements from stake holders or product owner and come up with some details and then come up with architecture design, code analysis , db design, etc.. and then later start writing the code and write tests around the newly developed functionality.once this is tested, they would unearth bugs and bugs would be logged for the same, so here there is back and forth between developer and tester.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Which increases the cycle of development and releasing the product or release&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;In TDD&lt;/b&gt; people will start writing test cases, which would obviously fail at the first hand as there is no code or functionality written so far to satisfy the test.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Now, team would start writing code to pass the test and indirectly address the functionality to make sure the tests pass, in this way basically the code is more qualitative and the number of to and fros would be minimized.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
The steps in TDD are:&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
1. Write a test that tests a specific part of the user story or feature you&#39;re developing.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
2. Run the test. It should fail right now, because you haven&#39;t written any code yet. If your test doesn&#39;t fail your existing code already behaves correctly or more likely your test doesn&#39;t test anything.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
3. Make the simplest possible change to your code that ensures the test succeeds. It&#39;s important that you do not make changes that go beyond your test, even if you know that you&#39;re going to change that later. This ensures that you do not introduce behavior that is untested because in TDD your tests are a major part of your documentation. It is also important that you do not touch anything that is unrelated to the test your trying to satisfy.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
4. Run your test again. It should now succeed. But if it fails go back to 1. Make sure that your test is actually correct before you try to find the problem in your code.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
5. When your test is satisfied refactor your code. Since in step 3 you did not touch anything unrelated to the test you may have introduced code smells. But because all of your existing code is now fully covered by tests (one of the reasons why TDD would be used in the first place) you can now safely reorganize it, and as long as your tests still succeed you&#39;re doing it right.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
There are three stages in TDD drive approAch for modern software development.First one is &lt;i&gt;RED &lt;/i&gt;stage,&lt;i&gt;GREEN &lt;/i&gt;Stage and third one is &lt;i&gt;refactor&lt;/i&gt;.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;In RED stage&lt;/i&gt;,developers write tests for missing behaviour and when executed those tests will fail because desired implementation is not implemented yet.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;In Green stage&lt;/i&gt;,minimal code that makes the failing tests to pass is written and no extra code is written.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;In Reactoring&lt;/i&gt;,as in GREEN stage we have written minimal code to pass ths the failing tests,here in this stage code refactoring is done to make code to the coding standards and remove any other code smells.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Test driven development is a micro cycle that needs to follow throughout the development. Ideally the code written with TDD approach should have:&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
1.All the test pass&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
2.It has no duplication&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
3.It communicates its intent&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
4.No unwanted parts exist&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
One of the chief limitations of TDD is the fact that tests can sometimes be incorrectly conceived or applied. This may result in units that do not perform as expected in the real world. Even if all the units work perfectly in isolation in all anticipated scenarios, end users may encounter situations not imagined by the developers and testers.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
The final results of TDD are only as good as the tests that have been used, the thoroughness with which they have been done and the extent to which they mimic conditions encountered by users of the final product.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://geekapprentice.blogspot.com/feeds/8262858463920895426/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://geekapprentice.blogspot.com/2018/01/tdd-test-driven-devlopment.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/8262858463920895426'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1306415718092887935/posts/default/8262858463920895426'/><link rel='alternate' type='text/html' href='http://geekapprentice.blogspot.com/2018/01/tdd-test-driven-devlopment.html' title='TDD (Test Driven Devlopment)'/><author><name>npnitin</name><uri>http://www.blogger.com/profile/06317717820753436710</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></feed>