<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hamsheed Salamut</title>
    <description>The latest articles on DEV Community by Hamsheed Salamut (@hamsheed993).</description>
    <link>https://dev.to/hamsheed993</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%2F409505%2F0aaebcbf-139b-48ad-a739-d682a78d3d2e.png</url>
      <title>DEV Community: Hamsheed Salamut</title>
      <link>https://dev.to/hamsheed993</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hamsheed993"/>
    <language>en</language>
    <item>
      <title>Hands-On: Strategy Design Pattern in .NET Core </title>
      <dc:creator>Hamsheed Salamut</dc:creator>
      <pubDate>Thu, 11 Mar 2021 12:25:21 +0000</pubDate>
      <link>https://dev.to/hamsheed993/in-depth-look-strategy-design-pattern-in-net-core-5d09</link>
      <guid>https://dev.to/hamsheed993/in-depth-look-strategy-design-pattern-in-net-core-5d09</guid>
      <description>&lt;h2&gt;
  
  
  About Patterns
&lt;/h2&gt;

&lt;p&gt;Design patterns were introduced in Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides’s seminal work &lt;em&gt;&lt;a href="https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612" rel="noopener noreferrer"&gt;Design Patterns: Elements of Reusable Object Oriented Software&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Design patterns provide a high-level language of discourse for programmers to describe a system and to discuss solutions to common problems. The proper and intelligent usage of patterns will guide a developer into designing a system that conforms to well-established prior practices, without stifling innovation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Strategy Pattern
&lt;/h2&gt;

&lt;p&gt;The Strategy Pattern is one of the original patterns described in the famous &lt;a href="https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612" rel="noopener noreferrer"&gt;Design Patterns&lt;/a&gt; book. It is a behavioral design pattern that allows us to implement different functionalities in separate classes and make their objects interchangeable. &lt;/p&gt;

&lt;p&gt;Put simply, we have a main Context object that holds a reference towards a Strategy object and delegates it by executing its corresponding functionality.&lt;/p&gt;

&lt;p&gt;Without any further ado, let's dive into the Strategy Pattern Design implementation.&lt;/p&gt;

&lt;p&gt;This post is organized into the following sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strategy Design Pattern Structure&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implementation of the Strategy Pattern&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Common usage of the Strategy Pattern&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaways and Final Thoughts&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Strategy Design Pattern Structure
&lt;/h2&gt;

&lt;p&gt;As emphasized above, the strategy design pattern consists of a &lt;strong&gt;Context&lt;/strong&gt; object which holds a reference towards the Strategy object. For an exhaustive implementation, we need the &lt;strong&gt;Strategy&lt;/strong&gt; object - Interface to define a mechanism for the &lt;strong&gt;Context&lt;/strong&gt; object to execute the strategy and the &lt;strong&gt;Concrete Strategies&lt;/strong&gt; objects which implement the &lt;strong&gt;Strategy Interface&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Participants
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;Strategy&lt;/strong&gt; declares an interface which is implemented by all supported algorithms.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;ConcreteStrategy&lt;/strong&gt; objects implement the algorithm defined by the Strategy.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Context&lt;/strong&gt; maintains a reference to a Strategy object, and uses that reference to invoke the algorithm defined by a particular ConcreteStrategy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation of the Strategy Pattern
&lt;/h2&gt;

&lt;p&gt;For this article, let's model this pattern by talking about some different ways to cook food.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Delicious Example
&lt;/h3&gt;

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

&lt;p&gt;Being an eat-meater, there's often more than one way to cook meats under safe eating temperatures. &lt;/p&gt;

&lt;p&gt;For instance, a meat can be roasted, deep-fried or broiled. Each of these methods will get the item cooked, just via different processes. These processes, in object-oriented code can each be their own class. &lt;/p&gt;

&lt;p&gt;In our example, we will assume that we will ask the user what method they would like to use to cook their food, and then implement that method using the Strategy design pattern.&lt;/p&gt;

&lt;p&gt;To start, let's write up the &lt;strong&gt;Strategy&lt;/strong&gt; participant, the Interface &lt;code&gt;ICookStrategyService&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ICookStrategyService&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Cook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;food&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;Each strategy implements the method &lt;code&gt;Cook()&lt;/code&gt; which indicates how a food item shall be cooked. Let's implement a few of those strategies - also referred to as &lt;strong&gt;ConcreteStrategy&lt;/strong&gt; participants.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GrillingService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ICookStrategyService&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;void&lt;/span&gt; &lt;span class="nf"&gt;Cook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Cooking &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; by grilling it"&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RoastingService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ICookStrategyService&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;void&lt;/span&gt; &lt;span class="nf"&gt;Cook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Cooking &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; by roasting it"&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeepFryingService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ICookStrategyService&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;void&lt;/span&gt; &lt;span class="nf"&gt;Cook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Cooking &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; by deep frying it"&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 a typical ASP.NET Core application, we shall leverage the built-in dependency injection mechanism to register the services as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ICookingStrategyService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GrillingService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ICookingStrategyService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RoastingService&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ICookingStrategyService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DeepFryingService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We would then inject these instances in the constructor of a class to use these services. However, when we ought to run the application, we will observe that all three parameters are instances of type &lt;code&gt;DeepFryingService&lt;/code&gt; because &lt;code&gt;DeepFryingService&lt;/code&gt; has been injected last.&lt;/p&gt;

&lt;p&gt;To overcome this limitation, we will use an IEnumerable collection of services to register the services and a delegate to retrieve a specific service instance.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use a delegate to retrieve a specific service instance
&lt;/h4&gt;

&lt;p&gt;Consider the following enum that contains integer constants that correspond to the three types &lt;code&gt;Grill&lt;/code&gt;, &lt;code&gt;Roast&lt;/code&gt;, and &lt;code&gt;Fry&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;CookingType&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Grill&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;Roast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;Fry&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then declare a shared delegate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;delegate&lt;/span&gt; &lt;span class="n"&gt;ICookingStrategyService&lt;/span&gt; &lt;span class="nf"&gt;ServiceResolver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CookingType&lt;/span&gt; &lt;span class="n"&gt;cookingType&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;ConcreteStrategies&lt;/strong&gt; are then registered as &lt;code&gt;Transient&lt;/code&gt; services in the &lt;code&gt;Startup.cs&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;GrillingService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RoastingService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;DeepFryingService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Return instances based on service type at runtime
&lt;/h4&gt;

&lt;p&gt;The following code snippet shows how we can return instances of the &lt;code&gt;GrillingService&lt;/code&gt;, &lt;code&gt;RoastingService&lt;/code&gt;, and &lt;code&gt;DeepFryingService&lt;/code&gt; classes depending on the service type selected at runtime.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddScoped&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ServiceResolver&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;serviceProvider&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&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="n"&gt;key&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="n"&gt;CookingType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Grill&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;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;GrillingService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
         &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;CookingType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Roast&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;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RoastingService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
         &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;CookingType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fry&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;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;DeepFryingService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
          &lt;span class="k"&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="nf"&gt;KeyNotFoundException&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;h2&gt;
  
  
  When to use Strategy Pattern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Where different variations for some functionalities in an object and we want to switch from one variation to another in a runtime. Furthermore, if we have business applications that may have many different possible strategies in play, the strategy pattern should be the right choice for us.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaways and Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The strategy pattern achieves decoupling by encapsulating variable parts of a behavior behind an abstract interface. This abstraction can make working with a family of related algorithms easier, and it lets us postpone introduction of new &lt;strong&gt;ConcreteStrategies&lt;/strong&gt;. In addition, the strategy pattern allows for different behaviors to be implemented and tested individually.&lt;/p&gt;

&lt;p&gt;Happy Coding! 💻&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>design</category>
    </item>
    <item>
      <title>Introduction to Simple Queue Service (SQS) with Amazon Web Service (AWS)</title>
      <dc:creator>Hamsheed Salamut</dc:creator>
      <pubDate>Tue, 25 Aug 2020 15:47:39 +0000</pubDate>
      <link>https://dev.to/hamsheed993/introduction-to-simple-queue-service-sqs-with-amazon-web-service-aws-2ffd</link>
      <guid>https://dev.to/hamsheed993/introduction-to-simple-queue-service-sqs-with-amazon-web-service-aws-2ffd</guid>
      <description>&lt;h1&gt;
  
  
  Overview ℹ️
&lt;/h1&gt;

&lt;p&gt;Amazon SQS is a fully managed message queueing service offered by Amazon Web Services (AWS). Before cloud-based message queueing services existed, we often needed to create our own message queue solution. Amazon SQS helps to eliminate the overhead and the complexity of trying to create your own managed queueing solution. When using Amazon SQS, we can more easily decouple and scale our microservices and serverless applications. Amazon SQS allows you to send, store and receive any volume of messages between software components securely without worrying of losing any of the messages.&lt;/p&gt;

&lt;p&gt;Amazon SQS provides two types of queues namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Standard&lt;/strong&gt;: It is fully managed, scales from 1 message per second to 10,000s per second.
Default retention of messages: 4 days, maximum of 14 days and no limit to how many messages can be in the queue.
It has low latency (&amp;lt;10 ms on publish and receive) and horizontal scaling in terms of number of consumers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FIFO&lt;/strong&gt;: It is referred as First-In-First-Out delivery and exactly-once processing. The order in which messages are sent and received is strictly preserved and a message is delivered once and remains until consumer processes delete it. Duplication is not allowed in FIFO and it is limited to 300 transactions per second.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If your application is cloud-native, large-scale, or distributed, and does not include a messaging component, that’s probably a bug! &lt;strong&gt;Tim Bray – AWS Senior Principal Engineer&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Benefits
&lt;/h1&gt;

&lt;p&gt;Amazon SQS provides several benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce administrative overheads – AWS manages all the ongoing operations and the underlying infrastructure that is required to provide the highly available and scalable messaging service. AWS has taken the need to install and configure any messaging software.&lt;/li&gt;
&lt;li&gt;Reliably deliver messages – We are able to transmit any volume of data and any level of throughput to our Amazon SQS queue without any worry of losing any messages. When using Amazon SQS, we can increase the fault tolerance of our system by decoupling our application components, which basically means creating components or services so that they run and fail independently. We can have a service which is unavailable and still processes messages.&lt;/li&gt;
&lt;li&gt;Messages are stored redundantly across multiple availability zones – which makes messages always available when we need them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Common Usage of SQS
&lt;/h1&gt;

&lt;h3&gt;
  
  
  TV Voting System
&lt;/h3&gt;

&lt;p&gt;Suppose you are watching a TV show that allows you to vote for a winner after a performance. At the end of the performance, 5 to 50 million viewers are all voting at the same time. How are you are going to handle a large spike of traffic in such a short space of time? One solution would be to build a significant web server tier and database back-end that could manage millions of messages per second but that would be costly to scale, as you would have to pre-provision for maximum expected workload and at the same time, it might not be fault-tolerant due a database failure or throttling. Consequently, this may cause a loss of votes.&lt;/p&gt;

&lt;p&gt;As a solution to address this problem is to make use of a queueing mechanism to decouple the voting application system from the service where the vote queue will be highly scalable. This will allow to adequately absorb 10 messages/sec or 10 million messages/sec. Eventually, you would have a consumer tier which will be responsible to pull the messages from the queue as fast as possible to tally the votes. The queuing system will provide us with a more resilient system, and handle parts of our system being down.&lt;/p&gt;

&lt;h1&gt;
  
  
  Amazon SQS Architecture
&lt;/h1&gt;

&lt;p&gt;Amazon states that there are three main parts in a distributed messaging system. First, there are the components or services which generally send or receive messages from our SQS queue. The second part is the SQS queue itself. The third part is the messages inside our queue.&lt;/p&gt;

&lt;p&gt;An important concept to understand is where and how our messages are being stored. Whenever a message is sent to our SQS queue, there are redundantly stored across multiple Amazon SQS services. By distributing the same message across different SQS services, AWS can guarantee safe and successful storage of our messages. In our example, you can see messages A, B, C and D are stored in different AWS services to ensure that if anything goes wrong on AWS’s side, we are still guaranteed that we receive our messages that we have sent to our SQS queue. &lt;/p&gt;

&lt;p&gt;The diagram below illustrates the architecture of Amazon SQS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ocaQhnir--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/70xi2da3vpn4qpt836zd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ocaQhnir--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/70xi2da3vpn4qpt836zd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrapping Up 📢
&lt;/h1&gt;

&lt;p&gt;In this article, we learnt that SQS can be used to efficiently manage messages that flow through queues, and how they are consumed. The architecture of Amazon SQS illustrated in this article explains the underlying mechanism of how messages are stored and processed. In a next article, I shall demonstrate how to create, configure Amazon SQS and consume a message. &lt;/p&gt;

</description>
      <category>aws</category>
      <category>sqs</category>
    </item>
  </channel>
</rss>
