<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Henri Idrovo</title>
    <description>The latest articles on DEV Community by Henri Idrovo (@henriavo).</description>
    <link>https://dev.to/henriavo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F16451%2Fbb859f4c-f6b6-4239-bda3-fcea49a8b456.jpg</url>
      <title>DEV Community: Henri Idrovo</title>
      <link>https://dev.to/henriavo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/henriavo"/>
    <language>en</language>
    <item>
      <title>What is the cheapest deploy option on Digital Ocean?</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Thu, 22 Dec 2022 17:10:10 +0000</pubDate>
      <link>https://dev.to/henriavo/what-is-the-cheapest-deploy-option-on-digital-ocean-51ie</link>
      <guid>https://dev.to/henriavo/what-is-the-cheapest-deploy-option-on-digital-ocean-51ie</guid>
      <description>&lt;p&gt;I'm still new to the web stack. One of the first cloud providers I deployed web apps to was Heroku, and that's because that's what the developer behind &lt;a href="https://www.theverge.com/tldr/22242998/bernie-meme-generator-sanders-sitting-chair-inauguration"&gt;Bernie Sits&lt;/a&gt; used to deploy his viral website back in early 2021. Also it was FREE!&lt;/p&gt;

&lt;p&gt;But since then, Heroku has unfortunately decided to stop offering a free tier, and that &lt;a href="https://dev.to/lukeecart/more-heroku-changes-that-will-definitely-affect-you-10o8"&gt;sucks&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Can't cry for too long. So where do I deploy to now? Enter Digital Ocean. So whats the cheapest option I have on Digital Ocean? See below for what I found. 512 MB ram, single core cpu, $5 per month.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mpEVE3VK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08pgudvlnhbml8p9dx8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mpEVE3VK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08pgudvlnhbml8p9dx8g.png" alt="screenshot of digital ocean control panel" width="880" height="783"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That sounds pretty good right? Well yes, until I found what kind of server Github is giving to developers for their new &lt;a href="https://github.com/features/codespaces"&gt;Codespaces&lt;/a&gt; feature for free. 4GB ram, dual core, 32GB disk space. See below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--29B7WrIm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fd5fx6ifox7x9g6ht31.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--29B7WrIm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fd5fx6ifox7x9g6ht31.png" alt="screenshot of github codespaces control panel" width="880" height="848"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hmmmmm. That really put things in perspective 🤔&lt;/p&gt;

</description>
      <category>digitalocean</category>
      <category>cost</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Design a Web Crawler</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Wed, 16 Jun 2021 15:33:55 +0000</pubDate>
      <link>https://dev.to/henriavo/design-a-web-crawler-1ecn</link>
      <guid>https://dev.to/henriavo/design-a-web-crawler-1ecn</guid>
      <description>&lt;h2&gt;
  
  
  What is a web crawler?
&lt;/h2&gt;

&lt;p&gt;Search engines like Google need to find and index new web pages frequently so that users can quickly fetch the latest information. Record labels need to monitor for cases of copyright and trademark infringements. These are just some of the use cases for a web crawler.  &lt;/p&gt;

&lt;h2&gt;
  
  
  The Scenario
&lt;/h2&gt;

&lt;p&gt;Let's consider a use case where we will need to process 1 billion web pages per month and store the pages content for up to 5 years. &lt;/p&gt;

&lt;h2&gt;
  
  
  Estimate the Storage Requirements
&lt;/h2&gt;

&lt;p&gt;The number of queries for web pages per second (QPS):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;1 x 10&lt;sup&gt;9&lt;/sup&gt; pages / 30 days / 24 hours / 3600 seconds = 400 QPS&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There can be several reasons why the QPS can be above this estimate. So we calculate a peak QPS:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Peak QPS = 2 * QPS = 800 QPS&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The average web page size we will use is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;500 kb&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The storage required per month for storing pages:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;1 x 10&lt;sup&gt;9&lt;/sup&gt; pages X 500 kb = 500 tb&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The storage required to store pages for 5 years:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;500 tb X 12 months X 5 years = 30 pb (petabytes)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  High Level Design
&lt;/h2&gt;

&lt;p&gt;Below is what the logical flow will look like at a high level. We will then cover each step in some detail. &lt;/p&gt;

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

&lt;h4&gt;
  
  
  Seed URLS
&lt;/h4&gt;

&lt;p&gt;The seed urls are a simple text file with the URLs that will serve as the starting point of the entire crawl process. The web crawler will visit all pages that are on the same domain. For example if you were to supply &lt;code&gt;www.homedepot.com&lt;/code&gt; as a seed url, you'l find that the web crawler will search through all the store's departments like &lt;code&gt;www.homedepot.com/gardening&lt;/code&gt; and &lt;code&gt;www.homedepot.com/lighting&lt;/code&gt; and so on. &lt;/p&gt;

&lt;p&gt;The seed URLs chosen depend on your use case. If you were a record label creating a crawler to find all instances of copyright infringement, you might use seed URLs were you are most likely to find copyright infringements like &lt;code&gt;www.soundcloud.com&lt;/code&gt; and &lt;code&gt;www.youtube.com&lt;/code&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  URL Frontier
&lt;/h4&gt;

&lt;p&gt;You can think of this step as a first-in-first-out(FIFO) queue of URLs to be visited. Only URLs never visited will find their way onto this queue. Up next we'll cover two important considerations for the URL frontier. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Politeness: Our web crawler needs to avoid sending too many consecutive or parallel requests to the same server, or else you have the possibility of causing a denial-of-service(&lt;a href="https://en.wikipedia.org/wiki/Denial-of-service_attack" rel="noopener noreferrer"&gt;DOS&lt;/a&gt;)  attack. In order to avoid flooding the  host server with requests, we can implement a strategy where all requests to a particular host are processed by only one process at a time. This avoids parallel requests to the same server. &lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The above design shows the components that manage politeness. The mapping table stores a mapping between  host domain (e.g. &lt;code&gt;www.homedepot.com&lt;/code&gt;) to a single and specific queue. The queue router uses that mapping to send URLs to the appropriate queue. Each queue (1,2....N) then contains URLs from the same host. Each download process is mapped to queue and receives one URL at a time. A delay between each download task can also be added. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Priority: Not all web pages have the same level of importance. A new post on a reddit group has less priority than a new post on the associated press, for example. So we need a component in our workflow to manage priority. We'll use the same design that we used for the politeness component, except in this case the different queues represent different priority levels. And the queue selector is responsible for selecting URLs from the high priority queue more often. We'll then put these two queues in sequence. First the URLs will be placed on priority queues, then placed on politeness queues. See design below. &lt;/li&gt;
&lt;/ul&gt;

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

&lt;h4&gt;
  
  
  HTML Downloader
&lt;/h4&gt;

&lt;p&gt;Given a URL, this step makes a request to DNS and receives an IP address. Then another request to the IP address to retrieve an HTML page. &lt;/p&gt;

&lt;p&gt;There exists a file on most websites called &lt;code&gt;robots.txt&lt;/code&gt; that is used by crawlers as a guide to which pages a crawler is allowed to parse. See below for a snippet of what the &lt;code&gt;https://www.apple.com/robots.txt&lt;/code&gt; file looks like. Before attempting to crawl a site, we should make sure to check for this file and follow its rules. This robots file is also a good candidate for cacheing to avoid duplicated downloads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Disallow: /*retail/availability*
Disallow: /*retail/availabilitySearch*
Disallow: /*retail/pickupEligibility*
Disallow: /*shop/sign_in*
Disallow: /*shop/sign_out*
Disallow: /*/shop/1-800-MY-APPLE/*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Up next we'll cover a few performance optimizations for the HTML downloader&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Distributed Crawl: Like most strategies to achieve high performance, distributing tasks among several servers instead of one is ideal. We'll have more than one server in the HTML Downloader be responsible for making HTTP requests given a subset of URLs. See below for an updated diagram of our URL frontier plus a distributed HTML downloader.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cache DNS Resolver: An average response time for a DNS request can be between 10ms - 200ms. When one &lt;a href="https://en.wikipedia.org/wiki/Thread_(computing)" rel="noopener noreferrer"&gt;thread&lt;/a&gt; makes a request to DNS, the others are blocked until the first request is finished. So maintaining our own DNS cache(in memory or separate db) is an effective technique for speed optimization. This cache can be updated periodically using a simple &lt;a href="https://en.wikipedia.org/wiki/Cron" rel="noopener noreferrer"&gt;cron&lt;/a&gt; job. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Locality: Choose to deploy the servers geographically. When the HTML Downloader server is located closer to website hosts this can significantly reduce download time. This can also apply to all other components in the workflow: crawl servers, cache, queue, storage, etc. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Short timeout: Some web servers may respond slower than others, and we don't want our crawl to be slowed down by this. So it's a good idea to set a maximal wait time on the HTML downloader. If a host does not respond within a predefined time, the downloader will skip and move on to other pages.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  DNS Resolver
&lt;/h4&gt;

&lt;p&gt;A URL needs to be translated into an IP address by the DNS resolver before the HTML page can be retrieved. &lt;/p&gt;

&lt;h4&gt;
  
  
  Content Parser
&lt;/h4&gt;

&lt;p&gt;Any HTML page on the internet is not guaranteed to be free of errors or erroneous data. The content parser is responsible for validating HTML pages and filtering out malformed data. &lt;/p&gt;

&lt;h4&gt;
  
  
  Content Seen?
&lt;/h4&gt;

&lt;p&gt;With billions of pages the crawler needs to search through, we want to be sure we aren't duplicating our work by eliminating pages that have already been processed in the past. This step will check if the HTML content is already in storage, and if it is, will discard. It will do this by making a query to the content storage database. &lt;/p&gt;

&lt;h4&gt;
  
  
  Content Storage
&lt;/h4&gt;

&lt;p&gt;A storage system for HTML. This can be a traditional relational database. We can improve the speed of checking for duplicate content by storing only a hash or a checksum of an HTML page, instead of having to parse and store an entire HTML page. &lt;/p&gt;

&lt;h4&gt;
  
  
  URL Extractor
&lt;/h4&gt;

&lt;p&gt;This step parses over HTML and extracts new URLs to process. For example, below is a snippet of HTML from Apple's website. The URL Extractor will find href values and create full URLs. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdwqc37yikvfhf0smh0pe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdwqc37yikvfhf0smh0pe.png" alt="Screen Shot 2021-06-16 at 10.16.44 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  URL Filter
&lt;/h4&gt;

&lt;p&gt;This step is responsible for excluding certain content types like PDFs or JPGs, and also URLs on blacklists we may have. It finds these content types by lookin for file extensions in URLs. &lt;/p&gt;

&lt;h4&gt;
  
  
  URL Seen
&lt;/h4&gt;

&lt;p&gt;This step keeps track of URLs that have already been visited before, or that already exists in the URL Frontier. It accomplishes this by checking if the URL is already in the URL storage database. This helps reduce duplicate work and possible infinite loop conditions. &lt;/p&gt;

&lt;p&gt;&lt;sup&gt; This post is an extract from a book titled "System Design Interview – An insider's guide" By Alex Xu. &lt;/sup&gt;&lt;/p&gt;

</description>
      <category>system</category>
      <category>design</category>
      <category>crawler</category>
      <category>search</category>
    </item>
    <item>
      <title>Java 8 Stream API on Arrays</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Thu, 29 Apr 2021 20:02:02 +0000</pubDate>
      <link>https://dev.to/henriavo/java-8-stream-api-on-arrays-3epa</link>
      <guid>https://dev.to/henriavo/java-8-stream-api-on-arrays-3epa</guid>
      <description>&lt;p&gt;Java 8 introduced the concept of functional programming. My first instinct when I need to iterate is to use a for-loop. So here are some common operations on arrays using the stream api. I'll reference this post when I need a friendly reminder. USE MORE STREAMS! You can read more about java streams &lt;a href="https://www.baeldung.com/java-8-streams"&gt;here&lt;/a&gt;. Also I'm using the Java 8 method reference syntax, you can read more about that topic &lt;a href="https://www.baeldung.com/java-method-references"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Given an array of strings that represent a phone number, format and print each to console.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;phoneNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"3125550909"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"3125557676"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"3125552323"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"3125556161"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"3125554141"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;formattedPhoneNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phoneNumbers&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1-(%s)-%s-%s"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toArray&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]::&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;formattedPhoneNumbers&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Given an array of strings that represent zip codes, filter out unwanted zip codes and print remaining to console.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;zipCodes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"60640"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"94102"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"60602"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"94115"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"60647"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"94140"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;subsetZipCodes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zipCodes&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"606"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toArray&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]::&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subsetZipCodes&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Given an array of sentences, count the number of occurrences for each word and print to console.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"toast for breakfast "&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"sleepy in chicago"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"tired in the morning"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"coffee in the morning"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"sleepy breakfast today"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"breakfast in chicago"&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;

&lt;span class="nc"&gt;Map&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;wordCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"\\s+"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;Arrays:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;Integer:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

&lt;span class="n"&gt;wordCount&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s ==&amp;gt;&amp;gt; %d"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Given an array of names, and a custom NbaPlayer class, create a new array with elements of NbaPlayer type.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"lebron"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"kyrie"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"doncic"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"davis"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"lavine"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NbaPlayer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;NbaPlayer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"NbaPlayer{"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;"name='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'\''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="sc"&gt;'}'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;NbaPlayer&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;players&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;NbaPlayer:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toArray&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NbaPlayer&lt;/span&gt;&lt;span class="o"&gt;[]::&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;players&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for now. Maybe in the next post I'll do more with streams and other data structures. ArrayLists for sure. 👨🏽‍💻&lt;/p&gt;

</description>
      <category>java</category>
      <category>stream</category>
      <category>functional</category>
    </item>
    <item>
      <title>Scale from Zero to Millions of Users</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Tue, 30 Mar 2021 18:05:35 +0000</pubDate>
      <link>https://dev.to/henriavo/scale-from-zero-to-millions-of-users-1j4p</link>
      <guid>https://dev.to/henriavo/scale-from-zero-to-millions-of-users-1j4p</guid>
      <description>&lt;p&gt;Lets start off with the simplest possible website setup: 1 web server and 1 database server. (yeah this could have been simpler by running both web service AND database on the same server).&lt;/p&gt;

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

&lt;p&gt;Your choice for database type will probably be a traditional &lt;strong&gt;relational&lt;/strong&gt; database like MySQL, PostgreSQL, or Oracle. These are popular and handle most use cases. These represent data in tables and rows, and supports join operations across multiple tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Non-relational&lt;/strong&gt; databases might be the right choice for you if have the following requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Very low latency&lt;/li&gt;
&lt;li&gt;Unstructured data&lt;/li&gt;
&lt;li&gt;Serialization and de-serialization of data like json or yaml. &lt;/li&gt;
&lt;li&gt;Massive amounts of data&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Scaling Web Layer: Part 1
&lt;/h1&gt;

&lt;p&gt;Vertical scaling is the process of adding more power like CPU and RAM. Horizontal scaling is the process of adding more servers. Horizontal scaling is more desirable for large scale applications. And to accomplish this, a &lt;strong&gt;load balancer&lt;/strong&gt; is required. &lt;/p&gt;

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

&lt;h1&gt;
  
  
  Scaling Database Layer: Part 1
&lt;/h1&gt;

&lt;p&gt;We can use the same principle of horizontal scaling for the database layer. Except not all database servers will have the same responsibility. There will be 'manager' and 'worker' types. The manager server only supports write operations. The worker server gets copies of the data from the manager server and only supports read operations. All operations that make changes (INSERT, DELETE, etc) will be sent to the manager. All read-only operations will be sent to the workers. &lt;/p&gt;

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

&lt;p&gt;With database replication you gain the following advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better Performance: Most applications require a much higher ratio of reads to writes. So it is best to distribute reads among several servers. &lt;/li&gt;
&lt;li&gt;Reliability &amp;amp; High Availability: If one of your databases crashes, you do not need to worry about data loss because data is replicated. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Putting It All Together (so far)
&lt;/h1&gt;

&lt;p&gt;Ok, after successfully scaling both web and database layers, lets take a look at what we got so far. &lt;/p&gt;

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

&lt;h1&gt;
  
  
  Cache
&lt;/h1&gt;

&lt;p&gt;A cache is a temporary storage space that stores database results of frequently accessed data in memory. This way subsequent database requests will fetch the cached results, instead of making a round trip from client to database and back. This caching strategy is called a read-through cache. Below are some considerations when using a cache system. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consider using a cache when data is read frequently but modified infrequently. &lt;/li&gt;
&lt;li&gt;It's good practice to implement an expiration policy. Once cached data is expired, it is removed from cache. &lt;/li&gt;
&lt;li&gt;Inconsistency between the cache and database can happen because data-modifying operations are not a single transaction. &lt;/li&gt;
&lt;li&gt;Mitigating failures by using multiple cache servers and the over-provision of memory. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Content Delivery Network (CDN)
&lt;/h1&gt;

&lt;p&gt;A CDN is a specific type of cache where static content like images and videos are stored on a network of geographically dispersed servers. A client will make a request for something like an image from the CDN server first, and will only make a request to the web server if the content isn't available on the CDN. Below are some considerations when using a CDN. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cost: CDNs are run by third party providers who charge you for data transfers in and out. Infrequently accessed assets should not be stored here. &lt;/li&gt;
&lt;li&gt;Cache Expiration: If the expiration policy is too long, you run the risk of the delivering stale content. If the expiration policy is too short, you run the risk of repeated requests to the origin server. &lt;/li&gt;
&lt;li&gt;CDN failures: Your application should be able to gracefully recover from outages and re-route requests to the origin web server. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The architecture of our website is getting complex. Lets take another look at what our design looks like with a database cache and CDN added. &lt;/p&gt;

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

&lt;h1&gt;
  
  
  Scaling Web Layer Part 2: Stateless Web Servers
&lt;/h1&gt;

&lt;p&gt;In order to scale our web layer further we need to allow a client to make requests to any web server at any time. Currently if user A logs into their social media by making requests to server 1, user A will need to continue to make requests to server 1 if they want to remain logged in. See below for an illustration of a "sticky session" scenario. &lt;/p&gt;

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

&lt;p&gt;The opposite of the above scenario is a stateless architecture. Where a user's session data is not stored on the web server itself, but a separate persistent storage location like a relational database or s3. &lt;/p&gt;

&lt;h1&gt;
  
  
  Data Centers
&lt;/h1&gt;

&lt;p&gt;If our website grows at a fast rate and attracts international users, setting up a clone of our core architecture (web + database) in other parts of the world is common practice. A user is then geo-routed to the closest data center. GeoDNS is a DNS service that allows domain names to be resolved to IP addresses based on the geographical location of a user. The relational database that helps us achieve a stateless web layer is not duplicated. &lt;/p&gt;

&lt;p&gt;Lets take a look at our architecture now with stateless web servers and multiple data centers. &lt;/p&gt;

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

&lt;p&gt;In the above scenario, in the event of a significant data center outage, we can direct all traffic to a healthy datacenter. There are some challenges that we need to address to achieve this multi-datacenter architecture. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traffic redirection: GeoDNS can be used to direct traffic to the nearest data center depending on where a user is located. &lt;/li&gt;
&lt;li&gt;Data synchronization: Users from two different regions could use two different databases or caches. In the event of a failover, traffic must be re-routed to a datacenter where data is unavailable. A strategy to replicate data across datacenters is required. &lt;/li&gt;
&lt;li&gt;Test and deployment: The use of automated deployment tools to quickly and consistently deploy across datacenters is crucial. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Message Queues
&lt;/h1&gt;

&lt;p&gt;A message queue is a software component that decouples two steps of workflow into 'producers' and 'consumers'. Decoupling of components is a preferred architecture for building scalable and reliable applications.  The message queue will serve as a buffer and distribute asynchronous requests. &lt;/p&gt;

&lt;p&gt;Consider the use case of an application that supports video uploads. Video publishing is not instantaneous. The application will need to preform several steps like cropping, blurring, audio enhancements, and format conversions before it is available to the public. All of these tasks take time to complete. One group of servers act as the producers of video processing tasks, and another group of servers as the consumers of these tasks and will complete the actual work. See illustration below.&lt;/p&gt;

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

&lt;h1&gt;
  
  
  Logging, Metrics, and Automation
&lt;/h1&gt;

&lt;p&gt;As a website's popularity grows adding the necessary infrastructure to support logging, metrics, and automation is essential. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logging: Allows the dev team to investigate and debug errors. &lt;/li&gt;
&lt;li&gt;Metrics: Collecting and analyzing metrics that are relevant to the business helps the team gain insights. Metrics such as:

&lt;ul&gt;
&lt;li&gt;Host level metrics: CPU, Memory, disk I/O, etc. &lt;/li&gt;
&lt;li&gt;Aggregated level metrics: performance of all layers of the application: Web, Database, Cache. &lt;/li&gt;
&lt;li&gt;Key business metrics: daily active users(DAU), retention, revenue, etc. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Automation: With a system as complex as this, we need to build or leverage automation tools to improve productivity. &lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  Scaling Database Layer Part 2: Sharding
&lt;/h1&gt;

&lt;p&gt;When the amount of data in application no longer fits on one server alone, a common approach is to distribute this data among several servers. Previously we used more than one database server, but one relational table still fit on one server at a time. In this next iteration, we'll essentially split up one table over several servers. Sharding separates large databases into smaller, more easily managed parts called shards. Each shard shares the same schema, though the actual data on each shard is unique to the shard. A hash function is used when accessing data to find the corresponding shard. For this next illustation, we'll use this simple hash function: user_id % 4. &lt;/p&gt;

&lt;p&gt;The most important factor to consider when implementing a sharding strategy is the choice of the sharding/parition key. The partition key is one or more table columns that make a unique key that determine how data is distributed. In the illustration below, "user_id" is the partition key. &lt;/p&gt;

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

&lt;p&gt;Sharding is a common approach to scaling a database layer. It does come with its own set of challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Resharding Data: redistributing data on the cluster is needed when a single shard is full.&lt;/li&gt;
&lt;li&gt;Celebrity problem: is when a single shard experiences excessive queries compared to the rest of the cluster. &lt;/li&gt;
&lt;li&gt;Join Operations: running a join operation on tables that are spread among several servers is not easy. De-normalizing so that these queries can be run on a single table is a possible solution. &lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Finally the illustration above shows what the architecture looks like with everything put together. Some parts are condensed. &lt;/p&gt;

&lt;p&gt;Thanks for reading!  &lt;/p&gt;

&lt;p&gt;&lt;sup&gt;Reference: "System Design Interview An Insider's Guide" by Alex Xu. &lt;/sup&gt;&lt;/p&gt;

</description>
      <category>system</category>
      <category>design</category>
      <category>scale</category>
      <category>web</category>
    </item>
    <item>
      <title>Design Patterns: Factory Pattern, Part 2</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Sun, 14 Jan 2018 16:26:16 +0000</pubDate>
      <link>https://dev.to/henriguy/design-patterns-factory-pattern-part-2-17la</link>
      <guid>https://dev.to/henriguy/design-patterns-factory-pattern-part-2-17la</guid>
      <description>&lt;p&gt;originally posted on: &lt;a href="https://henricodesjava.blog" rel="noopener noreferrer"&gt;henricodesjava.blog&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy new year everyone! Hope you’re excited about a new year of coding, learning, and growing &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5su36i0ohyx3f0i2gv4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5su36i0ohyx3f0i2gv4x.png" alt="📈"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We last left off on chapter four of &lt;a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124/ref=sr_1_1?ie=UTF8&amp;amp;qid=1505068308&amp;amp;sr=8-1&amp;amp;keywords=head+first+design+patterns" rel="noopener noreferrer"&gt;Head First Design Patterns: A Brain-Friendly Guide&lt;/a&gt; which covers the Factory patterns. In this post we’ll cover the second pattern in the chapter, known as “Abstract Factory Pattern”.&lt;/p&gt;

&lt;p&gt;Both of the factory patterns are responsible of encapsulating object creation, in other words, encapsulating the concrete implementation. Factory method pattern accomplished this with the use of Inheritance, the Abstract Factory Pattern accomplishes this using composition.&lt;/p&gt;

&lt;p&gt;Lets continue to use our Pizza Store example from part 1. The textbook gives us a hypothetical example were we want to control what type of ingredients are used in all future pizza stores. It is important to note that all stores will use some type of dough, sauce, cheese, and clams. This means there will be several different types of each ingredients. This is were can group them into “family of ingredients”.&lt;/p&gt;

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

&lt;p&gt;What we will do is create a factory class for each one of these regions. A Chicago, New York, and California ingredient factory. And each of these factories will implement a generic ‘PizzaIngredientFactory’ java interface. Lets take a look at the class diagram for this solution. California ingredient factory is not shown for brevity.&lt;/p&gt;

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

&lt;p&gt;It’s a little messy, but take a close look at the relationships in this class diagram. What we see is that each ingredient category (Dough, Sauce, Cheese, Clams) has an interface that then has implementations appropriate to a region. And these implementations come from their respective PizzaIngredientFactory. This is a going point to give a formal definition to our pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract Factory Pattern:&lt;/strong&gt;   Provides an interface for creating a families of related or dependent objects without specifying their concrete classes.&lt;/p&gt;

&lt;p&gt;Below is the PizzaIngredientFactory interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk6l7y181fyerhg2hxcqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk6l7y181fyerhg2hxcqy.png" alt="Screen Shot 2018-01-14 at 10.04.50 AM"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Below are the NYC and Chicago implementations of this interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpntvo1xun4e8mcc1vv7e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpntvo1xun4e8mcc1vv7e.png" alt="Screen Shot 2018-01-14 at 10.05.55 AM"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqqw4596t0h46u4fw53m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqqw4596t0h46u4fw53m.png" alt="Screen Shot 2018-01-14 at 10.07.01 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We aren’t done yet. We’ll need to make changes to the Pizza so that all pizzas only use factory produced ingredients, instead of accepting any string. Below is the abstract Pizza. Take a moment to look back at my part 1 post and notice the changes made. This abstract pizza and all interfaces and implementation of the ingredients is not shown in the class diagram above for brevity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxcq2t66fp1dn5375o5h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxcq2t66fp1dn5375o5h.png" alt="Screen Shot 2018-01-14 at 10.08.10 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is what the pepperoni pizza looks like. Notice how all the responsibility of choosing a specific type of dough, sauce, and cheese, is left up to the ingredientFactory. Which in this class is the generic interface, and can be passed a concrete implementation in the constructor. The cheese, clam, and veggie pizza look similar to this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu45yr670yoiytn3jniyn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu45yr670yoiytn3jniyn.png" alt="Screen Shot 2018-01-14 at 10.09.53 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally lets take a higher level look at a client’s use of these pizzas. Below is the NYC pizza store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3r77c93zkhiro2t5bgnr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3r77c93zkhiro2t5bgnr.png" alt="Screen Shot 2018-01-14 at 10.12.20 AM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here we see the constructor for ‘NYPizzaIngredientFactory’ being set to the generic ‘PizzaIngredientFactory’ data member. Then it being passed to each pizza types constructor. BOOM, all the magic happens in the background!&lt;/p&gt;

&lt;p&gt;That’s a lot of work and the class diagram can look a little confusing, but that’s what it takes to get an implementation of the Abstract Factory Pattern. If you want to take a look at the complete code base for this example check it out &lt;a href="https://github.com/bethrobson/Head-First-Design-Patterns/tree/master/src/headfirst/designpatterns/factory/pizzaaf" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Thanks for reading  &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fur549q469ai4y55yyd0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fur549q469ai4y55yyd0d.png" alt="👋🏽"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>designpatterns</category>
      <category>factory</category>
    </item>
    <item>
      <title>Design Patterns: Factory Pattern, Part 1</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Sun, 29 Oct 2017 21:01:50 +0000</pubDate>
      <link>https://dev.to/henriavo/design-patterns-factory-pattern-part-1-6k0</link>
      <guid>https://dev.to/henriavo/design-patterns-factory-pattern-part-1-6k0</guid>
      <description>&lt;p&gt;Hi everyone. In Chapter four of  &lt;a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124/ref=sr_1_1?ie=UTF8&amp;amp;qid=1505068308&amp;amp;sr=8-1&amp;amp;keywords=head+first+design+patterns" rel="noopener noreferrer"&gt;Head First Design Patterns: A Brain-Friendly Guide&lt;/a&gt; we learn about the Factory pattern. The text begins by talking about a programming idiom known as the â€˜the Simple Factory’ as a warm up to two real Factory patterns. The first is known as “Factory Method and the second “Abstract Factory”.&lt;/p&gt;

&lt;p&gt;Lets begins by introducing the problem that the factory pattern is solving. When we write code that uses the java keyword &lt;em&gt;new&lt;/em&gt;, we are calling constructors to concrete classes and essentially creating a hard dependency on a concrete class. The snippet below shows how a concrete dependency for three different duck classes is created, this can quickly get out of hand. (chapter 1 talked about ducks a lot).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhyi9vo096rmo4smzmm3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhyi9vo096rmo4smzmm3.png" alt="Screen Shot 2017-10-22 at 4.47.35 PM.png" width="265" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Factory Patterns can help us clean this up and decouple these dependencies. A new design principle is introduced to help define this design mistake above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependency Inversion Principle:&lt;/strong&gt; Depend upon abstractions. Do not depend on concrete classes.&lt;/p&gt;

&lt;p&gt;Lets give an example to help illustrate the design pattern. I’m going to use the same pizza shop example in the text. Lets say we have our own pizza shop, and we got some code for making pizzas that looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdnevq0q2isl8lhxdg5yy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdnevq0q2isl8lhxdg5yy.png" alt="Screen Shot 2017-10-23 at 10.38.08 PM.png" width="367" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As soon as we start adding new pizza types, this class can easily get out of hand with _if-statements. _As we learned in earlier chapters we need to identify what part of the this code is likely to change, and what is likely not to change, and separate them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedjgl709pdz1nrm99sxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedjgl709pdz1nrm99sxx.png" alt="Screen Shot 2017-10-23 at 10.52.43 PM.png" width="503" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What the Factory patterns suggest we do is move all the constructors that are likely to change and put them in class of their own. Furthermore, what if our pizza store needs to be able to create groups of different types of pizzas, say if we had to make different kind of pizzas for the New York and another different type for Chicago (see below).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zk69q2vfv0gpq2n3jhv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zk69q2vfv0gpq2n3jhv.png" alt="Screen Shot 2017-10-24 at 11.40.17 AM.png" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The factory patterns we’ll introduce allow us to organize and decouple the client from the concrete classes. All factory patterns encapsulates object creation, but lets give a formal definition for the first of our factory patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Factory Method Pattern:&lt;/strong&gt;   Defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.&lt;/p&gt;

&lt;p&gt;Lets take a look at a class diagram with the factory method pattern design for our pizza store problem.&lt;/p&gt;

&lt;p&gt;First a class diagram for the group known as the “creator classes. These are the classes that are responsible for pizza creation. The abstract creator contains the common code between all pizza creations, that is prepare() bake() cut() box() methods. It also contains the two abstract methods createPizza() and orderPizza(). The concrete creators is where we use the Java keyword &lt;em&gt;new&lt;/em&gt; and instantiate classes (known as product classes).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fay52yu6wiiegpcdmfmij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fay52yu6wiiegpcdmfmij.png" alt="Screen Shot 2017-10-25 at 12.42.17 PM.png" width="432" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next we have the Product Classes. These are the classes that we instantiate in the concrete creators. The abstract product serves as a generic type so that the creators don’t need to know what kind of pizza they are making, they just need to know that it is of pizza type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3tlg9upkjqs0onmw4iif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3tlg9upkjqs0onmw4iif.png" alt="Screen Shot 2017-10-25 at 12.49.12 PM.png" width="482" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now lets take a look at some code for this Factory Method Pattern implementation for our New York and Chicago pizza stores.&lt;/p&gt;

&lt;p&gt;Abstract creator:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazjsp9v9ids7ybk4yuph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazjsp9v9ids7ybk4yuph.png" alt="Screen Shot 2017-10-29 at 11.14.29 AM.png" width="477" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;New York City concrete creator:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjxkrx1zrmly860r4wmm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjxkrx1zrmly860r4wmm6.png" alt="Screen Shot 2017-10-29 at 11.16.03 AM.png" width="345" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Chicago concrete creator:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjrb6b8q1subi0y8d3ez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjrb6b8q1subi0y8d3ez.png" alt="Screen Shot 2017-10-29 at 11.20.10 AM.png" width="409" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, our group of product classes. First the abstract product. Note that the common code between all pizzas is kept here. Obviously we don’t have real implementation for these, so we simply print to standard output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv4lp2ei1ftd435zl43a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv4lp2ei1ftd435zl43a.png" alt="Screen Shot 2017-10-29 at 11.24.25 AM.png" width="800" height="790"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I won’t show all the concrete products, but here are a two, for a New York Pizza store, and a Chicago pizza store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7pw7tr6juhmalo8u78i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7pw7tr6juhmalo8u78i.png" alt="Screen Shot 2017-10-29 at 11.27.24 AM.png" width="349" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzvj772vguqh9lax1js86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzvj772vguqh9lax1js86.png" alt="Screen Shot 2017-10-29 at 11.28.07 AM.png" width="450" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each concrete product class gets to decide the name, the type of dough, sauce and toppings it has. Here all we see are strings being set, but you get the idea. Then what happens with these ingredients is out of the concrete product’s control (it’s in the abstract product).&lt;/p&gt;

&lt;p&gt;Finally lets take a look at a client class making use of this awesome factory pattern. The nyStore and the chicagoStore are our two “factories for New York style and Chicago style pizzas. If you run this code below, you’ll see print statements that describe the preparation with each pizza’s specific ingredients and then each pizzas common baking, cutting, and boxing steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f122132bjxqzzfcuxtb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f122132bjxqzzfcuxtb.png" alt="Screen Shot 2017-10-29 at 12.35.01 PM.png" width="800" height="730"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are interested in looking and running this example, take a look at this repo &lt;a href="https://github.com/bethrobson/Head-First-Design-Patterns/tree/master/src/headfirst/designpatterns/factory/pizzafm" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Thats it! This Factory Method pattern relies on inheritance to delegate object creation. My next post will be part two on the topic of Factory patterns where I”ll talk about the Abstract Factory pattern, which relies on composition instead of inheritance. Thanks for reading  &lt;/p&gt;

</description>
      <category>uncategorized</category>
    </item>
    <item>
      <title>Design Patterns: Decorator Pattern</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Fri, 06 Oct 2017 17:36:13 +0000</pubDate>
      <link>https://dev.to/henriavo/design-patterns-decorator-pattern-5in</link>
      <guid>https://dev.to/henriavo/design-patterns-decorator-pattern-5in</guid>
      <description>&lt;p&gt;&lt;em&gt;originally posted on my blog at &lt;a href="https://henricodesjava.blog/" rel="noopener noreferrer"&gt;henricodesjava.blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hi everyone! Chapter three in &lt;a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124/ref=sr_1_1?ie=UTF8&amp;amp;qid=1505068308&amp;amp;sr=8-1&amp;amp;keywords=head+first+design+patterns" rel="noopener noreferrer"&gt;Head First Design Patterns: A Brain-Friendly Guide&lt;/a&gt; introduces us to the Decorator pattern. A pattern that is heavily used in the JDK’s java.io package. And although not perfect, it can be combined with Factory and Builder pattern (not yet covered) to make them more useful and clean. Let’s talk about the Decorator pattern.&lt;/p&gt;

&lt;p&gt;A new design principle is introduced in this chapter. &lt;em&gt;Classes should be open for extension, but closed for modification&lt;/em&gt;. And it helps us understand that inheritance is only one form of extension, but not necessarily the best way to achieve flexibility in our designs. The pattern favors composition and delegation to add new behaviors during runtime, rather than during compile-time. In our designs we should allow behavior to be extended without the need to modify existing code. Lets give this pattern a formal definition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decorator Pattern:&lt;/strong&gt; This pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.&lt;/p&gt;

&lt;p&gt;This definition doesn’t really tell us &lt;em&gt;how&lt;/em&gt; this pattern is implemented, so lets take a look at a class diagram definition. I wont reproduce the class diagram in the text, I’ll give another slightly different diagram. In the diagram below â€˜Component’ is an interface instead of an abstract class, but the general idea is still the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzv390bvlgflz2we6jadj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzv390bvlgflz2we6jadj.png" alt="Screen Shot 2017-10-06 at 11.28.57 AM.png" width="574" height="612"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let me give a concrete example thats a little different from the text. Lets say we are starting our own taco stand restaurant, and we need to write some code for our taco stand’s ordering system. And lets say our starting point is a â€˜Taco’ interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtzdwfc255jjxm4tz8nc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtzdwfc255jjxm4tz8nc.png" alt="Screen Shot 2017-10-05 at 9.59.22 PM" width="194" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are four types of tacos we want to serve: beef, chicken, pork, and veggie. Then there are four condiments we can add to a taco: cilantro, onion, avocado, and potato (we don’t put lettuce or tomato on our tacos). Now lets show a class diagram four our taco stand’s ordering system that implements the decorator patten design.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffilee6eocdnvpm0fuajq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffilee6eocdnvpm0fuajq.png" alt="Screen Shot 2017-10-06 at 11.22.11 AM.png" width="800" height="690"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now lets take a look at some actual code implementing this design. First the Taco interface(component).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fam8dfenhgvl0flk62o1l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fam8dfenhgvl0flk62o1l.png" alt="Screen Shot 2017-10-06 at 11.03.02 AM.png" width="353" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next some Taco implementations (concrete components).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5wc5ghxo5mu04afkxf04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5wc5ghxo5mu04afkxf04.png" alt="Screen Shot 2017-10-06 at 11.05.43 AM.png" width="452" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhcoa6pfv0obzv18j0crz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhcoa6pfv0obzv18j0crz.png" alt="Screen Shot 2017-10-06 at 11.07.28 AM.png" width="456" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next lets look at the Condiment Decorator (Decorator).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7behlew6uif4e6rr322z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7behlew6uif4e6rr322z.png" alt="Screen Shot 2017-10-06 at 11.09.13 AM.png" width="431" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now some condiment implementations (concrete decorators).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxv5ft34b53gx0o6pynj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxv5ft34b53gx0o6pynj8.png" alt="Screen Shot 2017-10-06 at 11.11.31 AM.png" width="578" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fszebtdcre68edbnnpa0q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fszebtdcre68edbnnpa0q.png" alt="Screen Shot 2017-10-06 at 11.12.05 AM.png" width="580" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Decorator Pattern involves a set of decorator classes(cilantro, onion, avocado, potato) that are used to wrap concrete components (beef, chicken, pork, veggie). So lets take a look at some code that glues this all together. Note that we will learn about much better ways of creating decorated objects when we cover the Factory and Builder design patterns.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0hz5fludqbvmysw5abz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0hz5fludqbvmysw5abz.png" alt="Screen Shot 2017-10-06 at 11.25.27 AM.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Good news. I checked in all this code and verified it works as expected. If you are curious you can check it out on GitHub &lt;a href="https://github.com/henriguy1210/DecoratorPattern" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That is it! That is the Decorator pattern in action, thanks for reading  !ðŸ‘‹ðŸ½&lt;/p&gt;

&lt;p&gt;&lt;a href="http://feeds.wordpress.com/1.0/gocomments/henricodesjava.wordpress.com/331/" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Ffeeds.wordpress.com%2F1.0%2Fcomments%2Fhenricodesjava.wordpress.com%2F331%2F" width="" height=""&gt;&lt;/a&gt; &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73tpdzm8zg4t8hv3t7zp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73tpdzm8zg4t8hv3t7zp.gif" width="1" height="1"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>patterns</category>
    </item>
    <item>
      <title>Design Patterns: Observer Pattern</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Sun, 01 Oct 2017 19:07:20 +0000</pubDate>
      <link>https://dev.to/henriavo/design-patterns-observer-pattern-clb</link>
      <guid>https://dev.to/henriavo/design-patterns-observer-pattern-clb</guid>
      <description>&lt;p&gt;&lt;em&gt;originally posted on my blog at &lt;a href="https://henricodesjava.blog/" rel="noopener noreferrer"&gt;henricodesjava.blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hi everyone! Chapter two in &lt;a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124/ref=sr_1_1?ie=UTF8&amp;amp;qid=1505068308&amp;amp;sr=8-1&amp;amp;keywords=head+first+design+patterns" rel="noopener noreferrer"&gt;Head First Design Patterns: A Brain-Friendly Guide&lt;/a&gt; is all about the Observer Pattern. They make note of how fundamental and frequently this pattern is used. And also allude to the idea that future patterns in this book are based on this and other pattern combinations. So lets talk about the Observer Pattern.&lt;/p&gt;

&lt;p&gt;The problem this pattern is trying to solve is about communication and management. A simple abstract example given in the text is about a newspaper publishing company, and its subscribers. Technically this relationship can be categorized as a ONE-TO-MANY relationship, one publisher, and many subscribers. The text uses the term â€˜ &lt;strong&gt;Subject&lt;/strong&gt; â€˜ to refer to the publisher, and the term â€˜ &lt;strong&gt;Observer&lt;/strong&gt; â€˜ to refer to the subscribers.&lt;/p&gt;

&lt;p&gt;Next I’ll give a more concrete example. I’ll use an example slightly different from the text to help illustrate the idea. Lets say we are a finance startup, and we have an idea for a smartwatch app that can communicate with all your financial institutions to give you a bird’s eye view of your finances. For example it can show you balances of your checking account, or show you prices of stocks you own. What we have access to is a class that can return these values for us, lets call this class &lt;strong&gt;FinancialData&lt;/strong&gt;. For the purposes of this example, we don’t care how it retrieves this information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpvagx528rpeho86b7li7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpvagx528rpeho86b7li7.png" alt="Screen Shot 2017-09-30 at 12.28.17 AM.png" width="231" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is a naive implementation of our smart watch app, along with an explanation. Of course all of this is just pseudo code, no actual smartwatch code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzcvnb32wmeronjua2q2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzcvnb32wmeronjua2q2.png" alt="Screen Shot 2017-09-30 at 1.24.50 AM.png" width="656" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are a few things wrong with this implementation. First we are in violation of a design principle from back in chapter one. _Identify the aspect of your application that vary and separate them from what stays the same. _The lines with the green arrows represent a different screen on the watch, and in the future we might want to add more screens. When that times comes, changes to this class are unfortunately unavoidable. The lines with red arrows are retrieving financial data, and while this too can change, it would be a change caused by a API change and not a change on our client side code. We are also in violation of another design principle: _Program to an interface, not an implementation. _If only there was a way we can neatly separate code that changes, from code that doesn’t, and also not program to an implementation? Introducing, the Observer pattern!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Observer Pattern&lt;/strong&gt; : Defines a one-to-many dependency between objects so that when one object changes state, all of it’s dependents are notified and updated automatically.&lt;/p&gt;

&lt;p&gt;We will accomplish this, and introduce loose coupling by the use of interfaces. Lets illustrate this definition with a class diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2kxkb3xiy7w038gb384y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2kxkb3xiy7w038gb384y.png" alt="Screen Shot 2017-09-30 at 10.56.06 AM" width="520" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Now with these class diagrams as a guide, lets take a look at an implementation for our smart watch app. First the two interfaces are below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Subject&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;registerObserver&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Observer&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;removeObserver&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Observer&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;notifyObservers&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Observer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;upate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;br&gt;&lt;br&gt;
Next lets take a look at our new FinancialData class that implements the Subject interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FinancialData&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Subject&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt; &lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;checkingBalance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;savingsBalance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;stockValue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;fourOnekValue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;weekSpending&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;monthSpending&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;FinancialData&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
      &lt;span class="n"&gt;observers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;registerObserver&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Observer&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
      &lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;removeObserver&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
      &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;indexOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
         &lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;notifyObservers&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
         &lt;span class="nc"&gt;Observer&lt;/span&gt; &lt;span class="n"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Observer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
         &lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;update&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;measurementsChanged&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
      &lt;span class="n"&gt;notifyObservers&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setNewBalanceData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;val1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;val2&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;checkingBalance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;savingsBalance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;measurementsChanged&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setNewInvestmentData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;val1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;val2&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stockValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fourOnekValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;measurementsChanged&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setNewSpendingData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;val1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;val2&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;weekSpending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;monthSpending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;measurementsChanged&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="c1"&gt;// other FinancialData methods here&lt;/span&gt;
   &lt;span class="c1"&gt;// including getter methods for balance, investment, and spending values&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;br&gt;&lt;br&gt;
Now lets take a look at one of our observers, the Balance smart watch screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BalanceScreen&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Observer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;checkingBal&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;savingsBal&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Subject&lt;/span&gt; &lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BalanceScreen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Subject&lt;/span&gt; &lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;financialData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;registerObserver&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;FinancialData&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
         &lt;span class="nc"&gt;FinancialData&lt;/span&gt; &lt;span class="n"&gt;fd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FinancialData&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;checkingBal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getCheckingBalance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
         &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;savingsBal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSavingsBalance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="c1"&gt;// other BalanceScreen methods here&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally lets take a look at a program to glue all this together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmartWatch&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="nc"&gt;FinancialData&lt;/span&gt; &lt;span class="n"&gt;financialData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FinancialData&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;BalanceScreen&lt;/span&gt; &lt;span class="n"&gt;balanceScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BalanceScreen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;InvestmentScreen&lt;/span&gt; &lt;span class="n"&gt;investmentScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InvestmentScreen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;SpendingScreen&lt;/span&gt; &lt;span class="n"&gt;spendingScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SpendingScreen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// not using an actualy finance api, so we set values manually&lt;/span&gt;
        &lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setNewBalanceData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;348.12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;3600.87&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setNewInvestmentData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;899.12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;45000.65&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;financialData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setNewSpendingData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;210.34&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;677.45&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// at this point we should see print statements for all 3 screens&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There you have it! We implemented the interfaces and created some code to test it. Now please note I haven’t actually got this code running, so there might be a bug or two in these lines. For a future post I’ll have to create a project and push this working code up on github. Thanks for reading !ðŸ‘‹ðŸ½&lt;/p&gt;

&lt;p&gt;(originally posted at my personal blog: &lt;a href="https://henricodesjava.blog" rel="noopener noreferrer"&gt;https://henricodesjava.blog&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a href="http://feeds.wordpress.com/1.0/gocomments/henricodesjava.wordpress.com/186/" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Ffeeds.wordpress.com%2F1.0%2Fcomments%2Fhenricodesjava.wordpress.com%2F186%2F" width="" height=""&gt;&lt;/a&gt; &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3ozess32n4v322hw3b9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3ozess32n4v322hw3b9.gif" width="1" height="1"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>patterns</category>
      <category>java</category>
      <category>design</category>
    </item>
    <item>
      <title>Design Patterns: Strategy Pattern</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Thu, 14 Sep 2017 17:04:07 +0000</pubDate>
      <link>https://dev.to/henriguy/design-patterns-strategy-pattern</link>
      <guid>https://dev.to/henriguy/design-patterns-strategy-pattern</guid>
      <description>&lt;p&gt;&lt;em&gt;originally posted on my blog at &lt;a href="https://henricodesjava.blog/" rel="noopener noreferrer"&gt;henricodesjava.blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hello world! I'm currently readingÂ &lt;a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124/ref=sr_1_1?ie=UTF8&amp;amp;qid=1505068308&amp;amp;sr=8-1&amp;amp;keywords=head+first+design+patterns" rel="noopener noreferrer"&gt;Head First Design Patterns: A Brain-Friendly Guide&lt;/a&gt;, and if it is your first time reading about design patterns in Object Oriented Programming(OOP), its a great start! I wouldn't recommend it as a reference guide, because there are a ton of examples, illustrations, and informal descriptions. So here is the first pattern introduced in the book.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strategy Pattern&lt;/strong&gt;:Â &lt;em&gt;Defines a family of algorithms, encapsulates each one, and makes them interchangeable. The strategy lets the algorithm vary independently from clients that use it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Sounds confusing, but it is actually pretty simple. The pattern is based on the following three principles:&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;Identify the aspect of your application that vary and separate them from what stays the same.&lt;/li&gt;
    &lt;li&gt;Program to an interface, not an implementation.&lt;/li&gt;
    &lt;li&gt;Favor composition over inheritance.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let me try to clear up what these principles mean. I'll use an example that is different from the one included in the book.&lt;/p&gt;

&lt;p&gt;Lets say we are starting a robot company. Like Boston Dynamics. We want to build all types of robots. Check out the class below that will be used for all our robot's behaviors.Â &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhenricodesjava.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-13-at-9-09-24-pm.png%3Fw%3D456" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhenricodesjava.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-13-at-9-09-24-pm.png%3Fw%3D456" alt="Screen Shot 2017-09-13 at 9.09.24 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now here is where we apply our &lt;strong&gt;first&lt;/strong&gt; principle. Our imaginary company needs to be be able to build robots that operate on land, air, or water. All our robots also need to be able to give the greeting, "Hello world!". The greeting is the behavior that stays the same. The other three behaviors are what will change from robot to robot. So we will separate the 'Walk', 'Swim', and 'Fly' behavior from the 'Greeting' behavior. Lets see how.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;second&lt;/strong&gt; principle states it is better to use interfaces rather than direct implementations. So what we will do is create interfaces for each behavior group that has the possibility of changing. This way there can exist many different implementations of each, see below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhenricodesjava.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-13-at-11-43-18-pm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhenricodesjava.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-13-at-11-43-18-pm.png" alt="Screen Shot 2017-09-13 at 11.43.18 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When it comes time to create a specific implementation of a robot, we will extend the Robot class, and in this way have access to the 'greeting' behavior. The greeting behavior can be coded in the Robot class since this will be the same for all robots we ever create. Then when it comes time to add a Walk, Swim, or Fly behavior to our new robot, it will not inherit this behavior from the original Robot class, but it will instead have an implementation of an interface assigned to it, in other words, it will be composed during runtime. This successfully separates these changing, and non-changing behaviors using interfaces. This way we follow our &lt;strong&gt;third&lt;/strong&gt; design principle. Take a look at the diagram below for an overhead view of it all, including three example robots, Dog, Shark, and Eagle robots.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhenricodesjava.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-14-at-10-40-28-am.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhenricodesjava.files.wordpress.com%2F2017%2F09%2Fscreen-shot-2017-09-14-at-10-40-28-am.png" alt="Screen Shot 2017-09-14 at 10.40.28 AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Â &lt;/p&gt;

&lt;p&gt;So thats it! That is the quick introduction to the Strategy Pattern. Above you can see what a complete class diagram would look like when we implement the Strategy Pattern. Thanks for reading ðŸ‘‹ðŸ½&lt;/p&gt;

&lt;p&gt;(originally posted at my personal blog: &lt;a href="https://henricodesjava.blog" rel="noopener noreferrer"&gt;https://henricodesjava.blog&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>oop</category>
      <category>designpattern</category>
      <category>java</category>
      <category>strategy</category>
    </item>
    <item>
      <title>my intro</title>
      <dc:creator>Henri Idrovo</dc:creator>
      <pubDate>Sat, 15 Apr 2017 17:27:17 +0000</pubDate>
      <link>https://dev.to/henriavo/my-intro</link>
      <guid>https://dev.to/henriavo/my-intro</guid>
      <description>&lt;p&gt;Hello DEV.TO! My name is Henri and I'm a software engineer from Chicago. I work for HERE Maps(&lt;a href="http://360.here.com"&gt;http://360.here.com&lt;/a&gt;) and work a lot with java, hadoop, and aws. Catch you all on the wire.&lt;/p&gt;

&lt;p&gt;Henri&lt;/p&gt;

</description>
      <category>intro</category>
      <category>firstpost</category>
    </item>
  </channel>
</rss>
