<?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: Iñaki Arroyo</title>
    <description>The latest articles on Forem by Iñaki Arroyo (@inakiarroyo).</description>
    <link>https://forem.com/inakiarroyo</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%2F72412%2F6b241ce4-5001-40bd-ad59-dae0e2d9a230.png</url>
      <title>Forem: Iñaki Arroyo</title>
      <link>https://forem.com/inakiarroyo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/inakiarroyo"/>
    <language>en</language>
    <item>
      <title>The Builder Pattern in Dart</title>
      <dc:creator>Iñaki Arroyo</dc:creator>
      <pubDate>Fri, 28 Feb 2020 09:05:42 +0000</pubDate>
      <link>https://forem.com/inakiarroyo/the-builder-pattern-in-dart-efg</link>
      <guid>https://forem.com/inakiarroyo/the-builder-pattern-in-dart-efg</guid>
      <description>&lt;p&gt;Everyone coming from OOP programming have heard about the builder pattern... "and its intent of separating the construction of a complex objets from its representation, providing a flexible solution for creating objects to programmers".&lt;/p&gt;

&lt;p&gt;My first steps with OOP was with Java and trust me it was while ago. Nowadays, I pretty much code in Javascript (React &amp;amp; Typescript) and a week ago I decided to give a try Dart...!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Okay buddy, but...! I know, I know "Builder pattern"!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is a great &lt;a href="https://dev.to/jvarness/the-builder-pattern-in-java-and-dart-cascades-5l7"&gt;Dev article&lt;/a&gt; introducing &lt;a href="https://dart.dev/guides/language/language-tour#cascade-notation-"&gt;cascade notation&lt;/a&gt; in Dart, which cover in somehow an approach to the Builder pattern, but in my desire to discover more about this new language this post explain my own implementation of the Builder pattern in Dart.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;My example is based on the 🍕 &lt;code&gt;class&lt;/code&gt; from the mentioned article, in this way it will help to connect both articles and if you have read it before this one, you will be already familiar with the code&lt;/p&gt;

&lt;p&gt;If you prefer to go directly to the code and see what my brain coded, see this &lt;a href="https://gist.github.com/inakiarroyo/e8cfd95b4c3a5538c27d96970e2956af"&gt;gist&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  First attempts
&lt;/h2&gt;

&lt;p&gt;Thinking on a common implementation of a Builder pattern in &lt;em&gt;Java&lt;/em&gt; my first attempt was to write a &lt;code&gt;class&lt;/code&gt; that contained a &lt;code&gt;static&lt;/code&gt; nested builder class such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pizza&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Pizza&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;_&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Builder&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Should be easy... but &lt;em&gt;"arg!!"&lt;/em&gt; my first error came up: &lt;em&gt;classes can't be declared inside other classes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, researching about this error my surprise was that Dart doesn't allow to declare &lt;a href="https://github.com/dart-lang/sdk/issues/3755"&gt;nested classes&lt;/a&gt;. Okay, well... maybe just use the cascade notation and get this sorted out.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NO WAY... I need to find a work around it! &lt;br&gt;
but why? what are the reasons...?&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Immutability issues

&lt;ul&gt;
&lt;li&gt;cascade notation allows to modify/mutate the value of the &lt;code&gt;class Pizza&lt;/code&gt; attributes ones it has been instantiated/"built" (classes in Dart auto-create &lt;em&gt;setters&lt;/em&gt; methods for each of their attributes if they are not declared as &lt;code&gt;final&lt;/code&gt;). So, using it breaks one of the purpose of the builder pattern: build it once and keep it as immutable across its life cycle.&lt;/li&gt;
&lt;li&gt;Those attributes declared as &lt;code&gt;final&lt;/code&gt; could not be modified by the cascade notation (there is not &lt;em&gt;setter&lt;/em&gt; method), so they will need to be initialised into the constructor... &lt;em&gt;mmm this smells as another reason of why the builder pattern was created, isn't it?&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Messy code

&lt;ul&gt;
&lt;li&gt;there is not separation between those methods used for building the &lt;code&gt;Pizza&lt;/code&gt; object and those which express what a pizza could "do" or what could be done with a Pizza.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Dart Builder pattern
&lt;/h2&gt;

&lt;p&gt;There are two main code blocks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;pizza.dart&lt;/em&gt; which contains the &lt;code&gt;class Pizza&lt;/code&gt; &amp;amp; &lt;code&gt;class PizzaBuilder&lt;/code&gt; code&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;test.dart&lt;/em&gt; which contains code explaining how to use them and showing the output of the &lt;code&gt;print&lt;/code&gt; statements from the &lt;em&gt;test.dart&lt;/em&gt; code blocks
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// pizza.dart&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pizza&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;sauce&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;toppings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;hasExtraCheese&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;Pizza&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;_builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PizzaBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; 
    &lt;span class="n"&gt;sauce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sauce&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;toppings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toppings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;hasExtraCheese&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasExtraCheese&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PizzaBuilder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;neededTopping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'cheese'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;sauce&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;PizzaBuilder&lt;/span&gt;&lt;span class="p"&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;sauce&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;toppings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;hasExtraCheese&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;setToppings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;toppings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;toppings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;neededTopping&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s"&gt;'Really, without &lt;/span&gt;&lt;span class="si"&gt;$neededTopping&lt;/span&gt;&lt;span class="s"&gt;? :('&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&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;toppings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toppings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Pizza&lt;/span&gt; &lt;span class="n"&gt;build&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Pizza&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;_builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;class Pizza&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Ups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean code and concept separation&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;class Pizza&lt;/code&gt; declares their attributes as &lt;code&gt;final&lt;/code&gt;, so they can't be modified ones they have been instantiated or built&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;class Pizza&lt;/code&gt; redefines its default constructor, declaring a &lt;a href="https://dart.dev/guides/language/language-tour#named-constructors"&gt;named constructor&lt;/a&gt; &lt;code&gt;_builder&lt;/code&gt;, which warns devs that this class must be built through the Builder pattern. In addiction, it is marked as private (library scoped) with the &lt;code&gt;_&lt;/code&gt; and it does not allow to create direct instances of itself as there is not default constructor defined&lt;/li&gt;
&lt;li&gt;Injects the &lt;code&gt;PizzaBuilder&lt;/code&gt; as attribute of the &lt;code&gt;Pizza&lt;/code&gt; constructor initialing the &lt;code&gt;final&lt;/code&gt; attributes invoking a superclass constructor using the &lt;a href="https://dart.dev/guides/language/language-tour#initializer-list"&gt;initializer-list&lt;/a&gt; mode&lt;/li&gt;
&lt;li&gt;Because attributes are declared as &lt;code&gt;final&lt;/code&gt; they don't need to be private, so we could access them directly ones the &lt;code&gt;Pizza&lt;/code&gt; object is built with no extra &lt;em&gt;getters&lt;/em&gt; code inside the class&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;class PizzaBuilder&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Ups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean code and concept separation&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PizzaBuilder&lt;/code&gt; only knows about how to build a &lt;code&gt;Pizza&lt;/code&gt;, nothing else&lt;/li&gt;
&lt;li&gt;It allows to build a &lt;code&gt;Pizza&lt;/code&gt; object using specific methods as &lt;code&gt;setToppings&lt;/code&gt; (for a fine customisation) or directly accessing the attributes by the cascade notation mode without the verbose &lt;em&gt;setters &amp;amp; getters&lt;/em&gt; code &lt;/li&gt;
&lt;li&gt;Having it as external class allows to reuse a &lt;code&gt;PizzaBuilder&lt;/code&gt; instance for building more than one &lt;code&gt;Pizza&lt;/code&gt; object&lt;/li&gt;
&lt;li&gt;It combines the Builder pattern standards with the fast and flexible Dart cascade notation technique
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// test.dart&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'___PIZZA BBQ___'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Pizza&lt;/span&gt; &lt;span class="n"&gt;pizza&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;PizzaBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'bbq'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setToppings&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;'tomato'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'cheese'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'chicken'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasExtraCheese&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sauce&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;           &lt;span class="c1"&gt;// bbq&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toppings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;        &lt;span class="c1"&gt;// [onion, cheese, chicken]&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasExtraCheese&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Pizza BBQ was ordered with a nice bbq sauce, great toppings and an amazing extra of cheese.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;PizzaBuilder&lt;/code&gt; uses the cascade notation pattern to build the &lt;code&gt;Pizza&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;uses the customise &lt;code&gt;setToppings&lt;/code&gt; method which verify the needed toppings are part of the included ones&lt;/li&gt;
&lt;li&gt;set the &lt;code&gt;hasExtraCheese&lt;/code&gt; accessing directly the attribute, because there is no need to create a verbose &lt;em&gt;setter&lt;/em&gt; method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fast, easy and flexible 🍕!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'___PIZZA Carbonara___'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Pizza&lt;/span&gt; &lt;span class="n"&gt;pizza2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;PizzaBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'cream'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasExtraCheese&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sauce&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// cream&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toppings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// null&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasExtraCheese&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This Pizza Carbonara is a bit weird, don't you think it?... the employee forgot to add toppings to it!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;...but for the coding world everything is good, nothing breaks, you can build your Pizza as you want!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'___PIZZA Margherita___'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Pizza&lt;/span&gt; &lt;span class="n"&gt;pizza3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;PizzaBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'olive-oil'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setToppings&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;'PINEAPPLE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'tomato'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasExtraCheese&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                              &lt;span class="c1"&gt;// Uncaught Error: Really, without cheese? :( &lt;/span&gt;

&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sauce&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// No output due to Uncaught Error&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toppings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// No output due to Uncaught Error&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pizza3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasExtraCheese&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// No output due to Uncaught Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Hell yeah, the amazing and famous Neapolitan pizza Margherita has been ordered!&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Melted Mozzarella cheese... wait! what? Oh, God! the employee has added PINEAPPLE as topping instead of cheese!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;...but building the Pizza by cascade notation pattern using a custom &lt;code&gt;setToppings&lt;/code&gt; method allowed the restaurant to detect that the employee made a mistake showing &lt;code&gt;Really, without cheese? :(&lt;/code&gt; on the system&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;The employee always said he was from Naples and the real Pizza was made with pineapple... :)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As I tried to explained above, combining different techniques and patterns such as Builder one along with the OOP standards and the cascade notation from Dart, we have been able to build and easy, flexible and powerful Dart Builder pattern.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;...pineapple out! cheese forever!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>dart</category>
      <category>flutter</category>
      <category>patterns</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
