<?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: Osaigbovo Emmanuel</title>
    <description>The latest articles on Forem by Osaigbovo Emmanuel (@osaigbovoemmanuel1).</description>
    <link>https://forem.com/osaigbovoemmanuel1</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%2F148400%2F5f0e6f84-79bd-4c77-9cd8-96f7e09d4ba0.jpeg</url>
      <title>Forem: Osaigbovo Emmanuel</title>
      <link>https://forem.com/osaigbovoemmanuel1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/osaigbovoemmanuel1"/>
    <language>en</language>
    <item>
      <title>Strategy Design Pattern (simple example implementation in PHP)</title>
      <dc:creator>Osaigbovo Emmanuel</dc:creator>
      <pubDate>Sun, 07 Feb 2021 21:00:28 +0000</pubDate>
      <link>https://forem.com/osaigbovoemmanuel1/strategy-design-pattern-simple-example-implementation-in-php-41ae</link>
      <guid>https://forem.com/osaigbovoemmanuel1/strategy-design-pattern-simple-example-implementation-in-php-41ae</guid>
      <description>&lt;p&gt;&lt;strong&gt;NOTE - Please everything written down here is extracted from books which I read years ago and actually jotted down in a notebook (to be honest I don't remember the names of these books), I realized that keeping this information inside a notebook is not the best of options, come on what if something happened to my notebook? Also, I wanted this to be available to everyone who might have been wanting to understand the Strategy Design Pattern as well -:)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have not read my previous post on &lt;a href="https://dev.to/osaigbovoemmanuel1/factory-design-pattern-simple-example-implementation-in-php-2780"&gt;Factory Design Pattern&lt;/a&gt;, please do well to check it out.&lt;/p&gt;

&lt;p&gt;The strategy design pattern is a type of behavioural pattern. Behavioural patterns are used to address how an application runs. As a comparison, the factory pattern can change the object type on the fly.&lt;/p&gt;

&lt;p&gt;The Strategy design pattern comes in handy when we have multiple chunks of code performing similar operations. It defines an encapsulated and interchangeable family of algorithms(an algorithm just being a process or set of code used to solve a problem).&lt;/p&gt;

&lt;p&gt;The strategy design pattern is most useful in situations where you have classes that may be similar, but not related, and differ only in their specific behaviour. For example, say you need a filtering system for strings, Different filters might include &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stripping out HTML&lt;/li&gt;
&lt;li&gt;Crossing out swear words&lt;/li&gt;
&lt;li&gt;Catching character combinations that can be used to send spam through contacts form and the like.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only quality that these three approaches have in common is that they are all applied to strings, other than that there are no commonalities that might suggest they would make sense as derived subclasses from a base class (factories).&lt;/p&gt;

&lt;p&gt;Furthermore, these filters might be applied differently on the fly. For example, an application switch might indicate whether or not HTML or swear words are allowed in the text. This, then, is a good candidate for the strategy pattern.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, start by defining an interface that dictates the needed functionality:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$str&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;ul&gt;
&lt;li&gt;Specific filters types then implement their specific versions of the interface's methods:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HTMLFilter&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;//strip out the HTML&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$str&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SwearFilter&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;//cross out swear words&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$str&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;ul&gt;
&lt;li&gt;Finally, another class would be written that could use any filter:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FormData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$input&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Filter&lt;/span&gt; &lt;span class="nv"&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;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$type&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&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;In the Strategy design pattern, an interface is implemented by more concrete classes, which in turn are used by a specific object (the context, FormData in this case).&lt;br&gt;
In that code, the process() method expects to receive an object of type Filter through which the data would then be run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$form&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;FormData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$someUserInput&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="c1"&gt;//no HTML is allowed) {&lt;/span&gt;
      &lt;span class="nv"&gt;$form&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HTMLFilter&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="c1"&gt;//no swear words allowed) { &lt;/span&gt;
      &lt;span class="nv"&gt;$form&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SwearFilter&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;



</description>
      <category>php</category>
      <category>designpatterns</category>
      <category>strategypattern</category>
    </item>
    <item>
      <title>Factory Design Pattern (simple example implementation in PHP)</title>
      <dc:creator>Osaigbovo Emmanuel</dc:creator>
      <pubDate>Sat, 06 Feb 2021 19:19:22 +0000</pubDate>
      <link>https://forem.com/osaigbovoemmanuel1/factory-design-pattern-simple-example-implementation-in-php-2780</link>
      <guid>https://forem.com/osaigbovoemmanuel1/factory-design-pattern-simple-example-implementation-in-php-2780</guid>
      <description>&lt;p&gt;&lt;strong&gt;NOTE - Please everything written down here is extracted from a book which I read years ago and actually jotted down in a notebook (to be honest I don't remember the name of this book), I realized that keeping this information inside a notebook is not the best of options, come on what if something happened to my notebook? Also, I wanted this to be available to everyone who might have been wanting to understand the Factory Design Pattern as well -:)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have not read my previous post on &lt;a href="https://dev.to/osaigbovoemmanuel1/strategy-design-pattern-simple-example-implementation-in-php-41ae"&gt;Strategy Design Pattern&lt;/a&gt; Pattern, please do well to check it out.&lt;/p&gt;

&lt;p&gt;The Factory design pattern becomes useful in situations where the type of object that needs to be generated isn't known when the program is written but only once the program is running.&lt;/p&gt;

&lt;p&gt;Another clue for when the factory pattern might be appropriate is when there is an abstract base class and different subclasses will need to be created on the fly.&lt;/p&gt;

&lt;p&gt;This particular design structure is important with the factory pattern, as once you have created the object, regardless of its specific type, the use of the object will be consistent.&lt;/p&gt;

&lt;p&gt;The factory design pattern works via a state method,commonly named create(), factory(), factoryMethod() or createInstance(). The method takes at least one argument which indicates the type of object to create, the method returns an object of that type.&lt;br&gt;
&lt;/p&gt;

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

 + create ($type) : Product
    .
    .
    .

Concrete Factory          ========&amp;gt;  Object of that class 
                                     type eg Product
+ create ($type) : Product 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the factory design pattern, the abstract factory base class is extended by concrete factory classes that will output objects of that class type.&lt;/p&gt;

&lt;p&gt;Let's see how this will be implemented in the following example. which will create a different type of shape based on the input provided to the page through the URL.&lt;/p&gt;

&lt;p&gt;To create a Factory&lt;/p&gt;

&lt;p&gt;1) Add a new PHP Script in your text editor or IDE to be named ShapeFactory.php&lt;/p&gt;

&lt;p&gt;2) Start defining the ShapeFactory class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShapeFactory&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;// the factory class will be abstract, just as the shape itself is. you will never create an instance of the ShapeFactory class. &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) Start defining the static method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// this static method will take two arguments: the type of shape to create and an array of sizes. For a rectangle, that array would contain two values, for a triangle, three, and for a circle, only one (the radius)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4) create a different object based on that type of value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&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="s1"&gt;'rectangle'&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nv"&gt;$sizes&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="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="s1"&gt;'triangle'&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Triangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nv"&gt;$sizes&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="nv"&gt;$sizes&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="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="s1"&gt;'circle'&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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="c1"&gt;//end of switch&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The switch checks the value of $type against the expected value for now just these shapes are recognized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE - A complete version of this class would throw an exception if improper values were provided&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To use the ShapeFactory class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ShapeFactory&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$GET&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'shape'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nv"&gt;$GET&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'dimentions'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="cd"&gt;/**
to create the object, the static method of the ShapeFactory class is invoked, passing it the value that came from the URL.
**/&lt;/span&gt;

&lt;span class="cd"&gt;/**
In a more realized version of this script, one would add error handling here to confirm that the shape object was created before attempting to use it in the following steps
**/&lt;/span&gt;
&lt;span class="nv"&gt;$area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$obj&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getArea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



</description>
      <category>php</category>
      <category>designpatterns</category>
      <category>factory</category>
    </item>
  </channel>
</rss>
