<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Md. Kawser Habib </title>
    <description>The latest articles on DEV Community by Md. Kawser Habib  (@khabib97).</description>
    <link>https://dev.to/khabib97</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F639836%2F3092d0ca-7163-4d6b-89e4-c293d732ddf0.png</url>
      <title>DEV Community: Md. Kawser Habib </title>
      <link>https://dev.to/khabib97</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khabib97"/>
    <language>en</language>
    <item>
      <title>Legacy Interface</title>
      <dc:creator>Md. Kawser Habib </dc:creator>
      <pubDate>Sun, 30 May 2021 10:47:03 +0000</pubDate>
      <link>https://dev.to/khabib97/legacy-interface-edo</link>
      <guid>https://dev.to/khabib97/legacy-interface-edo</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Suppose you are working on a &lt;strong&gt;very very big&lt;/strong&gt; java project.&lt;br&gt;
&lt;em&gt;Congratulations!!!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Problem Scenario
&lt;/h3&gt;

&lt;p&gt;Suddenly your senior comes to your desk and says- "Hey X, we need to add a new method to &lt;em&gt;XYX&lt;/em&gt; interface".&lt;/p&gt;

&lt;h3&gt;
  
  
  Inside your brain
&lt;/h3&gt;

&lt;p&gt;Holy Crap! &lt;/p&gt;

&lt;p&gt;Impossible, I can't do this. &lt;/p&gt;

&lt;p&gt;It will break all existing code blocks. &lt;/p&gt;

&lt;p&gt;No, it is not possible. &lt;/p&gt;

&lt;h3&gt;
  
  
  Why it is not possible?
&lt;/h3&gt;

&lt;p&gt;According to &lt;a href="https://docs.oracle.com/javase/tutorial/java/concepts/interface.html" rel="noopener noreferrer"&gt;oracle documents&lt;/a&gt;, a java interface is a group of related methods with empty bodies. Like-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose, already 100 classes implement Bicycle interface. So, if you want to add a new method in the Bicycle interface-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to modify 100 classes. Complete the method's body in each and every classes, who implements bicycle interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Can it be a feasible solution?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;No, never. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;Java 8 introduced completely a new concept to overcome this crucial situation. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is &lt;strong&gt;default method&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using default keyword, you can add a &lt;em&gt;complete method&lt;/em&gt; in a java interface(&lt;em&gt;magic&lt;/em&gt;), this will help you maintain existing legacy code, untouched.&lt;/p&gt;

&lt;p&gt;With a new default method in Bicycle interface-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Bicycle {

    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);

    //method with body in an interface
    default void saveTheWord(){
      System.out.println("Haha! it is a magic");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you don't need to modify 100 existing classes(if you wish you can). However, after this point, newly implemented classes can use/override this method if they want.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extending Interfaces That Contain Default Methods
&lt;/h3&gt;

&lt;p&gt;When you extend an interface that contains a default method, you can do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not mention the default method at all, which lets your extended interface inherit the default method.&lt;/li&gt;
&lt;li&gt;Redeclare the default method, which makes it abstract.&lt;/li&gt;
&lt;li&gt;Redefine the default method, which overrides it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cheers
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi716wjvko2i9tl38841y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi716wjvko2i9tl38841y.jpg" alt="Alt Text"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Java ‘length’ dilemma: String.length() vs Array.length</title>
      <dc:creator>Md. Kawser Habib </dc:creator>
      <pubDate>Sat, 29 May 2021 16:27:22 +0000</pubDate>
      <link>https://dev.to/khabib97/java-length-dilemma-string-length-vs-array-length-3o84</link>
      <guid>https://dev.to/khabib97/java-length-dilemma-string-length-vs-array-length-3o84</guid>
      <description>&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;A very common mistake during the development time is .length or .length() for array and string. Okay, now we try to understand and remember it so that we can remember it easily.&lt;/p&gt;

&lt;h3&gt;
  
  
  For array, length is a property.
&lt;/h3&gt;

&lt;p&gt;It is a property and we pre-define it, and we only once can initialize an array size so, it is actually a final value.&lt;/p&gt;

&lt;h3&gt;
  
  
  For string, length is a function.
&lt;/h3&gt;

&lt;p&gt;We can not access a string’s length as property because it is not pre-defined like array, java needs to calculate the length for string… !!!&lt;/p&gt;

&lt;h2&gt;
  
  
  Deep Drive
&lt;/h2&gt;

&lt;p&gt;Interestingly java string is backed by array implementation actually (byte array). In that case, Sun Microsystem could use array’s length for string. Besides in Java, String is immutable, so there is no chance of length modification(!). But what is the philosophy behind this length function?&lt;/p&gt;

&lt;p&gt;Mainly for maintaining encapsulation philosophy,(hide direct access of length variable). Moreover, the String class implements CharSequence, and length() came from it (Historically, String API is older than CharSequence !).&lt;/p&gt;

&lt;h3&gt;
  
  
  String.length() =&amp;gt; O(1) or O(n)?
&lt;/h3&gt;

&lt;p&gt;Order O(1), because it just returns the underlying byte array’s length.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is this good practice: for(int i = 0; i &amp;lt; string.length(); i++) ?
&lt;/h3&gt;

&lt;p&gt;Yes, it is ok, if we consider modern compiler’s are smart enough and they optimize code and remove function call.&lt;br&gt;
Function call has some extra overheads so, I do not prefer this coding style. It is better to save the length value in other variables in case of this type of iteration.&lt;br&gt;
...&lt;br&gt;
&lt;em&gt;Medium Link: &lt;a href="https://khabib97.medium.com/java-length-dilemma-string-length-vs-array-length-ff0552ba7968"&gt;https://khabib97.medium.com/java-length-dilemma-string-length-vs-array-length-ff0552ba7968&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Proxy VS Reverse Proxy (less theory)</title>
      <dc:creator>Md. Kawser Habib </dc:creator>
      <pubDate>Sat, 29 May 2021 16:22:09 +0000</pubDate>
      <link>https://dev.to/khabib97/proxy-vs-reverse-proxy-less-theory-60b</link>
      <guid>https://dev.to/khabib97/proxy-vs-reverse-proxy-less-theory-60b</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Okay, it was a very common scenario during your university life, there were some friends who gave our attendance.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That actually means, during roll call, our friends respond on behalf of us. Simply, this is a proxy.&lt;/p&gt;

&lt;h3&gt;
  
  
  In Computer Science?
&lt;/h3&gt;

&lt;p&gt;Things are almost similar. But need some extra explanations.&lt;/p&gt;

&lt;p&gt;Before starting a discussion, we need to clarify 2 terms.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Proxy&lt;/strong&gt; means actually &lt;strong&gt;forward-proxy&lt;/strong&gt;. So, we should always remember it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reverse-Proxy&lt;/strong&gt; means &lt;strong&gt;reverse-proxy&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Forward-Proxy aka Proxy
&lt;/h3&gt;

&lt;p&gt;In the client-server model sometimes clients need to hide their identity (from the server or …).&lt;/p&gt;

&lt;p&gt;Sometimes some governments block Facebook/Instagram/Twitter in their countries, but people somehow manage to visit these sites. In most cases, it is the proxy who plays the main role(VPN is not proxy, we will discuss it next time)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;General Scenario&lt;/strong&gt;: User → Facebook = Successfully Access&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blocked Scenario&lt;/strong&gt;: User → &lt;strong&gt;Government Block&lt;/strong&gt; →Facebook = Access Denied&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Proxy Scenario&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--epvX7rDY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhnjc9bt3dfrf3dv09p6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--epvX7rDY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhnjc9bt3dfrf3dv09p6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;You&lt;/strong&gt;: Hey Proxy Server, I need to visit Facebook.&lt;br&gt;
&lt;strong&gt;Proxy-Server&lt;/strong&gt;: Ok, don't worry. I will communicate on behalf of you.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Your actual request(primary request) to a proxy server, inside the request there is another request(secondary-request) for the proxy server to access Facebook on behalf of you(!). So, blocakage does not work, your primary request is not for accessing Facebook. Additionally, you hide your actual IP from Facebook.&lt;/p&gt;

&lt;p&gt;Actually, using forward-proxy, clients(users) hide their identities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E_Oxn5vy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca6bwksxqci5shfnlplx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E_Oxn5vy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca6bwksxqci5shfnlplx.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reverse-Proxy
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Sometimes it is necessary for a server to hide its identity (!!! Seriously)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, when a server hides its identity, it is call reverse-proxy.&lt;/p&gt;

&lt;p&gt;It sounds wired. But true.&lt;/p&gt;

&lt;p&gt;This time user has no idea about the actual server. He knocks on the middleware server for any service. This server works as a broker, it knows the internal handover logic when a new request arrives. It handovers the request to the actual designated server. There will be one or multiple servers to serve the request. It is totally unknown to the users(clients), they only know the middleware(reverse-proxy) server. This way servers hide their identity from users(malicious users!!!). Besides, there are lots of very useful usages of reverse-proxy like load balancing, caching…(discuss next time)&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;h3&gt;
  
  
  Final image
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Kc6IzKdl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8orekcl9kqbaalctlsyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Kc6IzKdl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8orekcl9kqbaalctlsyk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;First Published: &lt;a href="https://khabib97.medium.com/proxy-vs-reverse-proxy-less-theory-f72988f51267"&gt;https://khabib97.medium.com/proxy-vs-reverse-proxy-less-theory-f72988f51267&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
