-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal-search.xml
More file actions
98 lines (45 loc) · 130 KB
/
Copy pathlocal-search.xml
File metadata and controls
98 lines (45 loc) · 130 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>New Java8 Features</title>
<link href="/2022/03/05/java8/"/>
<url>/2022/03/05/java8/</url>
<content type="html"><![CDATA[<p><img src="/data/java-features/assets/java8.jpg" alt="Java8"></p><h3 id="New-Java8-Features"><a href="#New-Java8-Features" class="headerlink" title="New Java8 Features"></a>New Java8 Features</h3><p>It has been years since Java 8 released over its predator<br>Java 7, though I saw it is missed in many today’s codes,<br>Today we have new versions of Java 11, 13, 15, 16.<br>But I thought to have some word on it first. Actually it is so important<br>that I need to emphasize this here:</p><blockquote><p>One should first know Java8 features if wants to<br> truly take benefit of newer JDKs. e.g. Java11, 15, 16 and so on.</strong></p></blockquote><h3 id="Optional-So-what-is-about-Java-8"><a href="#Optional-So-what-is-about-Java-8" class="headerlink" title="(Optional) So what is about Java 8 ?"></a>(Optional) So what is about Java 8 ?</h3><p>You can skip this.<br>One can ask why I need to use Java 8? I worked a lot with previous<br>version and had no problem so far. Some also find it complex<br>to comprehend.<br>So, before we go deep into it let’s clear up something.</p><p>Aside from performance and memory improvements, Java 8 released to make things more flexible and structured.<br>You can make things simpler or in reverse more complex, so it does not mean the framework is complex itself.<br>Let me explain what is Java 8 itself. Actually I see Java 8 emerge not only a new version<br>of JDK but also a new programming paradigm!</p><h3 id="Introduction-of-Java-8"><a href="#Introduction-of-Java-8" class="headerlink" title="Introduction of Java 8"></a>Introduction of Java 8</h3><p>Java 8 introduced functional paradigm in Java world, with Lambda language.<br>Actually you still write Java but you can tell Java to take benefit of<br>functional programming languages inside Java codes too! </p><p>So I just wanted to have a bit of emphasise, the mindset changes here.<br>Developers with solid and classic Java developing mindset have less willing to learn this new style<br>and yet the reason we can find why so many projects in Java are still skipped<br>to upgrade. </p><p>So far whatever we’ve defined and shown in this website were around classic Java.<br>You should not think this as a total change to whole code, but to add a flavour to<br>part of you code. Actually to make them more clean, understandable, and even more structured.<br>Still you can write classic java beside you can include functional style<br>to your work, still getting the same result if you’d do it all classic way.</p><p>Specially when we deal with data and arrays and need to play with data to make<br>some meaningful info, Java 8 helps us to get rid of complexity of huge classic<br>code architecture and as a result lees error prune codes.</p>]]></content>
</entry>
<entry>
<title>Guide to S.O.L.I.D Principles</title>
<link href="/2022/03/05/solid/"/>
<url>/2022/03/05/solid/</url>
<content type="html"><![CDATA[<h4 id="The-following-5-concepts-make-up-our-SOLID-principles"><a href="#The-following-5-concepts-make-up-our-SOLID-principles" class="headerlink" title="The following 5 concepts make up our SOLID principles:"></a>The following 5 concepts make up our SOLID principles:</h4><figure class="highlight markdown"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><pre><code class="hljs markdown"><span class="hljs-bullet">1.</span> Single Responsibility<br><span class="hljs-bullet">2.</span> Open/Closed<br><span class="hljs-bullet">3.</span> Liskov Substitution<br><span class="hljs-bullet">4.</span> Interface Segregation<br><span class="hljs-bullet">5.</span> Dependency Inversion<br></code></pre></td></tr></table></figure><h4 id="1-Introduction"><a href="#1-Introduction" class="headerlink" title="1. Introduction"></a>1. Introduction</h4><p>In this tutorial, we’ll be discussing the SOLID principles of Object-Oriented Design.</p><p>First, we’ll start by exploring the reasons they came about and why we should consider them when designing software.<br>Then, we’ll outline each principle alongside some example code to emphasize the point.</p><p>I specially want to , since this is a principle for designing a good high quality software.<br>You may encounter with this principle before or even wrote codes solidly! But one may wonder what are those other principles to observe and have pretty good software design. Specially I want to discuss this in Java and how we do those pricipals.</p><h4 id="2-The-Reason-for-SOLID-Principles"><a href="#2-The-Reason-for-SOLID-Principles" class="headerlink" title="2. The Reason for SOLID Principles"></a>2. The Reason for SOLID Principles</h4><p>The SOLID principles were first conceptualized by Robert C. Martin in his 2000 paper, Design Principles and Design Patterns.<br>These concepts were later built upon by Michael Feathers, who introduced us to the SOLID acronym and in the last 20 years, these 5 principles have revolutionized the world of object-oriented programming, changing the way that we write software.</p><p>So, what is SOLID and how does it help us write better code? Simply put</p><figure class="highlight brainfuck"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs brainfuck"><span class="hljs-comment">design principles encourage us to</span> <span class="hljs-comment">create</span> <span class="hljs-comment">more</span> <span class="hljs-comment">maintainable</span><span class="hljs-string">,</span> <span class="hljs-comment">understandable</span><span class="hljs-string">,</span> <span class="hljs-comment">and</span> <span class="hljs-comment">flexible</span> <span class="hljs-comment">software</span><span class="hljs-string">.</span> <br><span class="hljs-comment">Consequently</span><span class="hljs-string">,</span> <span class="hljs-comment">as our applications grow in</span> <span class="hljs-comment">size</span><span class="hljs-string">,</span> <span class="hljs-comment">we can reduce their complexity and save ourselves a</span> <span class="hljs-comment">lot</span> <span class="hljs-comment">of headaches further down the</span> <span class="hljs-comment">road!</span><br></code></pre></td></tr></table></figure><p>The following 5 concepts make up our SOLID principles:</p><ol><li><em><strong>S</strong></em>ingle Responsibility</li><li><em><strong>O</strong></em>pen/Closed</li><li><em><strong>L</strong></em>iskov Substitution</li><li><em><strong>I</strong></em>nterface Segregation</li><li><em><strong>D</strong></em>ependency Inversion</li></ol><p>While some of these words may sound scary!, they can be easily understood with some simple code examples.<br>In the following sections, I’ll take a deep dive into what each of these principles means, along with a quick Java example to illustrate each one.</p><h4 id="3-Single-Responsibility"><a href="#3-Single-Responsibility" class="headerlink" title="3. Single Responsibility"></a>3. Single Responsibility</h4><p>Let’s kick things off with the single responsibility principle. As we might expect, this principle states that <em><strong>a class should only have one responsibility. Furthermore, it should only have one reason to change.</strong></em></p><p>How does this principle help us to build better software? Let’s see a few of its benefits:</p><ol><li><em><strong>Testing</strong></em> – A class with one responsibility will have far fewer test cases</li><li><em><strong>Lower coupling</strong></em> – Less functionality in a single class will have fewer dependencies</li><li><em><strong>Organization</strong></em> – Smaller, well-organized classes are easier to search than monolithic ones</li></ol><p>Take, for example, a class to represent a simple book:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Book</span> {<br> <br> <span class="hljs-keyword">private</span> String name;<br> <span class="hljs-keyword">private</span> String author;<br> <span class="hljs-keyword">private</span> String text;<br> <br> <span class="hljs-comment">//constructor, getters and setters</span><br>}<br></code></pre></td></tr></table></figure><p>In this code, we store the name, author, and text associated with an instance of a Book.</p><p>Let’s now add a couple of methods to query the text:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Book</span> {<br> <br> <span class="hljs-keyword">private</span> String name;<br> <span class="hljs-keyword">private</span> String author;<br> <span class="hljs-keyword">private</span> String text;<br> <br> <span class="hljs-comment">//constructor, getters and setters</span><br> <br> <span class="hljs-comment">// methods that directly relate to the book properties</span><br> <span class="hljs-keyword">public</span> String <span class="hljs-title function_">replaceWordInText</span><span class="hljs-params">(String word)</span>{<br> <span class="hljs-keyword">return</span> text.replaceAll(word, text);<br> }<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">isWordInText</span><span class="hljs-params">(String word)</span>{<br> <span class="hljs-keyword">return</span> text.contains(word);<br> }<br>}<br></code></pre></td></tr></table></figure><p>Now, our Book class works well, and we can store as many books as we like in our application. But, what good is storing the information if we can’t output the text to our console and read it?</p><p>Let’s throw caution to the wind and add a print method:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Book</span> {<br> <span class="hljs-comment">//...</span><br> <br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">printTextToConsole</span><span class="hljs-params">()</span>{<br> <span class="hljs-comment">// our code for formatting and printing the text</span><br> }<br>}<br></code></pre></td></tr></table></figure><p>This code does, however, violate the single responsibility principle we outlined earlier. To fix our mess, we should implement a separate class that is concerned only with printing our texts:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">BookPrinter</span> {<br> <br> <span class="hljs-comment">// methods for outputting text</span><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">printTextToConsole</span><span class="hljs-params">(String text)</span>{<br> <span class="hljs-comment">//our code for formatting and printing the text</span><br> }<br> <br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">printTextToAnotherMedium</span><span class="hljs-params">(String text)</span>{<br> <span class="hljs-comment">// code for writing to any other location..</span><br> }<br>}<br></code></pre></td></tr></table></figure><p>Awesome. Not only have we developed a class that relieves the Book of its printing duties, but we can also leverage our BookPrinter class to send our text to other media.</p><p>Whether it’s email, logging, or anything else, we have a separate class dedicated to this one concern!</p><h4 id="4-Open-for-Extension-Closed-for-Modification"><a href="#4-Open-for-Extension-Closed-for-Modification" class="headerlink" title="4. Open for Extension, Closed for Modification"></a>4. Open for Extension, Closed for Modification</h4><p>Now, time for the ‘O’ – more formally known as the open-closed principle. Simply put, classes should be open for extension,<br>but closed for modification.<br>In doing so, we stop ourselves from modifying existing code and causing potential new bugs in an otherwise happy application.</p><p>Of course, the one exception to the rule is when fixing bugs in existing code.</p><p>Let’s explore the concept further with a quick code example. As part of a new project, imagine we’ve implemented a Guitar class.</p><p>It’s fully fledged and even has a volume knob:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Guitar</span> {<br> <br> <span class="hljs-keyword">private</span> String make;<br> <span class="hljs-keyword">private</span> String model;<br> <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> volume;<br> <br> <span class="hljs-comment">//Constructors, getters & setters</span><br>}<br></code></pre></td></tr></table></figure><p>We launch the application, and everyone loves it. However, after a few months, we decide the Guitar is a little bit boring and could do with an awesome flame pattern to make it look a bit more ‘rock and roll’.</p><p>At this point, it might be tempting to just open up the Guitar class and add a flame pattern – but who knows what errors that might throw up in our application.</p><p>Instead, let’s stick to the open-closed principle and simply extend our Guitar class:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SuperCoolGuitarWithFlames</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Guitar</span> {<br> <br> <span class="hljs-keyword">private</span> String flameColor;<br> <br> <span class="hljs-comment">//constructor, getters + setters</span><br>}<br></code></pre></td></tr></table></figure><p>By extending the Guitar class we can be sure that our existing application won’t be affected.</p><h4 id="5-Liskov-Substitution-LSP"><a href="#5-Liskov-Substitution-LSP" class="headerlink" title="5. Liskov Substitution - LSP"></a>5. Liskov Substitution - LSP</h4><p>Next up on our list is Liskov substitution, which is arguably the most complex of the 5 principles. </p><p>Simply put, <em><strong>if class A is a subtype of class B, then we should be able to replace B with A without disrupting the behavior of our program.</strong></em></p><p>Let’s just jump straight to the code to help wrap our heads around this concept:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">Vehicle</span> {<br> <br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">turnOnEngine</span><span class="hljs-params">()</span>;<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">accelerate</span><span class="hljs-params">()</span>;<br>}<br></code></pre></td></tr></table></figure><p>Above, we define a simple Vehicle interface with a couple of methods that all cars should be able to fulfill – turning on the engine, and accelerating forward.</p><p>Let’s implement our interface and provide some code for the methods:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Car</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Vehicle</span> {<br> <br> <span class="hljs-keyword">private</span> Engine engine;<br> <br> <span class="hljs-comment">//Constructors, getters + setters</span><br> <br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">turnOnEngine</span><span class="hljs-params">()</span> {<br> <span class="hljs-comment">//turn on the engine!</span><br> engine.on();<br> }<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">accelerate</span><span class="hljs-params">()</span> {<br> <span class="hljs-comment">//move forward!</span><br> engine.powerOn(<span class="hljs-number">1000</span>);<br> }<br>}<br></code></pre></td></tr></table></figure><p>As our code describes, we have an engine that we can turn on, and we can increase the power. But wait, its 2020 and we are now using bikes as a vehicle too! </p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Bike</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Vehicle</span> {<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">turnOnEngine</span><span class="hljs-params">()</span> {<br> <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">AssertionError</span>(<span class="hljs-string">"I don't have an engine!"</span>);<br> }<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">accelerate</span><span class="hljs-params">()</span> {<br> <span class="hljs-comment">//this acceleration is crazy!</span><br> }<br>}<br></code></pre></td></tr></table></figure><p>By throwing a bike without an engine into the mix, we are inherently changing the behavior of our program.<br>This is a blatant <em><strong>violation of Liskov substitution and is a bit harder to fix than our previous 2 principles.</strong></em></p><p>One possible solution would be to rework our model into interfaces that take into account the engine-less state of our Vehicle.</p><p>I would rewire them with another example to satisfy Mr. Liskov in <a target="_blank" href="#/lsp">this article</a> separately, make sure to check it out.</p><h4 id="6-Interface-Segregation"><a href="#6-Interface-Segregation" class="headerlink" title="6. Interface Segregation"></a>6. Interface Segregation</h4><p>The ‘I ‘ in SOLID stands for interface segregation, and it simply means that larger interfaces should be split into smaller ones.<br>It states that no client should be forced to depend on methods it does not use.<br>By doing so, we can ensure that implementing classes only need to be concerned about the methods that are of interest to them.</p><p>For this example, we’re going to try our hands as zookeepers. And more specifically, we’ll be working in the bear enclosure.</p><p>Let’s start with an interface that outlines our roles as a bear keeper:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">BearKeeper</span> {<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">washTheBear</span><span class="hljs-params">()</span>;<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">feedTheBear</span><span class="hljs-params">()</span>;<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">petTheBear</span><span class="hljs-params">()</span>;<br>}<br></code></pre></td></tr></table></figure><p>As avid zookeepers, we’re more than happy to wash and feed our beloved bears. However, we’re all too aware of the dangers of petting them.<br>Unfortunately, our interface is rather large, and we have no choice than to implement the code to pet the bear.</p><p>Let’s fix this by splitting our large interface into 3 separate ones:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">BearCleaner</span> {<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">washTheBear</span><span class="hljs-params">()</span>;<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">BearFeeder</span> {<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">feedTheBear</span><span class="hljs-params">()</span>;<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">BearPetter</span> {<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">petTheBear</span><span class="hljs-params">()</span>;<br>}<br></code></pre></td></tr></table></figure><p>Now, thanks to interface segregation, we’re free to implement only the methods that matter to us:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">BearCarer</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">BearCleaner</span>, BearFeeder {<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">washTheBear</span><span class="hljs-params">()</span> {<br> <span class="hljs-comment">//I think we missed a spot...</span><br> }<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">feedTheBear</span><span class="hljs-params">()</span> {<br> <span class="hljs-comment">//Tuna Tuesdays...</span><br> }<br>}<br></code></pre></td></tr></table></figure><p>And finally, we can leave the dangerous stuff to the crazy people:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">CrazyPerson</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">BearPetter</span> {<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">petTheBear</span><span class="hljs-params">()</span> {<br> <span class="hljs-comment">//Good luck with that!</span><br> }<br>}<br></code></pre></td></tr></table></figure><h4 id="7-Dependency-Inversion"><a href="#7-Dependency-Inversion" class="headerlink" title="7. Dependency Inversion"></a>7. Dependency Inversion</h4><p><em><strong>The principle of Dependency Inversion refers to the decoupling of software modules.<br>This way, instead of high-level modules depending on low-level modules, both will depend on abstractions.</strong></em></p><p>To demonstrate this, let’s go old-school and bring to life a Windows 98 computer with code:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Windows98Machine</span> {}<br></code></pre></td></tr></table></figure><p>But what good is a computer without a monitor and keyboard? Let’s add one of each to our constructor so that every Windows98Computer we instantiate comes pre-packed with a Monitor and a StandardKeyboard:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Windows98Machine</span> {<br> <br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> StandardKeyboard keyboard;<br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Monitor monitor;<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-title function_">Windows98Machine</span><span class="hljs-params">()</span> {<br> monitor = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Monitor</span>();<br> keyboard = <span class="hljs-keyword">new</span> <span class="hljs-title class_">StandardKeyboard</span>();<br> }<br> <br>}<br></code></pre></td></tr></table></figure><p>This code will work, and we’ll be able to use the <em><strong>StandardKeyboard</strong></em> and <em><strong>Monitor</strong></em> freely within our <em><strong>Windows98Computer</strong></em> class.<br>Problem solved? <em><strong>Not quite</strong></em>. By declaring the <em><strong>StandardKeyboard</strong></em> and <em><strong>Monitor</strong></em> with the new keyword, we’ve tightly coupled these 3 classes together.</p><p>Not only does this make our Windows98Computer hard to test, but we’ve also lost the ability to switch out our <em><strong>StandardKeyboard</strong></em><br>class with a different one should the need arise. And we’re stuck with our <em><strong>Monitor</strong></em> class, too.</p><p>Let’s decouple our machine from the <em><strong>StandardKeyboard</strong></em> by adding a more general <em><strong>Keyboard</strong></em> interface and using this in our class:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">Keyboard</span> { }<br></code></pre></td></tr></table></figure><p>And now by using <strong>Keyboard</strong> we can would allow <strong>Windows98Machine</strong> substitute any type of <strong>Keyboard</strong> </p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Windows98Machine</span>{<br> <br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Keyboard keyboard;<br> <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Monitor monitor;<br> <br> <span class="hljs-keyword">public</span> <span class="hljs-title function_">Windows98Machine</span><span class="hljs-params">(Keyboard keyboard, Monitor monitor)</span> {<br> <span class="hljs-built_in">this</span>.keyboard = keyboard;<br> <span class="hljs-built_in">this</span>.monitor = monitor;<br> }<br>}<br></code></pre></td></tr></table></figure><p>Here, we’re using the dependency injection pattern here to facilitate adding the Keyboard dependency into the Windows98Machine class.</p><p>Let’s also modify our StandardKeyboard class to implement the Keyboard interface so that it’s suitable for injecting into the Windows98Machine class:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">StandardKeyboard</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Keyboard</span> { <br><br>}<br></code></pre></td></tr></table></figure><p>Now our classes are decoupled and communicate through the Keyboard abstraction. If we want, we can easily switch out the type of keyboard in our machine with a different implementation of the interface. We can follow the same principle for the Monitor class.</p><p>Excellent! We’ve decoupled the dependencies and are free to test our Windows98Machine with whichever testing framework we choose.</p>]]></content>
</entry>
<entry>
<title>Liskov Substitution Principle Explained</title>
<link href="/2022/03/05/lsp/"/>
<url>/2022/03/05/lsp/</url>
<content type="html"><![CDATA[<p>This article gives a quick intro to the Liskov Substitution Principle (LSP), why it’s important, and how to use it to validate object-oriented designs. We’ll also see some examples and learn how to correctly identify and fix violations of the LSP.</p><h3 id="What-is-the-LSP"><a href="#What-is-the-LSP" class="headerlink" title="What is the LSP?"></a>What is the LSP?</h3><p>At a high level, the LSP states that in an object-oriented program, if we substitute a superclass object reference with an object of any of its subclasses, the program should not break.</p><p>Say we had a method that used a superclass object reference to do something:</p><figure class="highlight java"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">SomeClass</span> {<br> <br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">aMethod</span><span class="hljs-params">(SuperClass superClassReference)</span> {<br> doSomething(superClassReference);<br> }<br> <br> <span class="hljs-comment">// definition of doSomething() omitted</span><br>}<br></code></pre></td></tr></table></figure><p>This should work as expected for every possible subclass object of SuperClass that is passed to it. If substituting a superclass object with a subclass object changes the program behavior in unexpected ways, the LSP is violated.</p><p>The LSP is applicable when there’s a supertype-subtype inheritance relationship by either extending a class or implementing an interface. </p><figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs livecodeserver">We can think <span class="hljs-keyword">of</span> <span class="hljs-keyword">the</span> methods defined <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> supertype <span class="hljs-keyword">as</span> defining <span class="hljs-keyword">a</span> contract. Every subtype is expected <span class="hljs-built_in">to</span> stick <span class="hljs-built_in">to</span> this contract. If <span class="hljs-keyword">a</span> subclass does <span class="hljs-keyword">not</span> adhere <span class="hljs-built_in">to</span> <span class="hljs-keyword">the</span> superclass’s contract, <span class="hljs-keyword">it</span>’s violating <span class="hljs-keyword">the</span> LSP.<br></code></pre></td></tr></table></figure><p>This makes sense intuitively - a class’s contract tells its clients what to expect. If a subclass extends or overrides the behavior of the superclass in unintended ways, it would break the clients.</p><p>How can a method in a subclass break a superclass method’s contract? There are several possible ways:</p><ol><li>Returning an object that’s incompatible with the object returned by the superclass method.</li><li>Throwing a new exception that’s not thrown by the superclass method.</li><li>Changing the semantics or introducing side effects that are not part of the superclass’s contract.</li></ol><p>Java and other statically-typed languages prevent 1 (unless we use very generic classes like Object) and 2 (for checked exceptions) by flagging them at compile-time. It’s still possible to violate the LSP in these languages via the third way.</p><h4 id="Why-is-the-LSP-Important"><a href="#Why-is-the-LSP-Important" class="headerlink" title="Why is the LSP Important?"></a>Why is the LSP Important?</h4><p>LSP violations are a design smell. We may have generalized a concept prematurely and created a superclass where none is needed.<br>Future requirements for the concept might not fit the class hierarchy we have created.</p><p>If client code cannot substitute a superclass reference with a subclass object freely, it would be forced to do instanceof checks and specially handle some subclasses.<br>If this kind of conditional code is spread across the codebase, it will be difficult to maintain.</p><p>Every time we add or modify a subclass, we would have to comb through the codebase and change multiple places. This is difficult and error-prone.</p><p>It also defeats the purpose of introducing the supertype abstraction in the first place which is to make it easy to enhance the program.</p><p>It may not even be possible to identify all the places and change them - we may not own or control the client code. We could be developing our functionality as a library and providing them to external users, for example.</p><h4 id="Violating-the-LSP-An-Example"><a href="#Violating-the-LSP-An-Example" class="headerlink" title="Violating the LSP - An Example"></a>Violating the LSP - An Example</h4><p>Suppose we were building the payment module for our eCommerce website. Customers order products on the site and pay using payment instruments like a credit card or a debit card.</p><blockquote><p>When a customer provides their card details, we want to</p><ul><li>validate it,</li><li>run it through a third-party fraud detection system,</li><li>and then send the details to a payment gateway for processing.</li></ul></blockquote><p>While some basic validations are required on all cards, there are additional validations needed on credit cards. Once the payment is done, we record it in our database. Because of various security and regulatory reasons, we don’t store the actual card details in our database, but a fingerprint identifier for it that’s returned by the payment gateway.</p><p>Given these requirements, we might model our classes as below:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">PaymentInstrument</span> {<br> String name;<br> String cardNumber;<br> String verificationCode;<br> Date expiryDate;<br> String fingerprint;<br><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">validate</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentInstrumentInvalidException {<br> <span class="hljs-comment">// basic validation on name, expiryDate etc.</span><br> <span class="hljs-keyword">if</span> (name == <span class="hljs-literal">null</span> || name.isEmpty()) {<br> <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">PaymentInstrumentInvalidException</span>(<span class="hljs-string">"Name is invalid"</span>);<br> }<br> <span class="hljs-comment">// other validations</span><br> }<br><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">runFraudChecks</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> FraudDetectedException {<br> <span class="hljs-comment">// run checks against a third-party system</span><br> }<br><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendToPaymentGateway</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentFailedException {<br> <span class="hljs-comment">// send details to payment gateway (PG) and set fingerprint from </span><br> <span class="hljs-comment">// the payment gateway response</span><br> } <br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">CreditCard</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">PaymentInstrument</span> {<br><br> <span class="hljs-meta">@Override</span><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">validate</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentInstrumentInvalidException {<br> <span class="hljs-built_in">super</span>.validate();<br> <span class="hljs-comment">// additional validations for credit cards</span><br> } <br> <span class="hljs-comment">// other credit card-specific code</span><br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">DebitCard</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">PaymentInstrument</span> { <br> <span class="hljs-comment">// debit card-specific code</span><br>}<br></code></pre></td></tr></table></figure><p>A different area in our codebase where we process a payment might look something like this:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">PaymentProcessor</span> {<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">process</span><span class="hljs-params">(OrderDetails orderDetails, PaymentInstrument paymentInstrument)</span> {<br> <span class="hljs-keyword">try</span> {<br> paymentInstrument.validate();<br> paymentInstrument.runFraudChecks();<br> paymentInstrument.sendToPaymentGateway();<br> saveToDatabase(orderDetails, paymentInstrument);<br> } <span class="hljs-keyword">catch</span> (...){<br> <span class="hljs-comment">// exception handling</span><br> }<br> }<br><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">saveToDatabase</span><span class="hljs-params">(</span><br><span class="hljs-params"> OrderDetails orderDetails, </span><br><span class="hljs-params"> PaymentInstrument paymentInstrument)</span> {<br> <span class="hljs-type">String</span> <span class="hljs-variable">fingerprint</span> <span class="hljs-operator">=</span> paymentInstrument.getFingerprint();<br> <span class="hljs-comment">// save fingerprint and order details in DB</span><br> }<br>}<br></code></pre></td></tr></table></figure><p>Of course, in an actual production system, there would be many complex aspects to handle. The single processor class above might well be a bunch of classes in multiple packages across service and repository layers.</p><p>All is well and our system is processing payments as expected. At some point, the marketing team decides to introduce reward points to increase customer loyalty. Customers get a small number of reward points for each purchase. They can use the points to buy products on the site.</p><p>Ideally, we should be able to just add a <em><strong>RewardsCard</strong></em> class that extends <em><strong>PaymentInstrument</strong></em> and be done with it. But we find that adding it violates the LSP!</p><p>There are no fraud checks for Rewards Cards. Details are not sent to payment gateways and there is no concept of a fingerprint identifier.<br><em><strong>PaymentProcessor</strong></em> breaks as soon as we add <em><strong>RewardsCard</strong></em>.</p><p>We might try force-fitting <em><strong>RewardsCard</strong></em> into the current class hierarchy by overriding <em><strong>runFraudChecks()</strong></em> and <em><strong>sendToPaymentGateway()</strong></em> with empty, do-nothing implementations.</p><p>This would still break the application - we might get a <em><strong>NullPointerException</strong></em> from the <em><strong>saveToDatabase()</strong></em> method since the fingerprint would be null. Can we handle it just this once as a special case in <em><strong>saveToDatabase()</strong></em> by doing an ***instanceofv check on the <em><strong>PaymentInstrument</strong></em> argument?</p><p>But we know that if we do it once, we’ll do it again. Soon our codebase will be strewn with multiple checks and special cases to handle the problems created by the incorrect class model. We can imagine the pain this will cause each time we enhance the payment module.</p><p>For example, what if the business decides to accept Bitcoins? Or marketing introduces a new payment mode like Cash on Delivery?</p><h4 id="Fixing-the-Design"><a href="#Fixing-the-Design" class="headerlink" title="Fixing the Design"></a>Fixing the Design</h4><p>Let’s revisit the design and create supertype abstractions only if they are general enough to create code that is flexible to requirement changes. We will also use the following object-oriented design principles:</p><blockquote><ul><li>Program to interface, not implementation</li><li>Encapsulate what varies</li><li>Prefer composition over inheritance</li></ul></blockquote><p>To start with, what we can be sure of is that our application needs to collect payment - both at present and in the future. It’s also reasonable to think that we would want to validate whatever payment details we collect. Almost everything else could change. So let’s define the below interfaces:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">interface</span> <span class="hljs-title class_">PaymentInstrument</span>{<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">validate</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentInstrumentInvalidException;<br> PaymentResponse <span class="hljs-title function_">collectPayment</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentFailedException;<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">PaymentResponse</span> {<br> String identifier;<br>}<br></code></pre></td></tr></table></figure><p><strong>PaymentResponse</strong> encapsulates an <strong>identifier</strong> - this could be the fingerprint for credit and debit cards or the card number for rewards cards.<br>It could be something else for a different payment instrument in the future. The encapsulation ensures <strong>PaymentInstrument</strong> can remain unchanged if future payment instruments have more data.</p><p><strong>PaymentProcessor</strong> class now looks like this:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">PaymentProcessor</span> {<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">process</span><span class="hljs-params">(</span><br><span class="hljs-params"> OrderDetails orderDetails, </span><br><span class="hljs-params"> PaymentInstrument paymentInstrument)</span> {<br> <span class="hljs-keyword">try</span> {<br> paymentInstrument.validate();<br> <span class="hljs-type">PaymentResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> paymentInstrument.collectPayment();<br> saveToDatabase(orderDetails, response.getIdentifier());<br> } <span class="hljs-keyword">catch</span> (...) {<br> <span class="hljs-comment">// exception handling</span><br> }<br> }<br><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">saveToDatabase</span><span class="hljs-params">(OrderDetails orderDetails, String identifier)</span> {<br> <span class="hljs-comment">// save the identifier and order details in DB</span><br> }<br>}<br></code></pre></td></tr></table></figure><p>There are no <strong>runFraudChecks()</strong> and <strong>sendToPaymentGateway()</strong> calls in <strong>PaymentProcessor</strong> anymore - these are not general enough to apply to all payment instruments.</p><p>Let’s add a few more interfaces for other concepts which seem general enough in our problem domain:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">interface</span> <span class="hljs-title class_">FraudChecker</span> {<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">runChecks</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> FraudDetectedException;<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">interface</span> <span class="hljs-title class_">PaymentGatewayHandler</span> {<br> PaymentGatewayResponse <span class="hljs-title function_">handlePayment</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentFailedException;<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">interface</span> <span class="hljs-title class_">PaymentInstrumentValidator</span> {<br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">validate</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentInstrumentInvalidException;<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">PaymentGatewayResponse</span> {<br> String fingerprint;<br>}<br></code></pre></td></tr></table></figure><p>And here are the implementations:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">ThirdPartyFraudChecker</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">FraudChecker</span> {<br> <span class="hljs-comment">// members omitted</span><br> <br> <span class="hljs-meta">@Override</span><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">runChecks</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> FraudDetectedException {<br> <span class="hljs-comment">// external system call omitted</span><br> }<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">PaymentGatewayHandler</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">PaymentGatewayHandler</span> {<br> <span class="hljs-comment">// members omitted</span><br> <br> <span class="hljs-meta">@Override</span><br> PaymentGatewayResponse <span class="hljs-title function_">handlePayment</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentFailedException {<br> <span class="hljs-comment">// send details to payment gateway (PG), set the fingerprint</span><br> <span class="hljs-comment">// received from PG on a PaymentGatewayResponse and return</span><br> }<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">BankCardBasicValidator</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">PaymentInstrumentValidator</span> {<br> <span class="hljs-comment">// members like name, cardNumber etc. omitted</span><br><br> <span class="hljs-meta">@Override</span><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">validate</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentInstrumentInvalidException {<br> <span class="hljs-comment">// basic validation on name, expiryDate etc.</span><br> <span class="hljs-keyword">if</span> (name == <span class="hljs-literal">null</span> || name.isEmpty()) {<br> <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">PaymentInstrumentInvalidException</span>(<span class="hljs-string">"Name is invalid"</span>);<br> }<br> <span class="hljs-comment">// other basic validations</span><br> }<br>}<br></code></pre></td></tr></table></figure><p>Let’s build <strong>CreditCard</strong> and <strong>DebitCard</strong> abstractions by composing the above building blocks in different ways.<br>We first define a class that implements <strong>PaymentInstrument</strong>:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">BaseBankCard</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">PaymentInstrument</span> {<br> <span class="hljs-comment">// members like name, cardNumber etc. omitted</span><br> <span class="hljs-comment">// below dependencies will be injected at runtime</span><br> PaymentInstrumentValidator basicValidator;<br> FraudChecker fraudChecker;<br> PaymentGatewayHandler gatewayHandler;<br><br> <span class="hljs-meta">@Override</span><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">validate</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentInstrumentInvalidException {<br> basicValidator.validate();<br> }<br><br> <span class="hljs-meta">@Override</span><br> PaymentResponse <span class="hljs-title function_">collectPayment</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentFailedException {<br> <span class="hljs-type">PaymentResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">PaymentResponse</span>();<br> <span class="hljs-keyword">try</span> {<br> fraudChecker.runChecks();<br> <span class="hljs-type">PaymentGatewayResponse</span> <span class="hljs-variable">pgResponse</span> <span class="hljs-operator">=</span> gatewayHandler.handlePayment();<br> response.setIdentifier(pgResponse.getFingerprint());<br> } <span class="hljs-keyword">catch</span> (FraudDetectedException e) {<br> <span class="hljs-comment">// exception handling</span><br> }<br> <span class="hljs-keyword">return</span> response;<br> }<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">CreditCard</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">BaseBankCard</span> {<br> <span class="hljs-comment">// constructor omitted</span><br> <br> <span class="hljs-meta">@Override</span><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">validate</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentInstrumentInvalidException {<br> basicValidator.validate();<br> <span class="hljs-comment">// additional validations for credit cards</span><br> }<br>}<br></code></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">DebitCard</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">BaseBankCard</span> {<br> <span class="hljs-comment">// constructor omitted</span><br>}<br></code></pre></td></tr></table></figure><p>Though <strong>CreditCard</strong> and <strong>DebitCard</strong> extend a class, it’s not the same as before.<br>Other areas of our codebase now depend only on the <strong>PaymentInstrument</strong> interface, not on <strong>BaseBankCard</strong>.<br>Below snippet shows <strong>CreditCard</strong> object creation and processing:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">PaymentGatewayHandler</span> <span class="hljs-variable">gatewayHandler</span> <span class="hljs-operator">=</span> <br> <span class="hljs-keyword">new</span> <span class="hljs-title class_">PaymentGatewayHandler</span>(name, cardNum, code, expiryDate);<br><br><span class="hljs-type">PaymentInstrumentValidator</span> <span class="hljs-variable">validator</span> <span class="hljs-operator">=</span> <br> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BankCardBasicValidator</span>(name, cardNum, code, expiryDate);<br><br><span class="hljs-type">FraudChecker</span> <span class="hljs-variable">fraudChecker</span> <span class="hljs-operator">=</span> <br> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ThirdPartyFraudChecker</span>(name, cardNum, code, expiryDate);<br><br><span class="hljs-type">CreditCard</span> <span class="hljs-variable">card</span> <span class="hljs-operator">=</span> <br> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CreditCard</span>(<br> name,<br> cardNum,<br> code,<br> expiryDate,<br> validator,<br> fraudChecker,<br> gatewayHandler);<br><br>paymentProcessor.process(order, card);<br></code></pre></td></tr></table></figure><p>Our design is now <strong>flexible enough</strong> to let us add a <strong>RewardsCard</strong> - no force-fitting and no conditional checks.<br>We just add the new class and it works as expected.</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">RewardsCard</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">PaymentInstrument</span> {<br> String name;<br> String cardNumber;<br><br> <span class="hljs-meta">@Override</span><br> <span class="hljs-keyword">void</span> <span class="hljs-title function_">validate</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentInstrumentInvalidException {<br> <span class="hljs-comment">// Rewards card related validations</span><br> }<br><br> <span class="hljs-meta">@Override</span><br> PaymentResponse <span class="hljs-title function_">collectPayment</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> PaymentFailedException {<br> <span class="hljs-type">PaymentResponse</span> <span class="hljs-variable">response</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">PaymentResponse</span>();<br> <span class="hljs-comment">// Steps related to rewards card payment like getting current </span><br> <span class="hljs-comment">// rewards balance, updating balance etc.</span><br> response.setIdentifier(cardNumber);<br> <span class="hljs-keyword">return</span> response;<br> }<br>}<br></code></pre></td></tr></table></figure><p>And here’s client code using the new card:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">RewardsCard</span> <span class="hljs-variable">card</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RewardsCard</span>(name, cardNum);<br>paymentProcessor.process(order, card);<br></code></pre></td></tr></table></figure><h4 id="Here-is-the-final-class-diagram"><a href="#Here-is-the-final-class-diagram" class="headerlink" title="Here is the final class diagram"></a>Here is the final class diagram</h4><p><img src="https://raw.githubusercontent.com/java-art/java-art.github.io/master/data/solid-principle/ldp-bank-class-diagram.png" alt="final-class-diagram"></p><h4 id="Advantages-of-the-New-Design"><a href="#Advantages-of-the-New-Design" class="headerlink" title="Advantages of the New Design"></a>Advantages of the New Design</h4><p>The new design not only fixes the LSP violation but also gives us a loosely-coupled, flexible set of classes to handle changing requirements.<br>For example, adding new payment instruments like Bitcoin and Cash on Delivery is easy - we just add new classes that implement <strong>PaymentInstrument</strong>.</p><p>Business needs debit cards to be processed by a different payment gateway? No problem - we add a new class that implements <strong>PaymentGatewayHandler</strong> and inject it into <strong>DebitCard</strong>.<br>If DebitCard’s requirements begin to diverge a lot from CreditCard’s, we can have it implement directly instead of extending <strong>BaseBankCard</strong> - <em>no other class is impacted</em>.</p><p>If we need an in-house fraud check for <strong>RewardsCard</strong>, we add an <strong>InhouseFraudChecker</strong> that implements <strong>FraudChecker</strong>, inject it into <strong>RewardsCard</strong> and only change <strong>RewardsCard.collectPayment()</strong>.</p><h4 id="How-to-Identify-LSP-Violations"><a href="#How-to-Identify-LSP-Violations" class="headerlink" title="How to Identify LSP Violations?"></a>How to Identify LSP Violations?</h4><p>Some good indicators to identify LSP violations are:</p><p>Conditional logic (using the <strong>instanceof</strong> operator or <strong>object.getClass().getName()</strong> to identify the actual <strong>subclass</strong>)<br>in client code empty, do-nothing implementations of one or more methods in subclasses throwing an <strong>UnsupportedOperationException</strong> or some other unexpected exception from a subclass method.</p><p>Consider <strong>java.util.List<E></strong> interface’s <strong>add(E e)</strong> method. Since <strong>java.util.Arrays.asList(T …)</strong> returns an unmodifiable list, client code which adds an element to a List would break if it were passed a List returned by <strong>Arrays.asList</strong>.</p><p>Is this an LSP violation? No - the <strong>List.add(E e)</strong> method’s contract says implementations may throw an <strong>UnsupportedOperationException</strong>. Clients are expected to handle this when using the method.</p><h4 id="Example-output"><a href="#Example-output" class="headerlink" title="Example output"></a>Example output</h4><div class="code-wrapper"><pre><code class="hljs">Starting payment processing for customer Mehrdad with creadit card number 1234-1234-1234-1234-1234Running fraud checks against third-party systemFraud checks passedSending details to payment gatewayCredit card payment successful!Starting payment processing for customer Mehrdad with rewards card number 1234-1234-1234-1234-1234Updating rewards balanceRewards card payment successful!</code></pre></div>]]></content>
</entry>
<entry>
<title>Depth First Search - DFS</title>
<link href="/2022/03/05/dfs/"/>
<url>/2022/03/05/dfs/</url>
<content type="html"><![CDATA[<p>A DFS is an algorithm to travers graph-based structures (also for trees included in graphs)<br>in a depth order fashion. </p><figure class="highlight applescript"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br></pre></div></td><td class="code"><pre><code class="hljs applescript">DFS <span class="hljs-keyword">is</span> helpful <span class="hljs-keyword">to</span> traverse nodes <span class="hljs-keyword">of</span> a graph only once <span class="hljs-keyword">and</span> you want <span class="hljs-keyword">to</span> go deep <span class="hljs-keyword">in</span> a child <span class="hljs-keyword">from</span> parent node <span class="hljs-keyword">as</span> much <span class="hljs-keyword">as</span> <span class="hljs-keyword">it</span> <span class="hljs-keyword">is</span> possible.<br></code></pre></td></tr></table></figure><h3 id="Usecases"><a href="#Usecases" class="headerlink" title="Usecases:"></a>Usecases:</h3><p>Before jumping into discussion what DFS is, let’s understand the usecase of DFS.</p><p>Imagine that you are asked to write a basic algorithm for a Chess game. The opponent which can<br>be an AI computer wants to defeat the player. It should decide all available movements for a piece.</p><p>To continue, let’s imagine computer starts with a Pawn. It is obvious the first move<br>only go one or two upfront (just as the beginning of time). If you want to devise a<br>very professional algorithm that stimulate the player, it should predict the other movements after.</p><p>How about the next moves? Computer can decide randomly but have a good play matters to win and challenge the game!</p><p>So for this algorithm we can use start the move in the memory and then see other available movements<br>until no movement is possible and pick the one to prevent defeat fast!</p><p>A pseudo code for our simple algorithm is as follows:</p><p>~<br>pick a piece (pawn here)</p><ol><li>for any available movements in neighbor cells<br> check if movement is possible<br> insert it into a Stack if it was possible</li><li>for any movement in Stack<br> peek the move<br> check if it will be defeated?<br> repeat 1.<br>~</li></ol><p>Now you can see our algorithm is getting toward intelligent movements and can simulate a real opponent.</p><h4 id="Definition"><a href="#Definition" class="headerlink" title="Definition"></a>Definition</h4><p>Before going to implement this chess algo let’s DFS.<br>For a DFS we have a Graph (G for instance),and a starting node that we want to start traversing the graph<br>The pseudo code for DFS is like below:</p><h5 id="G-Graph-v-starting-node-can-be-root-node-vertex-node-vertices-nodes"><a href="#G-Graph-v-starting-node-can-be-root-node-vertex-node-vertices-nodes" class="headerlink" title="G: Graph, v: starting node (can be root node) , vertex: node, vertices: nodes"></a>G: Graph, v: starting node (can be root node) , vertex: node, vertices: nodes</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">dfs</span>(G, v):<br> label v <span class="hljs-keyword">as</span> discovered<br> <span class="hljs-keyword">for</span> all vertices connected to v<br> check vertex is not visited before?<br> call <span class="hljs-title function_">dfs</span>(G,vertex)<br></code></pre></td></tr></table></figure><p>Note that recursive calling acts like a Stack for us.If we want to have iterative approach we will need a stack.<br>Below is the non-recursive implementation or iterative approach for the above pseudo code.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">dfs</span>(G, v):<br> define a stack <br> put v into stack<br> <span class="hljs-keyword">while</span> stack is not empty<br> poll top vertex <span class="hljs-keyword">from</span> stack<br> label v <span class="hljs-keyword">as</span> discovered<br> <span class="hljs-keyword">for</span> all vertices connected to v<br> push vertex into stack<br></code></pre></td></tr></table></figure><p>Imagine we have tree (sort of a simple graph), then you can see the below action when you run the algo against this tree: </p><p><img src="/data/structures/assets/dfs.gif" alt="dfs"></p><h2 id="Implementation-of-Chess-algo"><a href="#Implementation-of-Chess-algo" class="headerlink" title="Implementation of Chess algo"></a>Implementation of Chess algo</h2><p>Now Let’s see how to we can implement the chess algo in Java using DFS.</p><p>There are two ways to implement the DFS:</p><ul><li>Recursive approach</li><li>Iterative approach</li></ul><p>I will implement the recursive approach.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><br></code></pre></td></tr></table></figure><p>I will implement the iterative approach.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> java.<span class="hljs-property">util</span>.<span class="hljs-property">Stack</span>;<br><br>public <span class="hljs-keyword">class</span> <span class="hljs-title class_">DFS</span> {<br><br> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Vertex</span> {<br> private final <span class="hljs-title class_">String</span> cell;<br><br> <span class="hljs-title class_">Vertex</span>(<span class="hljs-title class_">String</span> cell) {<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">cell</span> = cell;<br> }<br> }<br><br> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Tree</span> {<br> public final <span class="hljs-title class_">Vertex</span> vertex;<br> public final <span class="hljs-title class_">Tree</span> left;<br> public final <span class="hljs-title class_">Tree</span> right;<br> public boolean visited;<br><br> private <span class="hljs-title class_">Tree</span>(<span class="hljs-title class_">Builder</span> builder) {<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">left</span> = builder.<span class="hljs-title function_">getLeft</span>();<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">right</span> = builder.<span class="hljs-title function_">getRight</span>();<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">vertex</span> = builder.<span class="hljs-title function_">getValue</span>();<br> }<br><br> public <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Builder</span> {<br> private <span class="hljs-title class_">Tree</span> left, right;<br> private final <span class="hljs-title class_">Vertex</span> value;<br><br> public <span class="hljs-title class_">Builder</span>(<span class="hljs-title class_">Vertex</span> vertex) {<br> value = vertex;<br> }<br><br> public <span class="hljs-keyword">static</span> <span class="hljs-title class_">Builder</span> <span class="hljs-title function_">builder</span>(<span class="hljs-params">Vertex vertex</span>) {<br> <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Builder</span>(vertex);<br> }<br><br> public <span class="hljs-title class_">Builder</span> <span class="hljs-title function_">setLeft</span>(<span class="hljs-params">Tree left</span>) {<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">left</span> = left;<br> <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>;<br> }<br><br> public <span class="hljs-title class_">Builder</span> <span class="hljs-title function_">setRight</span>(<span class="hljs-params">Tree right</span>) {<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">right</span> = right;<br> <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>;<br> }<br><br> public <span class="hljs-title class_">Tree</span> <span class="hljs-title function_">getLeft</span>(<span class="hljs-params"></span>) {<br> <span class="hljs-keyword">return</span> left;<br> }<br><br> public <span class="hljs-title class_">Tree</span> <span class="hljs-title function_">getRight</span>(<span class="hljs-params"></span>) {<br> <span class="hljs-keyword">return</span> right;<br> }<br><br> public <span class="hljs-title class_">Vertex</span> <span class="hljs-title function_">getValue</span>(<span class="hljs-params"></span>) {<br> <span class="hljs-keyword">return</span> value;<br> }<br><br> <span class="hljs-title class_">Tree</span> <span class="hljs-title function_">build</span>(<span class="hljs-params"></span>) {<br> <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Tree</span>(<span class="hljs-variable language_">this</span>);<br> }<br> }<br><br> public <span class="hljs-title class_">Vertex</span> <span class="hljs-title function_">getVertex</span>(<span class="hljs-params"></span>) {<br> <span class="hljs-keyword">return</span> vertex;<br> }<br><br> public <span class="hljs-title class_">Tree</span> <span class="hljs-title function_">getRight</span>(<span class="hljs-params"></span>) {<br> <span class="hljs-keyword">return</span> right;<br> }<br><br> public <span class="hljs-title class_">Tree</span> <span class="hljs-title function_">getLeft</span>(<span class="hljs-params"></span>) {<br> <span class="hljs-keyword">return</span> left;<br> }<br> }<br><br> public <span class="hljs-title function_">DFS</span>(<span class="hljs-params"></span>) {<br> <span class="hljs-title class_">Tree</span> tree = <span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"2"</span>))<br> .<span class="hljs-title function_">setLeft</span>(<span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"3"</span>))<br> .<span class="hljs-title function_">setLeft</span>(<span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"4"</span>)).<span class="hljs-title function_">build</span>())<br> .<span class="hljs-title function_">setRight</span>(<span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"1"</span>)).<span class="hljs-title function_">build</span>())<br> .<span class="hljs-title function_">build</span>()<br> )<br> .<span class="hljs-title function_">setRight</span>(<span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"10"</span>)).<span class="hljs-title function_">build</span>())<br> .<span class="hljs-title function_">build</span>();<br><br> <span class="hljs-title class_">Tree</span> tree2 = <span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"1"</span>))<br> .<span class="hljs-title function_">setLeft</span>(<span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"2"</span>))<br> .<span class="hljs-title function_">setLeft</span>(<span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"3"</span>))<br> .<span class="hljs-title function_">setLeft</span>(<span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"4"</span>)).<span class="hljs-title function_">build</span>())<br> .<span class="hljs-title function_">setRight</span>(<span class="hljs-title class_">Tree</span>.<span class="hljs-property">Builder</span>.<span class="hljs-title function_">builder</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(<span class="hljs-string">"5"</span>)).<span class="hljs-title function_">build</span>())<br> .<span class="hljs-title function_">build</span>())<br> .<span class="hljs-title function_">build</span>()).<span class="hljs-title function_">build</span>();<br> <br> <span class="hljs-title class_">Stack</span><<span class="hljs-title class_">Tree</span>> vertices = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span><>();<br> vertices.<span class="hljs-title function_">push</span>(tree);<br> vertices.<span class="hljs-title function_">push</span>(tree2);<br> <span class="hljs-keyword">while</span> (!vertices.<span class="hljs-title function_">isEmpty</span>()) {<br> <span class="hljs-title class_">Tree</span> topNode = vertices.<span class="hljs-title function_">pop</span>();<br> <span class="hljs-keyword">if</span> (!topNode.<span class="hljs-property">visited</span>) {<br> <span class="hljs-title class_">System</span>.<span class="hljs-property">out</span>.<span class="hljs-title function_">println</span>(topNode.<span class="hljs-property">vertex</span>.<span class="hljs-property">cell</span>);<br> }<br> topNode.<span class="hljs-property">visited</span> = <span class="hljs-literal">true</span>;<br> <span class="hljs-title class_">Tree</span> leftChild = topNode.<span class="hljs-property">left</span>;<br> <span class="hljs-title class_">Tree</span> rightChild = topNode.<span class="hljs-property">right</span>;<br> <span class="hljs-keyword">if</span> (rightChild != <span class="hljs-literal">null</span> && !rightChild.<span class="hljs-property">visited</span>) {<br> vertices.<span class="hljs-title function_">push</span>(rightChild);<br> }<br> <span class="hljs-keyword">if</span> (leftChild != <span class="hljs-literal">null</span> && !leftChild.<span class="hljs-property">visited</span>) {<br> vertices.<span class="hljs-title function_">push</span>(leftChild);<br> }<br> }<br> }<br><br> public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span>(<span class="hljs-params"><span class="hljs-built_in">String</span>[] args</span>) {<br> <span class="hljs-keyword">new</span> <span class="hljs-title function_">DFS</span>();<br> }<br>}<br><br></code></pre></td></tr></table></figure><p>Another example using Graph</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><br>public <span class="hljs-keyword">class</span> <span class="hljs-title class_">DepthFirstSearch</span> {<br><br> public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span>(<span class="hljs-params"><span class="hljs-built_in">String</span>[] args</span>) {<br> <span class="hljs-title class_">Vertex</span> v1 = <span class="hljs-title class_">Vertex</span>.<span class="hljs-title function_">build</span>(<span class="hljs-string">"1"</span>);<br> <span class="hljs-title class_">Vertex</span> v2 = <span class="hljs-title class_">Vertex</span>.<span class="hljs-title function_">build</span>(<span class="hljs-string">"2"</span>);<br> <span class="hljs-title class_">Vertex</span> v3 = <span class="hljs-title class_">Vertex</span>.<span class="hljs-title function_">build</span>(<span class="hljs-string">"3"</span>);<br> <span class="hljs-title class_">Vertex</span> v4 = <span class="hljs-title class_">Vertex</span>.<span class="hljs-title function_">build</span>(<span class="hljs-string">"4"</span>);<br> <span class="hljs-title class_">Vertex</span> v5 = <span class="hljs-title class_">Vertex</span>.<span class="hljs-title function_">build</span>(<span class="hljs-string">"5"</span>);<br><br> v1.<span class="hljs-title function_">setConnection</span>(v2.<span class="hljs-title function_">setConnection</span>(v3, v4), v5);<br> <span class="hljs-title function_">dfs</span>(v1);<br> }<br><br> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">dfs</span>(<span class="hljs-params">Vertex root</span>) {<br> <span class="hljs-title class_">List</span><<span class="hljs-title class_">Vertex</span>> visited = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span><>();<br> <span class="hljs-title class_">Stack</span><<span class="hljs-title class_">Vertex</span>> stack = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Stack</span><>();<br> stack.<span class="hljs-title function_">push</span>(root);<br> <span class="hljs-keyword">while</span> (!stack.<span class="hljs-title function_">empty</span>()) {<br> <span class="hljs-title class_">Vertex</span> top = stack.<span class="hljs-title function_">pop</span>();<br> <span class="hljs-keyword">if</span> (!visited.<span class="hljs-title function_">contains</span>(top)) {<br> visited.<span class="hljs-title function_">add</span>(top);<br> }<br> <span class="hljs-keyword">if</span> (top.<span class="hljs-property">vertices</span> != <span class="hljs-literal">null</span>) {<br> <span class="hljs-keyword">for</span> (<span class="hljs-title class_">Vertex</span> vertex : top.<span class="hljs-property">vertices</span>) {<br> stack.<span class="hljs-title function_">push</span>(vertex);<br> }<br> }<br> }<br> visited.<span class="hljs-title function_">forEach</span>(vertex -> <span class="hljs-title class_">System</span>.<span class="hljs-property">out</span>.<span class="hljs-title function_">println</span>(vertex.<span class="hljs-property">cell</span>));<br> }<br><br> private <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Graph</span> {<br> private final <span class="hljs-title class_">Vertex</span> root;<br><br> <span class="hljs-title class_">Graph</span>(<span class="hljs-title class_">Vertex</span> root) {<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span> = root;<br> }<br> }<br><br> private <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Vertex</span> {<br> private final <span class="hljs-title class_">String</span> cell;<br> private <span class="hljs-title class_">Vertex</span>[] vertices;<br><br> private <span class="hljs-title class_">Vertex</span>(<span class="hljs-title class_">String</span> cell) {<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">cell</span> = cell;<br> }<br><br> <span class="hljs-keyword">static</span> <span class="hljs-title class_">Vertex</span> <span class="hljs-title function_">build</span>(<span class="hljs-params"><span class="hljs-built_in">String</span> cell</span>) {<br> <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vertex</span>(cell);<br> }<br><br> public <span class="hljs-title class_">Vertex</span> <span class="hljs-title function_">setConnection</span>(<span class="hljs-params">Vertex... vertices</span>) {<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">vertices</span> = vertices;<br> <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>;<br> }<br> }<br>}<br></code></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>about me</title>
<link href="/2022/03/05/footer/about/"/>
<url>/2022/03/05/footer/about/</url>
<content type="html"><![CDATA[<p>I’m a software developer and data engineer focused on writing better codes.</p>]]></content>
</entry>
<entry>
<title>privacy policy</title>
<link href="/2022/03/05/footer/privacy/"/>
<url>/2022/03/05/footer/privacy/</url>
<content type="html"><![CDATA[<h3 id="Privacy-Policy"><a href="#Privacy-Policy" class="headerlink" title="Privacy Policy"></a>Privacy Policy</h3><p><strong>Mehrdad Karami</strong> (“us”, “we”, or “our”) operates the <a href="https://java-art.github.io/">https://java-art.github.io</a> website (hereinafter referred to as the “Service”).</p><p>This page informs you of our policies regarding the collection, use and disclosure of personal data when you use our Service and the choices you have associated with that data.</p><p>We use your data to provide and improve the Service. By using the Service, you agree to the collection and use of information in accordance with this policy.</p><h4 id="Definitions"><a href="#Definitions" class="headerlink" title="Definitions"></a>Definitions</h4><p>Service: Service is the <a href="https://java-art.github.io/">https://java-art.github.io</a> website operated by <strong>Mehrdad Karami</strong>.<br>Personal Data: Personal Data means data about a living individual who can be identified from those data (or from those and other information either in our possession or likely to come into our possession).<br>Usage Data: Usage Data is data collected automatically either generated by the use of the Service or from the Service infrastructure itself (for example, the duration of a page visit).<br>Cookies: Cookies are small files stored on your device (computer or mobile device).<br>Data Controller: Data Controller means the natural or legal person who (either alone or jointly or in common with other persons) determines the purposes for which and the manner in which any personal information are, or are to be, processed.</p><p>For the purpose of this Privacy Policy, we are a Data Controller of your Personal Data.</p><p>Data Processors (or Service Providers): Data Processor (or Service Provider) means any natural or legal person who processes the data on behalf of the Data Controller.</p><p>We may use the services of various Service Providers in order to process your data more effectively.</p><p>Data Subject (or User): Data Subject is any living individual who is using our Service and is the subject of Personal Data.<br>Information Collection and Use<br>We collect several different types of information for various purposes to provide and improve our Service to you.</p><p>Types of Data Collected<br>Personal Data<br>While using our Service, we may ask you to provide us with certain personally identifiable information that can be used to contact or identify you (“Personal Data”). Personally identifiable information may include, but is not limited to:</p><p>Name<br>Email address<br>Cookies and Usage Data<br>We may use your Personal Data to contact you with newsletters, marketing or promotional materials and other information that may be of interest to you. You may opt out of receiving any, or all, of these communications from us by following the unsubscribe link or the instructions provided in any email we send.</p><h4 id="Usage-Data"><a href="#Usage-Data" class="headerlink" title="Usage Data"></a>Usage Data</h4><p>We may also collect information on how the Service is accessed and used (“Usage Data”). This Usage Data may include information such as your computer’s Internet Protocol address (e.g. IP address), browser type, browser version, the pages of our Service that you visit, the time and date of your visit, the time spent on those pages, unique device identifiers and other diagnostic data.</p><p>Tracking & Cookies Data<br>We use cookies and similar tracking technologies to track the activity on our Service and we hold certain information.</p><p>Cookies are files with a small amount of data which may include an anonymous unique identifier. Cookies are sent to your browser from a website and stored on your device. Other tracking technologies are also used such as beacons, tags and scripts to collect and track information and to improve and analyse our Service.</p><p>You can instruct your browser to refuse all cookies or to indicate when a cookie is being sent. However, if you do not accept cookies, you may not be able to use some portions of our Service.</p><h4 id="Examples-of-Cookies-we-use"><a href="#Examples-of-Cookies-we-use" class="headerlink" title="Examples of Cookies we use:"></a>Examples of Cookies we use:</h4><p>Session Cookies. We use Session Cookies to operate our Service.<br>Preference Cookies. We use Preference Cookies to remember your preferences and various settings.<br>Security Cookies. We use Security Cookies for security purposes.<br>Advertising Cookies. Advertising Cookies are used to serve you with advertisements that may be relevant to you and your interests.</p><h4 id="Use-of-Data"><a href="#Use-of-Data" class="headerlink" title="Use of Data"></a>Use of Data</h4><p>reflectoring uses the collected data for various purposes:</p><p>To provide and maintain our Service<br>To notify you about changes to our Service<br>To allow you to participate in interactive features of our Service when you choose to do so<br>To provide customer support<br>To gather analysis or valuable information so that we can improve our Service<br>To monitor the usage of our Service<br>To detect, prevent and address technical issues<br>To provide you with news, special offers and general information about other goods, services and events which we offer that are similar to those that you have already purchased or enquired about unless you have opted not to receive such information<br>Legal Basis for Processing Personal Data under the General Data Protection Regulation (GDPR)<br>If you are from the European Economic Area (EEA), reflectoring legal basis for collecting and using the personal information described in this Privacy Policy depends on the Personal Data we collect and the specific context in which we collect it.</p><p>reflectoring may process your Personal Data because:</p><p>We need to perform a contract with you<br>You have given us permission to do so<br>The processing is in our legitimate interests and it is not overridden by your rights<br>For payment processing purposes<br>To comply with the law</p><h4 id="Retention-of-Data"><a href="#Retention-of-Data" class="headerlink" title="Retention of Data"></a>Retention of Data</h4><p>reflectoring will retain your Personal Data only for as long as is necessary for the purposes set out in this Privacy Policy. We will retain and use your Personal Data to the extent necessary to comply with our legal obligations (for example, if we are required to retain your data to comply with applicable laws), resolve disputes and enforce our legal agreements and policies.</p><p>reflectoring will also retain Usage Data for internal analysis purposes. Usage Data is generally retained for a shorter period of time, except when this data is used to strengthen the security or to improve the functionality of our Service, or we are legally obligated to retain this data for longer periods.</p><h4 id="Transfer-of-Data"><a href="#Transfer-of-Data" class="headerlink" title="Transfer of Data"></a>Transfer of Data</h4><p>Your information, including Personal Data, may be transferred to — and maintained on — computers located outside of your state, province, country or other governmental jurisdiction where the data protection laws may differ from those of your jurisdiction.</p><p>Your consent to this Privacy Policy followed by your submission of such information represents your agreement to that transfer.</p><p>reflectoring will take all the steps reasonably necessary to ensure that your data is treated securely and in accordance with this Privacy Policy and no transfer of your Personal Data will take place to an organisation or a country unless there are adequate controls in place including the security of your data and other personal information.</p><h4 id="Disclosure-of-Data"><a href="#Disclosure-of-Data" class="headerlink" title="Disclosure of Data"></a>Disclosure of Data</h4><p>Business Transaction<br>If reflectoring is involved in a merger, acquisition or asset sale, your Personal Data may be transferred. We will provide notice before your Personal Data is transferred and becomes subject to a different Privacy Policy.</p><h4 id="Disclosure-for-Law-Enforcement"><a href="#Disclosure-for-Law-Enforcement" class="headerlink" title="Disclosure for Law Enforcement"></a>Disclosure for Law Enforcement</h4><p>Under certain circumstances, reflectoring may be required to disclose your Personal Data if required to do so by law or in response to valid requests by public authorities (e.g. a court or a government agency).</p><h4 id="Legal-Requirements"><a href="#Legal-Requirements" class="headerlink" title="Legal Requirements"></a>Legal Requirements</h4><p>reflectoring may disclose your Personal Data in the good faith belief that such action is necessary to:</p><p>To comply with a legal obligation<br>To protect and defend the rights or property of reflectoring<br>To prevent or investigate possible wrongdoing in connection with the Service<br>To protect the personal safety of users of the Service or the public<br>To protect against legal liability</p><h4 id="Security-of-Data"><a href="#Security-of-Data" class="headerlink" title="Security of Data"></a>Security of Data</h4><p>The security of your data is important to us but remember that no method of transmission over the Internet or method of electronic storage is 100% secure. While we strive to use commercially acceptable means to protect your Personal Data, we cannot guarantee its absolute security.</p><p>Your Data Protection Rights under the General Data Protection Regulation (GDPR)<br>If you are a resident of the European Economic Area (EEA), you have certain data protection rights. reflectoring aims to take reasonable steps to allow you to correct, amend, delete or limit the use of your Personal Data.</p><p>If you wish to be informed about what Personal Data we hold about you and if you want it to be removed from our systems, please contact us.</p><p>In certain circumstances, you have the following data protection rights:</p><p>The right to access, update or delete the information we have on you. Whenever made possible, you can access, update or request deletion of your Personal Data directly within your account settings section. If you are unable to perform these actions yourself, please contact us to assist you.</p><p>The right of rectification. You have the right to have your information rectified if that information is inaccurate or incomplete.</p><p>The right to object. You have the right to object to our processing of your Personal Data.</p><p>The right of restriction. You have the right to request that we restrict the processing of your personal information.</p><p>The right to data portability. You have the right to be provided with a copy of the information we have on you in a structured, machine-readable and commonly used format.</p><p>The right to withdraw consent. You also have the right to withdraw your consent at any time where reflectoring relied on your consent to process your personal information.</p><p>Please note that we may ask you to verify your identity before responding to such requests.</p><p>You have the right to complain to a Data Protection Authority about our collection and use of your Personal Data. For more information, please contact your local data protection authority in the European Economic Area (EEA).</p><h4 id="Service-Providers"><a href="#Service-Providers" class="headerlink" title="Service Providers"></a>Service Providers</h4><p>We may employ third party companies and individuals to facilitate our Service (“Service Providers”), provide the Service on our behalf, perform Service-related services or assist us in analysing how our Service is used.</p><p>These third parties have access to your Personal Data only to perform these tasks on our behalf and are obligated not to disclose or use it for any other purpose.</p><h4 id="MailerLite"><a href="#MailerLite" class="headerlink" title="MailerLite"></a>MailerLite</h4><p>reflectoring uses MailerLite as its email service provider.</p><p>MailerLite collects contact information, distributes emails, and tracks actions you take that assist us in measuring the performance of the website and emails. Upon subscription, MailerLite also tracks the pages you visit on the website.</p><p>Our emails may contain tracking pixels. This pixel is embedded in emails and allows us to analyze the success of our emails. Because of these tracking pixels, we may see if and when you open an email and which links within the email you click.</p><p>This behavior is not passed to third parties. All data submitted at the time of subscription to our emails is stored on MailerLite’s servers. You may access MailerLite’s privacy policy here.</p><p>At any time, you may be removed from our newsletter list by clicking on the unsubscribe button provided in each email.</p><h4 id="Analytics"><a href="#Analytics" class="headerlink" title="Analytics"></a>Analytics</h4><p>We may use third-party Service Providers to monitor and analyse the use of our Service.</p><h4 id="Google-Analytics"><a href="#Google-Analytics" class="headerlink" title="Google Analytics"></a>Google Analytics</h4><p>Google Analytics is a web analytics service offered by Google that tracks and reports website traffic. Google uses the data collected to track and monitor the use of our Service. This data is shared with other Google services. Google may use the collected data to contextualise and personalise the ads of its own advertising network.</p><p>You can opt-out of having made your activity on the Service available to Google Analytics by installing the Google Analytics opt-out browser add-on. The add-on prevents the Google Analytics JavaScript (ga.js, analytics.js and dc.js) from sharing information with Google Analytics about visits activity.</p><p>For more information on the privacy practices of Google, please visit the Google Privacy & Terms web page.</p><h4 id="Advertising"><a href="#Advertising" class="headerlink" title="Advertising"></a>Advertising</h4><p>We may use third-party Service Providers to show advertisements to you to help support and maintain our Service.</p><h4 id="Google-AdSense-amp-DoubleClick-Cookie"><a href="#Google-AdSense-amp-DoubleClick-Cookie" class="headerlink" title="Google AdSense & DoubleClick Cookie"></a>Google AdSense & DoubleClick Cookie</h4><p>Google, as a third party vendor, uses cookies to serve ads on our Service. Google’s use of the DoubleClick cookie enables it and its partners to serve ads to our users based on their visit to our Service or other websites on the Internet.</p><p>You may opt out of the use of the DoubleClick Cookie for interest-based advertising by visiting the Google Ads Settings web page.</p><h4 id="Payments"><a href="#Payments" class="headerlink" title="Payments"></a>Payments</h4><p>We may provide paid products and/or services within the Service. In that case, we use third-party services for payment processing (e.g. payment processors).</p><p>We will not store or collect your payment card details. That information is provided directly to our third-party payment processors whose use of your personal information is governed by their Privacy Policy. These payment processors adhere to the standards set by PCI-DSS as managed by the PCI Security Standards Council, which is a joint effort of brands like Visa, MasterCard, American Express and Discover. PCI-DSS requirements help ensure the secure handling of payment information.</p><p>The payment processor we work with is PayPal. Their Privacy Policy can be viewed here.</p><h4 id="Links-to-Other-Sites"><a href="#Links-to-Other-Sites" class="headerlink" title="Links to Other Sites"></a>Links to Other Sites</h4><p>Our Service may contain links to other sites that are not operated by us. If you click a third party link, you will be directed to that third party’s site. We strongly advise you to review the Privacy Policy of every site you visit.</p><p>We have no control over and assume no responsibility for the content, privacy policies or practices of any third party sites or services.</p><h4 id="Children’s-Privacy"><a href="#Children’s-Privacy" class="headerlink" title="Children’s Privacy"></a>Children’s Privacy</h4><p>Our Service does not address anyone under the age of 18 (“Children”).</p><p>We do not knowingly collect personally identifiable information from anyone under the age of 18. If you are a parent or guardian and you are aware that your Child has provided us with Personal Data, please contact us. If we become aware that we have collected Personal Data from children without verification of parental consent, we take steps to remove that information from our servers.</p><h4 id="Changes-to-This-Privacy-Policy"><a href="#Changes-to-This-Privacy-Policy" class="headerlink" title="Changes to This Privacy Policy"></a>Changes to This Privacy Policy</h4><p>We may update our Privacy Policy from time to time. We will notify you of any changes by posting the new Privacy Policy on this page.</p><p>We will let you know via email and/or a prominent notice on our Service, prior to the change becoming effective and update the “effective date” at the top of this Privacy Policy.</p><p>You are advised to review this Privacy Policy periodically for any changes. Changes to this Privacy Policy are effective when they are posted on this page.</p><h4 id="Contact-Us"><a href="#Contact-Us" class="headerlink" title="Contact Us"></a>Contact Us</h4><p>If you have any questions about this Privacy Policy, please contact us at <a href="mailto:contact@onlinejavaclass.com">contact@onlinejavaclass.com</a>.</p>]]></content>
</entry>
<entry>
<title>terms and conditions</title>
<link href="/2022/03/05/footer/terms/"/>
<url>/2022/03/05/footer/terms/</url>
<content type="html"><![CDATA[<h3 id="Terms-and-Conditions"><a href="#Terms-and-Conditions" class="headerlink" title="Terms and Conditions"></a>Terms and Conditions</h3><p>Last updated: April 17, 2019</p><p>These Terms and Conditions (“Terms”, “Terms and Conditions”) govern your relationship with <a href="https://java-art.github.io/">https://java-art.github.io</a> website (the “Service”) operated by <strong>Mehrdad Karami</strong> (“us”, “we”, or “our”).</p><p>Please read these Terms and Conditions carefully before using the Service.</p><p>Your access to and use of the Service is conditioned on your acceptance of and compliance with these Terms. These Terms apply to all visitors, users and others who access or use the Service.</p><p>By accessing or using the Service you agree to be bound by these Terms. If you disagree with any part of the terms then you may not access the Service.</p><h4 id="Purchases"><a href="#Purchases" class="headerlink" title="Purchases"></a>Purchases</h4><p>If you wish to purchase any product or service made available through the Service (“Purchase”), you may be asked to supply certain information relevant to your Purchase including, without limitation, your credit card number, the expiration date of your credit card, your billing address, and your shipping information.</p><p>You represent and warrant that: (i) you have the legal right to use any credit card(s) or other payment method(s) in connection with any Purchase; and that (ii) the information you supply to us is true, correct and complete.</p><p>By submitting such information, you grant us the right to provide the information to third parties for purposes of facilitating the completion of Purchases.</p><p>We reserve the right to refuse or cancel your order at any time for certain reasons including but not limited to: product or service availability, errors in the description or price of the product or service, error in your order or other reasons.</p><p>We reserve the right to refuse or cancel your order if fraud or an unauthorised or illegal transaction is suspected.</p><p>Rights to all purchased ebooks are governed by the eBook license.</p><p>Additional Purchases Terms and Conditions<br>G2A.COM Limited<br>When you use G2A Pay services provided by G2A.COM Limited (hereinafter referred to as the “G2A Pay services provider”) to make a purchase on our website, responsibility over your purchase will first be transferred to G2A.COM Limited before it is delivered to you. G2A.COM is becoming Merchant of Record over your purchase. G2A Pay services provider assumes primary responsibility, with our assistance, for payment and payment related customer support. The terms between G2A Pay services provider and customers who utilize services of G2A Pay are governed by separate agreements which can be found under the link <a href="https://pay.g2a.com/terms-and-conditions">https://pay.g2a.com/terms-and-conditions</a> and are not subject to the Terms on this website.</p><p>In order to proceed the payment transaction, you temporary entrusts the G2A.COM with subject of the transaction, and G2A.COM takes responsibility for the product and for the transaction processing.</p><p>With respect to customers making purchases through G2A Pay services provider checkout, (i) the Privacy Policy of G2A Pay services provider shall apply to all payments and should be reviewed before making any purchase, and (ii) the G2A Pay services provider Refund Policy shall apply to all payments unless notice is expressly provided by the relevant supplier to buyers in advance. In addition the purchase of certain products may also require shoppers to agree to one or more End-User License Agreements (or “”EULAs””) that may include additional terms set by the product supplier rather than by Us or G2A Pay services provider. You will be bound by any EULA that you agree to.</p><p>You are responsible for any fees, taxes or other costs associated with the purchase and delivery of your items resulting from charges imposed by your relationship with payment services providers or the duties and taxes imposed by your local customs officials or other regulatory body.</p><p>For customer service inquiries or disputes, You may contact us by email at <a href="mailto:support@onlinejavaclass.com">support@onlinejavaclass.com</a>. Questions related to payments made through G2A Pay services provider payment should be addressed to <a href="mailto:paymentissue@g2a.com">paymentissue@g2a.com</a>. Where possible, we will work with you and/or any user selling on our website, to resolve any disputes arising from your purchase.</p><p>Availability, Errors and Inaccuracies<br>We are constantly updating our offerings of products and services on the Service. The products or services available on our Service may be mispriced, described inaccurately, or unavailable, and we may experience delays in updating information on the Service and in our advertising on other web sites.</p><p>We cannot and do not guarantee the accuracy or completeness of any information, including prices, product images, specifications, availability, and services. We reserve the right to change or update information and to correct errors, inaccuracies, or omissions at any time without prior notice.</p><p>Contests, Sweepstakes and Promotions<br>Any contests, sweepstakes or other promotions (collectively, “Promotions”) made available through the Service may be governed by rules that are separate from these Terms. If you participate in any Promotions, please review the applicable rules as well as our Privacy Policy. If the rules for a Promotion conflict with these Terms, the Promotion rules will apply.</p><h4 id="Accounts"><a href="#Accounts" class="headerlink" title="Accounts"></a>Accounts</h4><p>When you create an account with us, you must provide us information that is accurate, complete, and current at all times. Failure to do so constitutes a breach of the Terms, which may result in immediate termination of your account on our Service.</p><p>You are responsible for safeguarding the password that you use to access the Service and for any activities or actions under your password, whether your password is with our Service or a third-party service.</p><p>You agree not to disclose your password to any third party. You must notify us immediately upon becoming aware of any breach of security or unauthorized use of your account.</p><h4 id="Copyright-Policy"><a href="#Copyright-Policy" class="headerlink" title="Copyright Policy"></a>Copyright Policy</h4><p>We respect the intellectual property rights of others. It is our policy to respond to any claim that Content posted on the Service infringes the copyright or other intellectual property infringement (“Infringement”) of any person.</p><p>If you are a copyright owner, or authorized on behalf of one, and you believe that the copyrighted work has been copied in a way that constitutes copyright infringement that is taking place through the Service, you must submit your notice in writing to the attention of “Copyright Infringement” of <a href="mailto:dmca@onlinejavaclass.com">dmca@onlinejavaclass.com</a> and include in your notice a detailed description of the alleged Infringement.</p><p>You may be held accountable for damages (including costs and attorneys’ fees) for misrepresenting that any Content is infringing your copyright.</p><h4 id="Links-To-Other-Web-Sites"><a href="#Links-To-Other-Web-Sites" class="headerlink" title="Links To Other Web Sites"></a>Links To Other Web Sites</h4><p>Our Service may contain links to third-party web sites or services that are not owned or controlled by onlinejavaclass.com.</p><p>onlinejavaclass.com has no control over, and assumes no responsibility for, the content, privacy policies, or practices of any third party web sites or services. You further acknowledge and agree that onlinejavaclass.com shall not be responsible or liable, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such content, goods or services available on or through any such web sites or services.</p><p>We strongly advise you to read the terms and conditions and privacy policies of any third-party web sites or services that you visit.</p><h4 id="Termination"><a href="#Termination" class="headerlink" title="Termination"></a>Termination</h4><p>We may terminate or suspend your account immediately, without prior notice or liability, for any reason whatsoever, including without limitation if you breach the Terms.</p><p>Upon termination, your right to use the Service will immediately cease. If you wish to terminate your account, you may simply discontinue using the Service.</p><h4 id="Limitation-Of-Liability"><a href="#Limitation-Of-Liability" class="headerlink" title="Limitation Of Liability"></a>Limitation Of Liability</h4><p>In no event shall onlinejavaclass.com, nor its directors, employees, partners, agents, suppliers, or affiliates, be liable for any indirect, incidental, special, consequential or punitive damages, including without limitation, loss of profits, data, use, goodwill, or other intangible losses, resulting from (i) your access to or use of or inability to access or use the Service; (ii) any conduct or content of any third party on the Service; (iii) any content obtained from the Service; and (iv) unauthorized access, use or alteration of your transmissions or content, whether based on warranty, contract, tort (including negligence) or any other legal theory, whether or not we have been informed of the possibility of such damage, and even if a remedy set forth herein is found to have failed of its essential purpose.</p><h4 id="Disclaimer"><a href="#Disclaimer" class="headerlink" title="Disclaimer"></a>Disclaimer</h4><p>Your use of the Service is at your sole risk. The Service is provided on an “AS IS” and “AS AVAILABLE” basis. The Service is provided without warranties of any kind, whether express or implied, including, but not limited to, implied warranties of merchantability, fitness for a particular purpose, non-infringement or course of performance.</p><p>onlinejavaclass.com, its subsidiaries, affiliates, and its licensors do not warrant that a) the Service will function uninterrupted, secure or available at any particular time or location; b) any errors or defects will be corrected; c) the Service is free of viruses or other harmful components; or d) the results of using the Service will meet your requirements.</p><h4 id="Governing-Law"><a href="#Governing-Law" class="headerlink" title="Governing Law"></a>Governing Law</h4><p>These Terms shall be governed and construed in accordance with the laws of Germany, without regard to its conflict of law provisions.</p><p>Our failure to enforce any right or provision of these Terms will not be considered a waiver of those rights. If any provision of these Terms is held to be invalid or unenforceable by a court, the remaining provisions of these Terms will remain in effect. These Terms constitute the entire agreement between us regarding our Service, and supersede and replace any prior agreements we might have between us regarding the Service.</p><h4 id="Changes"><a href="#Changes" class="headerlink" title="Changes"></a>Changes</h4><p>We reserve the right, at our sole discretion, to modify or replace these Terms at any time. If a revision is material we will try to provide at least 30 days notice prior to any new terms taking effect. What constitutes a material change will be determined at our sole discretion.</p><p>By continuing to access or use our Service after those revisions become effective, you agree to be bound by the revised terms. If you do not agree to the new terms, please stop using the Service.</p><h4 id="Contact-Us"><a href="#Contact-Us" class="headerlink" title="Contact Us"></a>Contact Us</h4><p>If you have any questions about these Terms, please contact us:</p><p>By email: <a href="mailto:contact@onlinejavaclass.com">contact@onlinejavaclass.com</a></p>]]></content>
</entry>
</search>