<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
        <title>Confluent Coding</title>
        <description>Confluent Coding - Chris Cooper</description>
        <link>http://alistanis.github.io</link>
        <link>http://alistanis.github.io</link>
        <lastBuildDate>2016-01-26T05:54:53+00:00</lastBuildDate>
        <pubDate>2016-01-26T05:54:53+00:00</pubDate>
        <ttl>1800</ttl>


        <item>
                <title>Command Pattern</title>
                <description>
&lt;p&gt;&lt;em&gt;The command pattern&lt;/em&gt;
is one of many Design Patterns first introduced by the &lt;a href=&quot;http://en.wikipedia.org/wiki/Design_Patterns&quot;&gt;Gang of Four&lt;/a&gt;.
The book Design Patterns: Elements of Reusable Object-Oriented Software is one of the few de facto books about software
that has become required reading for many reasons. The command pattern is one of the first covered in the book, I believe it to be one of the most
useful, and have seen it implemented many times in my professional career.&lt;/p&gt;

&lt;p&gt;Full code for the following tutorial can be found here: &lt;a href=&quot;https://github.com/Alistanis/DesignPatterns/tree/master/app/models/command&quot;&gt;Command Pattern&lt;/a&gt;, and
API documentation generated from that code is here: &lt;a href=&quot;http://alistanis.github.io/DesignPatterns/doc/Patterns/Command.html&quot;&gt;Command Pattern API docs&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;getting-started&quot;&gt;Getting started&lt;/h2&gt;

&lt;p&gt;I’ll begin by assuming you have a development environment set up for Ruby.
MacOSX comes with a system Ruby which, on Yosemite, is 2.0, but prior to Yosemite was 1.8.7. I recommend installing and using &lt;a href=&quot;https://rvm.io/&quot;&gt;RVM&lt;/a&gt; to manage different
versions of Ruby, but this code should work just fine on 2.0 either way.
At the time of this writing my dev environment is as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;MacOSX 10.10.1&lt;/li&gt;
  &lt;li&gt;RVM 1.26.7&lt;/li&gt;
  &lt;li&gt;Ruby 2.1.5&lt;/li&gt;
  &lt;li&gt;Homebrew 0.9.5&lt;/li&gt;
  &lt;li&gt;Rubymine 7.0.2 (my personal preference, use whatever text editor you like)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of the code is tested and run for this environment, and it should work on Linux and Windows, but I haven’t tested it. Please let me know if you have any trouble
running the samples and I’ll do my best to assist you.&lt;/p&gt;

&lt;h2 id=&quot;starting-out&quot;&gt;Starting out&lt;/h2&gt;

&lt;p&gt;The command pattern is defined as “a behavioral design pattern in which an object is used to represent and encapsulate all
the information needed to call a method at a later time.” Generally a command class will contain at least one method - execute, though often it will contain others as well,
the most popular being undo. Undo is an example of the Memento pattern and has a lot of great uses. Could you imagine not being able to undo an accidental deletion
of an entire page of text? Chances are good the underlying code in your favorite text editor uses some variation of Command and Memento.&lt;/p&gt;

&lt;p&gt;Today we’re going to build a Base Command Class and then we’ll build a Create File class that will serve as our first concrete command. I’ll also cover a few points
on inheritance and &lt;a href=&quot;http://ruby-doc.org/core-2.2.0/Proc.html&quot;&gt;Proc Objects&lt;/a&gt; and &lt;a href=&quot;http://rubylearning.com/satishtalim/ruby_blocks.html&quot;&gt;blocks&lt;/a&gt;,
so if you’re new to Ruby you should still be able to follow along. If you have trouble with any of the concepts here,
feel free to leave a comment below and I’ll try to clear it up.&lt;/p&gt;

&lt;p&gt;Let’s start defining our base class and its first few methods and fields - note - more detailed comments are contained in the actual code and the API docs.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Command&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# stores the description of the command&lt;/span&gt;
    &lt;span class=&quot;kp&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:description&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# the most recent status of the command&lt;/span&gt;
    &lt;span class=&quot;kp&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:status&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Initializes the Command Class&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Designed to be called by a child class in its initialize method&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# * +description+ - The description of the sub class&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Examples&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; #inside child class&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; def initialize&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;  desc = &amp;#39;A new child class of Command&amp;#39;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;  super(desc)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@description&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;description&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;initialized&amp;#39;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Called by the child class&amp;#39; execute function&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# * +function+ - The actual command that will be executed&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Examples&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; #inside child class&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; def execute&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;  function = Proc.new do # or Proc.new {}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;    #code to be executed&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;  end&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;  super(function) # (calls the super class&amp;#39; execute function defined below)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Command began: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@description&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Execution completed: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@description&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Called by the child class&amp;#39; undo function.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# * +function+ - The command that will be undone&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Examples&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; #inside child class&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; def undo&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;  function = Proc.new do&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;    #code to undo execute function&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt;  end&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; super(function)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;undo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Undo began: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@description&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Undo completed: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@description&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We’ve defined a base class that will act as an interface for the other commands we’re going to implement. It has two fields, a description and a status,
both of which are initialized in the initialize method. It has two other methods: execute and undo. Both of those methods take an argument called ‘function’
that will be a &lt;a href=&quot;http://ruby-doc.org/core-2.2.0/Proc.html&quot;&gt;Proc Object&lt;/a&gt; defined in our child classes.&lt;/p&gt;

&lt;p&gt;Note: It is not required to implement additional functionality into the parent class (Command) to follow the Command Pattern as it’s traditionally written.
All of our functions could be written in child classes and we could use the Command class as a true abstract class,
but I like my commands to have a little bit more flexibility - for instance, think about how easy
it would be to add logging functionality to every command simply by changing a few things in the parent class.&lt;/p&gt;

&lt;p&gt;Now let’s look at implementing a concrete Command in a child class, CreateFile. CreateFile will inherit from our Command class:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CreateFile&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Command&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Initializes the CreateFile Class&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# * +path+ - The file path to write the file&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# * +data+ - The data to write to the file&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Examples&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; create_file_cmd = CreateFile.new(file_path, data_to_write)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Create File: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Creates a file and writes data to it; will overwrite data&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Examples&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; create_file_cmd.execute&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@data&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Undoes file creation;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Examples&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#   =&amp;gt; create_file_cmd.undo&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;undo&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;no&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As we can see above, we have implemented all of the same named functions in the superclass (Command) in the child class (CreateFile). In those functions we explicitly
call the superclass function, which is designed to provide the same functionality to any Command we’d like to implement. In the execute and undo functions, we’ve created
a Proc object that contains the instructions for writing some data to a file and undoing that write respectively.&lt;/p&gt;

&lt;p&gt;Note: The function could be passed into the superclass other ways as well&lt;/p&gt;

&lt;p&gt;Parent class examples of alternate methods:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# as a block and an explicit call - block is required&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# as a block that yields - block is required&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# as a block that yields, block is optional&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;block_given?&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Child class examples:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# calling the execute method looks the same for all three parent class examples&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello!&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# for the original example:&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello!&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# or this way&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello!&amp;#39;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For a more in depth look at Procs, blocks, and Lamdas in Ruby, this is the best writeup I’ve seen on it:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/&quot;&gt;http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because we implemented our concrete class as a child class of the Command class, we can call super inside of our execute function.
In Ruby, if you write a method in a child class that has the same name as a method in the superclass, you can call the superclass method inside the child class simply
by calling super:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ChildCommand&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Command&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello!&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# calls Command.execute(function)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If we don’t call super, the superclass function will be overridden by whatever we define in the child class - example below.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello&amp;#39;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hi&amp;#39;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Hi&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Above, class B inherits from class A, and we can see that when class B is instantiated, its output is ‘Hi’ intead of ‘Hello’ because the superclass(A) initialization
function (constructor) has been overridden by the child class(B).&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Now we’ve seen how the basic command pattern works by defining an abstract interface for executing commands and for undoing them.&lt;/p&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Design-Patterns-Object-Oriented-Professional-Computing/dp/0201634988&quot;&gt;Design Patterns: Elements of reusable Object-Oriented Software&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://gameprogrammingpatterns.com/&quot;&gt;Game Programming Patterns&lt;/a&gt; - Absolutely excellent book by Bob Nystrom.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://designpatternsinruby.com/&quot;&gt;Ruby Design Patterns&lt;/a&gt; - Admittedly, though my implementation is different, I took the idea for FileIO and the command pattern from another excellent book by Ross Olsen.&lt;/li&gt;
  &lt;li&gt;Countless Stack Overflow and Google searches throughout the years&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;up-next&quot;&gt;Up next:&lt;/h2&gt;

&lt;p&gt;Next time we’ll cover a few more commands, a command list, and then we’ll write a full suite of Rspec tests to make sure our commands do what we expect them to!&lt;/p&gt;
</description>
                <link>http://alistanis.github.io/behavioral%20patterns/2015/01/03/command-pattern</link>
                <guid>http://alistanis.github.io/behavioral%20patterns/2015/01/03/command-pattern</guid>
                <pubDate>2015-01-03T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>Beginning Ruby</title>
                <description>
&lt;p&gt;&lt;em&gt;Ruby - Where to begin?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I want to make this resource as useful as possible to as many people as possible, and I realized after discussing it with my Father, who is a software engineer that
doesn’t know Ruby, that it might be a good idea to 1) Offer a list of decent resources for learning Ruby, and 2) Potentially start a beginner series of my own at
some point that can serve as a crash course for learning Ruby when you’re already an engineer. To start though, I’ll offer a small list of good resources.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ruby-lang.org/en/documentation/quickstart/&quot;&gt;Ruby Quick Start - requires Ruby and IRB(comes with Ruby)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://tryruby.org/levels/1/challenges/0&quot;&gt;Try Ruby - Interactive Online Tutorial - runs Interactive Ruby remotely&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://learnrubythehardway.org/book/&quot;&gt;Learn Ruby the Hard way - part of the ‘learn the hard way’ programming series&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://ruby-doc.com/docs/ProgrammingRuby/&quot;&gt;Programming Ruby - The Pragmatic Programmers Guide - One of my favorites, also known as “the pickaxe” and the definitive Ruby guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After you’re a little more accustomed to the language:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/markets/awesome-ruby&quot;&gt;Awesome Ruby - amazing list of Ruby resources, frameworks, libraries, and utilities&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are plenty of other great tutorials out there, and if you think you have one that deserves to be in this list, please leave a link in the comments and I’ll review it.&lt;/p&gt;

</description>
                <link>http://alistanis.github.io/beginning%20ruby/2015/01/03/beginning-ruby</link>
                <guid>http://alistanis.github.io/beginning%20ruby/2015/01/03/beginning-ruby</guid>
                <pubDate>2015-01-03T00:00:00+00:00</pubDate>
        </item>

        <item>
                <title>posts&#91;0&#93;; (first post)</title>
                <description>
&lt;h3 id=&quot;welcome-to-confluent-coding&quot;&gt;Welcome to Confluent Coding!&lt;/h3&gt;

&lt;p&gt;This is the first of what I hope to be many entries on this blog. I chose the name confluent because I want this blog to represent the joining of ideas, creativity, and information,
and I’m not sure a better word exists to represent that.
This project began because I needed a companion to go with my long term project of building a repository of design patterns, C extensions, and unit tests in Ruby.
At work we occasionally have a ‘hack Friday’ where we all take a day to try to think of and implement something creative or useful that’s not part of our regular work.
It’s a great way to let off steam and get to work with people on different teams that you don’t get to work with as often.&lt;/p&gt;

&lt;p&gt;This led to me teaching a group of junior engineers twice a week about Ruby(our current core language), Design Patterns, and how to architect sustainable systems. The purpose of Design Patterns
is not to teach how to solve every problem, as there’s certainly no catch all and there are always different ways to solve things, but is instead aimed at
exposing people who may have not read the original &lt;a href=&quot;http://www.amazon.com/Design-Patterns-Object-Oriented-Professional-Computing/dp/0201634988&quot;&gt;Gang of Four book&lt;/a&gt;.
Even if you have read the book (and if not I encourage you to), my examples are my own ideas and I’ll be taking a more modern approach towards demonstrating them. Ruby as a language provides some
of the original Design Patterns as part of the language, and I’ll highlight these when they are applicable, but I will still build a working solution so you can
take those ideas and use them if you’re working in a language that doesn’t provide those facilities out of the box.&lt;/p&gt;

&lt;p&gt;Hopefully this will be helpful not only to the engineers I’m teaching at work, but to anyone else out there looking to learn! (And that includes me: constructive criticism welcome)
Check back soon, I’ll be starting with the Command Pattern.&lt;/p&gt;
</description>
                <link>http://alistanis.github.io/welcome/2015/01/02/first-post</link>
                <guid>http://alistanis.github.io/welcome/2015/01/02/first-post</guid>
                <pubDate>2015-01-02T00:00:00+00:00</pubDate>
        </item>


</channel>
</rss>
