<?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>Forem: Gabriel Ong</title>
    <description>The latest articles on Forem by Gabriel Ong (@qgabe).</description>
    <link>https://forem.com/qgabe</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%2F376038%2F69542b85-067a-4c9a-8439-267525717b81.jpeg</url>
      <title>Forem: Gabriel Ong</title>
      <link>https://forem.com/qgabe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/qgabe"/>
    <language>en</language>
    <item>
      <title>Functional Programming Design Patterns: Part 2 - Factory Method Pattern</title>
      <dc:creator>Gabriel Ong</dc:creator>
      <pubDate>Sun, 15 Nov 2020 15:57:48 +0000</pubDate>
      <link>https://forem.com/qgabe/functional-programming-design-patterns-part-2-factory-method-pattern-e19</link>
      <guid>https://forem.com/qgabe/functional-programming-design-patterns-part-2-factory-method-pattern-e19</guid>
      <description>&lt;p&gt;&lt;em&gt;This post is &lt;strong&gt;Part 2&lt;/strong&gt; of a series on utilizing &lt;strong&gt;Design Patterns&lt;/strong&gt; in the context of &lt;strong&gt;Functional Programming&lt;/strong&gt;. If you are interested in learning more, stay tuned for more on my &lt;a href="https://dev.to/qgabe"&gt;DEV page&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;As mentioned in the first part of the series, design patterns in Software Engineering (SE) have been widely popularized by the book &lt;em&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/em&gt; written by the Gang of Four (GoF).&lt;/p&gt;

&lt;p&gt;The book further categorizes design patterns into what kind of problems these patterns are designed to solve, namely the following categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Creational&lt;/strong&gt; - Patterns that provide ways to instantiate single or groups of objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structural&lt;/strong&gt; - Patterns that provide ways to define relationships between classes and objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Behavioural&lt;/strong&gt; - Patterns that define manners of communication between classes and objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Previously, we looked at the &lt;a href="https://dev.to/qgabe/functional-programming-design-patterns-part-1-strategy-pattern"&gt;&lt;strong&gt;Strategy Pattern&lt;/strong&gt;&lt;/a&gt;, which is a &lt;em&gt;Behavioural pattern&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can learn more about the categorization of the different patterns using &lt;a href="http://www.vincehuston.org/dp/" rel="noopener noreferrer"&gt;this excellent page by Vince Huston&lt;/a&gt;.&lt;/p&gt;

&lt;center&gt;
&lt;br&gt;&lt;img alt="Vince Huston's Diagram on Design Patterns" 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%2Fi%2Fmb11ai5nwesxlzqwsf3a.png"&gt;&lt;em&gt;Vince's Periodic Table inspired diagram on Design Patterns.&lt;br&gt;[It's interactive too!](http://www.vincehuston.org/dp/)&lt;/em&gt;
&lt;/center&gt;

&lt;p&gt;For this article, we'll take a look at a &lt;em&gt;Creational pattern&lt;/em&gt;: the &lt;strong&gt;Factory Method pattern&lt;/strong&gt;.&lt;/p&gt;



&lt;h1&gt;
  
  
  The &lt;em&gt;Factory Method&lt;/em&gt; Pattern
&lt;/h1&gt;

&lt;p&gt;The &lt;em&gt;Factory Method&lt;/em&gt; design pattern describes exposing a &lt;em&gt;factory method&lt;/em&gt; in a &lt;em&gt;Creator&lt;/em&gt; class without specifying the exact &lt;em&gt;Product&lt;/em&gt; class that will be created. The factory method lets the Creator class defer instantiation of Products to its subclasses.&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%2Fi%2Fktbrzgjqercymd17jllc.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%2Fi%2Fktbrzgjqercymd17jllc.png" alt="Factory Method Pattern"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whew! Once again, what a mouthful.&lt;/p&gt;

&lt;p&gt;Let's break this definition down like we did last time. We have a few key components here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Creator&lt;/strong&gt; - An interface or abstract class that defines a factory method which returns a Product.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Product&lt;/strong&gt; - An interface or abstract class that provides an abstract layer of reference to a concrete product.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Factory Method&lt;/strong&gt; - A method that is implemented by concrete creators, which specific a concrete product to return.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this is confusing, do not fret! This is easier to visualize with some examples!&lt;/p&gt;



&lt;h1&gt;
  
  
  Retail Store Example
&lt;/h1&gt;

&lt;p&gt;In the spirit of the previous article, let us use the same Retail Store example but in a different problem context.&lt;/p&gt;

&lt;p&gt;Suppose that for the retail store, we want to introduce a simulator to better estimate our business. In our simulator, we have two different "customer modes" we can select, where we can simulate a really bad and picky customers, or simulate a really good and gracious customers.&lt;/p&gt;

&lt;p&gt;We want to differentiate the way we deal with creating the different Customers but without affecting the rest of the simulator code. We can implement the &lt;em&gt;Factory Method&lt;/em&gt; pattern as follows:&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;interface&lt;/span&gt; &lt;span class="nc"&gt;CustomerCreator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="nf"&gt;createCustomer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Customer&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;boolean&lt;/span&gt; &lt;span class="nf"&gt;isGood&lt;/span&gt;&lt;span class="o"&gt;();&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;GoodCustomerCreator&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;CustomerCreator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="nf"&gt;createCustomer&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="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;GoodCustomer&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;class&lt;/span&gt; &lt;span class="nc"&gt;BadCustomerCreator&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;CustomerCreator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="nf"&gt;createCustomer&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="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BadCustomer&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;class&lt;/span&gt; &lt;span class="nc"&gt;GoodCustomer&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Customer&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;boolean&lt;/span&gt; &lt;span class="nf"&gt;isGood&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="kc"&gt;true&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;class&lt;/span&gt; &lt;span class="nc"&gt;BadCustomer&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Customer&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;boolean&lt;/span&gt; &lt;span class="nf"&gt;isGood&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="kc"&gt;false&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, we can easily decouple the logic of having different ways to create and represent products. This pattern really shines when:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;There are an increasing number of different ways of create products.&lt;/strong&gt; Let say for example, in our store simulator, we now want a way that will randomly return us a Good or Bad customer. We can simply create a &lt;code&gt;RandomCustomerCreator&lt;/code&gt; and encapsulate the randomizing logic there. Our parent process will still only have to maintain a reference to a &lt;code&gt;Creator&lt;/code&gt; class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;There are an increasing number of products.&lt;/strong&gt; Similarly, we only need to implement the &lt;code&gt;Customer&lt;/code&gt; interface for any new customers and implement any additional logic there.&lt;/li&gt;
&lt;/ol&gt;



&lt;h1&gt;
  
  
  The Functional Approach
&lt;/h1&gt;

&lt;p&gt;Let's see Strategy Pattern implemented in a Functional Programming-friendly way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Currying
&lt;/h2&gt;

&lt;p&gt;For this pattern, we can take advantage of a concept known as &lt;strong&gt;Currying&lt;/strong&gt;. Currying is the concept mostly seen in functional programming and mathematics as the process where a function taking multiple arguments can be transformed into a function that takes in only a single argument while returning another function which accepts further arguments.&lt;/p&gt;

&lt;p&gt;Another wordy definition! Let us illustrate this with a simple example:&lt;/p&gt;

&lt;p&gt;Let us implement a function to add three numbers together:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addMyTriple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;addMyTriple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Returns 6.&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Currying this function would give us the following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addMyTriple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;addMyTriple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Returns 6.&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This looks like a we're just increasing the verbosity of our function doesn't it? But the key is in realizing that we're &lt;strong&gt;returning the inner function every time&lt;/strong&gt;. Suddenly, this becomes invaluable as we can fix variables and form other functions by using currying.&lt;/p&gt;

&lt;p&gt;Here's an example. Let's say we want to add three numbers like before, but now we want to &lt;strong&gt;fix the first number to always be &lt;code&gt;10&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can simply compose a new function by doing this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addMyDoublePlusTen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;addMyTriple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;addMyDoublePlusTen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Returns 15.&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;Now let us apply this in implementing the Factory Method Pattern.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customerCreator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;isGood&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;goodCustomer&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;badCustomer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;isGood&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;isGood&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;goodCustomerCreator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;customerCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;badCustomerCreator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;customerCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;goodCustomer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;badCustomer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, we utilize currying here to differentiate the different creator functions by their type. We do that similarly for the customers themselves.&lt;/p&gt;

&lt;p&gt;While this example seems intuitive, the key to understanding when to use the pattern is in two key nuances:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;How do you want to your instantiate objects?&lt;/strong&gt; (Order, Composition, Configurations, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How to represent the different types of objects?&lt;/strong&gt; (Data Structure, etc.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You might realize that this pattern maintains encapsulation of logic really nicely, even as the complexity of the example grows (which ties in with the whole point of using design patterns)!&lt;/p&gt;

&lt;h2&gt;
  
  
  Extending the example
&lt;/h2&gt;

&lt;p&gt;To catalyze your learning process, think about some of the following questions and how you would implement them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Suppose in our simulator we maintain a list of 10 Customers. &lt;em&gt;How would you implement the Creators for a good day and a bad day?&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;What if there were instead 3 types of Customer? &lt;em&gt;(Good, Bad, Neutral)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;h1&gt;
  
  
  What's next
&lt;/h1&gt;

&lt;p&gt;This post is &lt;strong&gt;Part 2&lt;/strong&gt; of a series on utilizing &lt;strong&gt;Design Patterns&lt;/strong&gt; in the context of &lt;strong&gt;Functional Programming&lt;/strong&gt;. If you are interested in learning more, stay tuned for more on my &lt;a href="https://dev.to/qgabe"&gt;DEV page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>functional</category>
      <category>programming</category>
      <category>designpattern</category>
    </item>
    <item>
      <title>Functional Programming Design Patterns: Part 1 - Strategy Pattern</title>
      <dc:creator>Gabriel Ong</dc:creator>
      <pubDate>Sun, 15 Nov 2020 12:52:25 +0000</pubDate>
      <link>https://forem.com/qgabe/functional-programming-design-patterns-part-1-strategy-pattern-4f92</link>
      <guid>https://forem.com/qgabe/functional-programming-design-patterns-part-1-strategy-pattern-4f92</guid>
      <description>&lt;p&gt;&lt;em&gt;This post is &lt;strong&gt;Part 1&lt;/strong&gt; of a series on utilizing &lt;strong&gt;Design Patterns&lt;/strong&gt; in the context of &lt;strong&gt;Functional Programming&lt;/strong&gt;. If you are interested in learning more, stay tuned for more on my &lt;a href="https://dev.to/qgabe"&gt;DEV page&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Made ubiquitous by the indispensable book &lt;em&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/em&gt; written by the Gang of Four, &lt;strong&gt;Design Patterns&lt;/strong&gt; in Software Engineering (SE) are often utilized as battle-tested ways to solve recurrent system design problems in a flexible and reusable way.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages-na.ssl-images-amazon.com%2Fimages%2FI%2F51szD9HC9pL._SX395_BO1%2C204%2C203%2C200_.jpg" class="article-body-image-wrapper"&gt;&lt;img alt="Design Patterns: Elements of Reusable Object-Oriented Software" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages-na.ssl-images-amazon.com%2Fimages%2FI%2F51szD9HC9pL._SX395_BO1%2C204%2C203%2C200_.jpg"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;The now must-have book in every Software Engineer's bookshelf. [&lt;a href="https://images-na.ssl-images-amazon.com/images/I/51szD9HC9pL._SX395_BO1,204,203,200_.jpg" rel="noopener noreferrer"&gt;source&lt;/a&gt;]&lt;/em&gt;&lt;/center&gt;

&lt;p&gt;However, Design Patterns are more often than not, are utilized in an &lt;strong&gt;Object-Oriented Programming&lt;/strong&gt; (OOP) fashion. Many patterns include the use of abstract classes, interfaces and other OOP features that do not make sense in the context of &lt;strong&gt;Functional Programming&lt;/strong&gt; (FP), where whole systems are composed of functions instead of concrete classes.&lt;/p&gt;

&lt;p&gt;Thus, this poses a sort of cognitive mismatch for many engineers who have internalized these OOP patterns and are finding themselves in an environment where FP is growing in popularity.&lt;/p&gt;

&lt;p&gt;This post and the subsequent series seeks to provide some instruction and examples on using some of these patterns in the context of FP.&lt;/p&gt;

&lt;h1&gt;
  
  
  The &lt;em&gt;Strategy&lt;/em&gt; Pattern
&lt;/h1&gt;

&lt;p&gt;The &lt;em&gt;Strategy&lt;/em&gt; Pattern from the GoF book is described as a way to vary a &lt;em&gt;family&lt;/em&gt; of encapsulated &lt;em&gt;algorithms&lt;/em&gt; to make them interchangeable from a runtime &lt;em&gt;context&lt;/em&gt;. That definition can be a mouthful, so let's break it down.&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%2Fi%2Ffvkjhyqtxqvjswkjg33v.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%2Fi%2Ffvkjhyqtxqvjswkjg33v.png" alt="Strategy"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;We have a few components here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Algorithm&lt;/strong&gt; - A procedure that takes some value as input, performs some steps on the input, and produces an output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strategy&lt;/strong&gt; - A common interface implemented by all Algorithms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context&lt;/strong&gt; - A Run-time environment or a parent process that seeks to utilize a different algorithm given some condition.&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Retail Store Example
&lt;/h2&gt;

&lt;p&gt;To make it easier to understand this, let us adapt this pattern in a real-life context:&lt;/p&gt;

&lt;p&gt;Suppose we have a &lt;strong&gt;retail store&lt;/strong&gt; and we want to introduce a way to implement &lt;strong&gt;different pricing algorithms&lt;/strong&gt; to calculate the &lt;strong&gt;total price of a customer's order&lt;/strong&gt; depending on the &lt;strong&gt;type of a customer&lt;/strong&gt; (&lt;em&gt;walk-in&lt;/em&gt; or &lt;em&gt;online&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;We can first define our pricing algorithms as follows:&lt;/p&gt;

&lt;h3&gt;
  
  
  Strategy and Algorithms
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;PricingStrategy&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;float&lt;/span&gt; &lt;span class="nf"&gt;getTotalPrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CustomerOrder&lt;/span&gt;&lt;span class="o"&gt;);&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;WalkInPricing&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;PricingStrategy&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;float&lt;/span&gt; &lt;span class="nf"&gt;getTotalPrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CustomerOrder&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Calculate the price per item in order&lt;/span&gt;
      &lt;span class="c1"&gt;// according to the walk-in pricing&lt;/span&gt;
      &lt;span class="o"&gt;...&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;totalPrice&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;class&lt;/span&gt; &lt;span class="nc"&gt;OnlinePricing&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;PricingStrategy&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;float&lt;/span&gt; &lt;span class="nf"&gt;getTotalPrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CustomerOrder&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Calculate the price per item in order&lt;/span&gt;
      &lt;span class="c1"&gt;// according to the online pricing&lt;/span&gt;
      &lt;span class="o"&gt;...&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;totalPrice&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Following that, we can then introduce the strategy and algorithms to the context, like so:&lt;/p&gt;

&lt;h3&gt;
  
  
  Context
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nc"&gt;PricingStrategy&lt;/span&gt; &lt;span class="n"&gt;pricing&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="c1"&gt;// First, we decide type of customer and pricing algorithm to use.&lt;/span&gt;
&lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"walkIn"&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;pricing&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;WalkInPricing&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"online"&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;pricing&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;OnlinePricing&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalCustomerType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Secondly, we get the calculated total price for the order using the pricing algorithm.&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;totalPrice&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;pricing&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getTotalPrice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;order&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;This is pretty simple and intuitive!&lt;/p&gt;

&lt;p&gt;There are a few benefits to designing the variation this way:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We avoid implementing a &lt;strong&gt;single brittle monolithic algorithm&lt;/strong&gt; that accounts for all the conditions. Or even implementing the logic in &lt;code&gt;Context&lt;/code&gt; itself! (e.g. Implementing a &lt;code&gt;PricingAlgorithm&lt;/code&gt; class that has a lot of if/else and switch statements to check the customer type).&lt;/li&gt;
&lt;li&gt;We introduce &lt;strong&gt;extensibility&lt;/strong&gt; by encapsulating the algorithms separately. If we want to introduce a new pricing algorithm for a new customer type, we simply need to extend the switch statement in &lt;code&gt;Context&lt;/code&gt; and have the new pricing algorithm implement the &lt;code&gt;Strategy&lt;/code&gt; interface.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  The Functional Approach
&lt;/h1&gt;

&lt;p&gt;Now that we've established how to use the &lt;em&gt;Strategy&lt;/em&gt; Pattern in an OOP fashion, how do we implement the same kind of logic in a FP-friendly way?&lt;/p&gt;

&lt;p&gt;Well, quite simply we can leverage the idea of &lt;strong&gt;higher-order functions&lt;/strong&gt;, where functions can take in other functions as parameters.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getMathResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mathOperation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;mathOperation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;center&gt;&lt;em&gt;A simple higher-order function.&lt;/em&gt;&lt;/center&gt;

&lt;p&gt;This allows us to represent a similar abstraction provided by using an interface in OOP!&lt;/p&gt;

&lt;p&gt;Let us explore how we can do this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;walkInPricing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CustomerOrder&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Calculate the price per item in order&lt;/span&gt;
    &lt;span class="c1"&gt;// according to the walk-in pricing&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;totalPrice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onlinePricing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CustomerOrder&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Calculate the price per item in order&lt;/span&gt;
    &lt;span class="c1"&gt;// according to the online pricing&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;totalPrice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getTotalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pricingStrategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;CustomerOrder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CustomerOrders&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;pricingStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pricing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;walkIn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;pricing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;walkInPricing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;online&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;pricing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;onlinePricing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IllegalCustomerType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;getTotalPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pricing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, we simply substitute the Strategy interface with the &lt;code&gt;getTotalPrice&lt;/code&gt; function which consumes the &lt;code&gt;pricing&lt;/code&gt; algorithm specified as a parameter and returns us the price for the given function.&lt;/p&gt;

&lt;p&gt;This structure allows us to leverage off the same extensibility and encapsulated design that the pattern gives us in the OOP approach! With lesser OOP specific syntax, the code is also shorter and more understandable, which more complex implementations can benefit from.&lt;/p&gt;

&lt;h1&gt;
  
  
  What's next
&lt;/h1&gt;

&lt;p&gt;This post is &lt;strong&gt;Part 1&lt;/strong&gt; of a series on utilizing &lt;strong&gt;Design Patterns&lt;/strong&gt; in the context of &lt;strong&gt;Functional Programming&lt;/strong&gt;. If you are interested in learning more about design patterns implementing in the Functional Programming paradigm, stay tuned for more on my &lt;a href="https://dev.to/qgabe"&gt;DEV page&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>functional</category>
      <category>programming</category>
      <category>designpattern</category>
    </item>
  </channel>
</rss>
