<?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: Abdullah Al Mahmud</title>
    <description>The latest articles on DEV Community by Abdullah Al Mahmud (@abdullahal_mahmud).</description>
    <link>https://dev.to/abdullahal_mahmud</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%2F1212210%2Fae841b87-ce9c-42cc-9ef5-2ca714a570cb.jpg</url>
      <title>DEV Community: Abdullah Al Mahmud</title>
      <link>https://dev.to/abdullahal_mahmud</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdullahal_mahmud"/>
    <language>en</language>
    <item>
      <title>Useful Math Symbols to Understand Equations Better</title>
      <dc:creator>Abdullah Al Mahmud</dc:creator>
      <pubDate>Mon, 09 Feb 2026 12:44:22 +0000</pubDate>
      <link>https://dev.to/abdullahal_mahmud/useful-math-symbols-to-understand-equations-better-5m4</link>
      <guid>https://dev.to/abdullahal_mahmud/useful-math-symbols-to-understand-equations-better-5m4</guid>
      <description>&lt;p&gt;As a postgrad student and AI enthusiast, often I have to read research papers on AI/ML topics. But there are times when I find it difficult to understand complex equations in those papers due to unknown mathematical symbols. So, I decided to make a list of all necessary mathematical symbols. I took help from chatGPT and claude to make the list. Here are few symbols from the document I created. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzj9a80c35ldfsgj4eaul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzj9a80c35ldfsgj4eaul.png" alt="math symbols (greek letters)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Frypj2iqunlspdsnhgcw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frypj2iqunlspdsnhgcw7.png" alt="math symbols (set)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgceqn78kzimc2lrk78pk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgceqn78kzimc2lrk78pk.png" alt="math symbols (calculus)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The list has following categories&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Greek letters&lt;/li&gt;
&lt;li&gt;Set theory and logic&lt;/li&gt;
&lt;li&gt;Calculus and analysis&lt;/li&gt;
&lt;li&gt;Linear algebra&lt;/li&gt;
&lt;li&gt;Probability and statistics&lt;/li&gt;
&lt;li&gt;Optimization and functions&lt;/li&gt;
&lt;li&gt;Neural network specific symbols&lt;/li&gt;
&lt;li&gt;Special symbols and operators. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can download the document from my linkedin post 😊.&lt;br&gt;


&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:7426577340593487872?collapsed=1%22" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstatic.licdn.com%2Faero-v1%2Fsc%2Fh%2Fc45fy346jw096z9pbphyyhdz7" height="auto" class="m-0"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:7426577340593487872?collapsed=1%22" rel="noopener noreferrer" class="c-link"&gt;
            Math Symbols for ML/AI Research | Abdullah Al Mahmud posted on the topic | LinkedIn
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            While reading research papers on ML/AI, sometimes I find it difficult to read equations because of different unknown symbols. So asked chatGPT and Claude to give me a comprehensive list of mathematical symbols. I thought it's worth sharing. 😀 
Do let me know if you find any mistakes!
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstatic.licdn.com%2Faero-v1%2Fsc%2Fh%2Fal2o9zrvru7aqj8e1x2rzsrca"&gt;
          linkedin.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;




</description>
      <category>ai</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>🔎Exploring Design Patterns: Strategy Pattern</title>
      <dc:creator>Abdullah Al Mahmud</dc:creator>
      <pubDate>Thu, 24 Apr 2025 15:20:14 +0000</pubDate>
      <link>https://dev.to/abdullahal_mahmud/exploring-design-patterns-strategy-pattern-3fh6</link>
      <guid>https://dev.to/abdullahal_mahmud/exploring-design-patterns-strategy-pattern-3fh6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The Strategy Design Pattern is a behavioral design pattern that enables selecting an algorithm's behavior at runtime. Rather than implementing a single algorithm directly, code receives run-time instructions as to which algorithm to use.&lt;/p&gt;

&lt;p&gt;Think of it like choosing different routes to get to work: walking, driving, or taking public transport. Each "strategy" achieves the same goal (getting to work) but uses a different approach. Instead of hardcoding your commute method, you can switch between strategies based on weather, time constraints, or other factors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern Classification
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Type&lt;/strong&gt;: Behavioral&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scope&lt;/strong&gt;: Object&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Software often needs to support multiple ways of performing the same task. Without a proper pattern, this leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conditional logic (if/else or switch statements) scattered throughout the code&lt;/li&gt;
&lt;li&gt;Difficulty adding new algorithms without modifying existing code&lt;/li&gt;
&lt;li&gt;Code that violates the Open/Closed Principle&lt;/li&gt;
&lt;li&gt;Tight coupling between the client code and the specific algorithms&lt;/li&gt;
&lt;li&gt;Challenges in unit testing individual algorithms separately&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, consider a payment processing system that must support multiple payment methods. Traditional approaches might use complex conditional branches that become increasingly difficult to maintain as more payment options are added.&lt;/p&gt;

&lt;h2&gt;
  
  
  Anti-Pattern Case Study
&lt;/h2&gt;

&lt;p&gt;Here's an example of code that doesn't use the Strategy pattern but should:&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;ShippingCalculator&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&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;shippingMethod&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;shippingCost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&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;shippingMethod&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="s"&gt;"Standard"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;// Standard shipping calculation&lt;/span&gt;
                &lt;span class="n"&gt;shippingCost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;1.5m&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="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;shippingCost&lt;/span&gt; &lt;span class="p"&gt;*=&lt;/span&gt; &lt;span class="m"&gt;0.9m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 10% discount for heavy items&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="s"&gt;"Express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;// Express shipping calculation&lt;/span&gt;
                &lt;span class="n"&gt;shippingCost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;3.0m&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="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TotalAmount&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;shippingCost&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// $5 off for orders over $100&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="s"&gt;"NextDay"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;// Next day shipping calculation&lt;/span&gt;
                &lt;span class="n"&gt;shippingCost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;4.5m&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="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Distance&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;shippingCost&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// $10 surcharge for long distances&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;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;ArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Unsupported shipping method"&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;shippingCost&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="c1"&gt;// Supporting class for the example&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;Order&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&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="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;TotalAmount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Distance&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&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;This implementation has several problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adding a new shipping method requires modifying existing code&lt;/li&gt;
&lt;li&gt;The class violates the Single Responsibility Principle by handling multiple algorithms&lt;/li&gt;
&lt;li&gt;If the calculation logic needs to be reused elsewhere, it cannot be easily extracted&lt;/li&gt;
&lt;li&gt;As more shipping methods are added, the switch statement grows unwieldy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Over time, these issues compound. When business requirements change (e.g., adding regional shipping rules or promotional discounts), the code becomes increasingly complex and error-prone. Refactoring might require changes throughout the application, leading to potential regressions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;The Strategy pattern addresses these issues by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Defining a family of algorithms (strategies)&lt;/li&gt;
&lt;li&gt;Encapsulating each algorithm in separate classes&lt;/li&gt;
&lt;li&gt;Making the algorithms interchangeable within that family&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The pattern consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strategy Interface&lt;/strong&gt;: Defines the common interface for all concrete strategies&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concrete Strategies&lt;/strong&gt;: Implement the algorithm using the strategy interface&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context&lt;/strong&gt;: Maintains a reference to a strategy object and delegates to it&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation in &lt;code&gt;C#&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Step 1: Define the strategy interface&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IShippingStrategy&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Step 2: Implement concrete strategies&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;StandardShippingStrategy&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IShippingStrategy&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;1.5m&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="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="p"&gt;*=&lt;/span&gt; &lt;span class="m"&gt;0.9m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 10% discount for heavy items&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cost&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;ExpressShippingStrategy&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IShippingStrategy&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;3.0m&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="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TotalAmount&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// $5 off for orders over $100&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cost&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;NextDayShippingStrategy&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IShippingStrategy&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;4.5m&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="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Distance&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// $10 surcharge for long distances&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cost&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="c1"&gt;// Step 3: Create the context class&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;ShippingCalculator&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IShippingStrategy&lt;/span&gt; &lt;span class="n"&gt;_strategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ShippingCalculator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IShippingStrategy&lt;/span&gt; &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_strategy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strategy&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;void&lt;/span&gt; &lt;span class="nf"&gt;SetStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IShippingStrategy&lt;/span&gt; &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_strategy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strategy&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="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&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;_strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&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;
  
  
  Step-by-Step Breakdown
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We define the &lt;code&gt;IShippingStrategy&lt;/code&gt; interface with a single method &lt;code&gt;CalculateShipping&lt;/code&gt; that takes an order and returns the shipping cost.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We implement three concrete strategies (&lt;code&gt;StandardShippingStrategy&lt;/code&gt;, &lt;code&gt;ExpressShippingStrategy&lt;/code&gt;, and &lt;code&gt;NextDayShippingStrategy&lt;/code&gt;), each with its own algorithm for calculating shipping costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We create a &lt;code&gt;ShippingCalculator&lt;/code&gt; context class that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Takes a strategy in its constructor&lt;/li&gt;
&lt;li&gt;Provides a method to change the strategy at runtime&lt;/li&gt;
&lt;li&gt;Delegates the actual calculation to the current strategy&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This approach separates the calculation algorithms from the context that uses them, making the system much more flexible and maintainable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Usage Examples
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create an order&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Order&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Weight&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;TotalAmount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;150&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Distance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;120&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;standardShipping&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StandardShippingStrategy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;expressShipping&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ExpressShippingStrategy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;nextDayShipping&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;NextDayShippingStrategy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ShippingCalculator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;standardShipping&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;standardCost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&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;$"Standard shipping cost: $&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;standardCost&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expressShipping&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;expressCost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&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;$"Express shipping cost: $&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;expressCost&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nextDayShipping&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;nextDayCost&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CalculateShipping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&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;$"Next day shipping cost: $&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nextDayCost&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// we can also create a factory to get the appropriate strategy:&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;IShippingStrategy&lt;/span&gt; &lt;span class="nf"&gt;GetShippingStrategy&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;method&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;method&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"Standard"&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StandardShippingStrategy&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="s"&gt;"Express"&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ExpressShippingStrategy&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="s"&gt;"NextDay"&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;NextDayShippingStrategy&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;=&amp;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;ArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Unsupported shipping method"&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="c1"&gt;// And use it like this:&lt;/span&gt;
&lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;GetShippingStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Express"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Open/Closed Principle&lt;/strong&gt;: You can add new strategies without modifying existing code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Responsibility Principle&lt;/strong&gt;: Each strategy encapsulates its own algorithm&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Testability&lt;/strong&gt;: Strategies can be tested independently&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runtime Flexibility&lt;/strong&gt;: Algorithms can be switched during runtime&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Elimination of Conditional Logic&lt;/strong&gt;: Reduces or eliminates complex if/else or switch statements&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Reuse&lt;/strong&gt;: Strategies can be reused across different contexts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainability&lt;/strong&gt;: Easier to understand and maintain multiple simple classes than one complex class&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Disadvantages and Limitations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increased Number of Classes&lt;/strong&gt;: Creates more classes, which can add complexity&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client Must Be Aware of Strategies&lt;/strong&gt;: The client must know which strategy to select&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Communication Overhead&lt;/strong&gt;: Strategies might need data from the context, requiring parameter passing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overkill for Simple Cases&lt;/strong&gt;: May be unnecessary for applications with only one or two simple algorithms&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Usage&lt;/strong&gt;: Additional objects are created, which might impact performance in memory-constrained environments&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Related Patterns
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Factory Method&lt;/strong&gt;: Often used with Strategy to instantiate the appropriate strategy&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices and Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use dependency injection to provide strategies to the context&lt;/li&gt;
&lt;li&gt;Consider using a factory method to create strategies&lt;/li&gt;
&lt;li&gt;Keep the strategy interface focused and cohesive&lt;/li&gt;
&lt;li&gt;Ensure strategies have access to all the data they need to function&lt;/li&gt;
&lt;li&gt;Use default strategies when appropriate&lt;/li&gt;
&lt;li&gt;Document the behavior of each strategy clearly&lt;/li&gt;
&lt;li&gt;Consider making strategies stateless to improve reusability&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The Strategy Design Pattern is a powerful tool for managing algorithms, behaviors, or processes that need to vary independently from clients that use them. By encapsulating different algorithms in separate classes with a common interface, it promotes code that is more maintainable, extensible, and adheres to SOLID principles.&lt;/p&gt;

&lt;p&gt;When you find yourself writing complex conditional logic to choose between different algorithms or behaviors, consider the Strategy pattern. It's particularly valuable in systems that need to support multiple ways of performing the same task, especially when those algorithms need to be selected at runtime or are likely to change over time.&lt;/p&gt;

&lt;p&gt;🌟Find design pattern examples &lt;a href="https://github.com/MahmudMridul/DesignPatterns" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>designpatterns</category>
      <category>backend</category>
    </item>
    <item>
      <title>C# &amp; .NET Insights: AsEnumerable in LINQ</title>
      <dc:creator>Abdullah Al Mahmud</dc:creator>
      <pubDate>Fri, 18 Apr 2025 11:59:41 +0000</pubDate>
      <link>https://dev.to/abdullahal_mahmud/c-net-insights-pgp</link>
      <guid>https://dev.to/abdullahal_mahmud/c-net-insights-pgp</guid>
      <description>&lt;p&gt;The AsEnumerable LINQ function might seem redundant at first glance since it simply returns the source collection as an IEnumerable. However, it serves some important purposes in LINQ operations. &lt;/p&gt;

&lt;p&gt;Here are some practical examples:&lt;br&gt;
✔️ Deferring query execution from LINQ to SQL/Entity Framework&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmzoh8bvh1ivydzxr7w07.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmzoh8bvh1ivydzxr7w07.png" alt="Deferring query execution from LINQ to SQL/Entity Framework" width="723" height="402"&gt;&lt;/a&gt;&lt;br&gt;
✔️ Change LINQ provider/context&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0b00h3epjkj4phfoxcxj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0b00h3epjkj4phfoxcxj.png" alt="Change LINQ provider" width="669" height="318"&gt;&lt;/a&gt;&lt;br&gt;
✔️ Using custom implementation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffwnfuww4b85tdhgymaah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffwnfuww4b85tdhgymaah.png" alt="Using custom implementation" width="705" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>🔎Exploring Design Patterns: Adapter Pattern</title>
      <dc:creator>Abdullah Al Mahmud</dc:creator>
      <pubDate>Sat, 16 Nov 2024 10:42:07 +0000</pubDate>
      <link>https://dev.to/abdullahal_mahmud/exploring-design-patterns-adapter-pattern-3o91</link>
      <guid>https://dev.to/abdullahal_mahmud/exploring-design-patterns-adapter-pattern-3o91</guid>
      <description>&lt;h3&gt;
  
  
  General Structure:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Target (IJsonAdapter):&lt;/strong&gt; The interface that clients expect to use&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adaptee (LegacyXmlSystem):&lt;/strong&gt; The existing class that needs to be adapted&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adapter (XmlToJsonAdapter):&lt;/strong&gt; The class that bridges between Target and Adaptee&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; The code that works with the Target interface&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface IJsonAdapter
{
    string GetJsonData();
}

// Adaptee: Legacy system that needs to be adapted
public class LegacyXmlSystem
{
    public string GetXmlData()
    {
        return "&amp;lt;data&amp;gt; some XML data &amp;lt;/data&amp;gt;";
    }
}

// Adapter: Adapter implementation
public class  XmlToJsonAdapter : IJsonAdapter
{
    private LegacyXmlSystem _xmlSystem;

    public XmlToJsonAdapter(LegacyXmlSystem xmlSystem)
    {
        _xmlSystem = xmlSystem;
    }

    public string GetJsonData()
    {
        string xmlData = _xmlSystem.GetXmlData();
        // converting xml to json
        return "{ data: some XML data converted to JSON }";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  When to Use:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Integrating legacy code with new systems&lt;/li&gt;
&lt;li&gt;Making incompatible interfaces work together&lt;/li&gt;
&lt;li&gt;Reusing existing classes with incompatible interfaces&lt;/li&gt;
&lt;li&gt;Converting data formats between systems&lt;/li&gt;
&lt;li&gt;Maintaining backward compatibility&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Benefits:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Promotes reusability of existing code&lt;/li&gt;
&lt;li&gt;Provides clean separation of concerns&lt;/li&gt;
&lt;li&gt;Makes incompatible code work together without modifying source&lt;/li&gt;
&lt;li&gt;Follows Single Responsibility Principle&lt;/li&gt;
&lt;li&gt;Enhances maintainability&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Limitations:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Can add complexity to the codebase&lt;/li&gt;
&lt;li&gt;May require creating multiple adapters for different interfaces&lt;/li&gt;
&lt;li&gt;Could impact performance due to additional layer&lt;/li&gt;
&lt;li&gt;Might make debugging more challenging&lt;/li&gt;
&lt;li&gt;Not suitable when extensive interface modifications are needed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The real-world example in the code shows adapting XML data to JSON format. The pattern is particularly useful in scenarios like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database interface adaptations&lt;/li&gt;
&lt;li&gt;Third-party library integration&lt;/li&gt;
&lt;li&gt;API versioning&lt;/li&gt;
&lt;li&gt;File format conversions&lt;/li&gt;
&lt;li&gt;Payment gateway integrations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✨ You can find design pattern codes &lt;a href="https://github.com/MahmudMridul/DesignPatterns" rel="noopener noreferrer"&gt;in this repository&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✨ Exploring Design Patterns Other Posts&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/abdullahal_mahmud/implementing-thread-safe-singleton-pattern-in-c-22b5"&gt;Singleton&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/abdullahal_mahmud/exploring-design-patterns-factory-method-1ink"&gt;Factory Method&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>designpatterns</category>
      <category>csharp</category>
      <category>backend</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>🔎Exploring Design Patterns: Factory Method</title>
      <dc:creator>Abdullah Al Mahmud</dc:creator>
      <pubDate>Fri, 08 Nov 2024 00:52:30 +0000</pubDate>
      <link>https://dev.to/abdullahal_mahmud/exploring-design-patterns-factory-method-1ink</link>
      <guid>https://dev.to/abdullahal_mahmud/exploring-design-patterns-factory-method-1ink</guid>
      <description>&lt;h1&gt;
  
  
  Factory Method
&lt;/h1&gt;

&lt;p&gt;Let's say we need to create two types of documents in your application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PDF&lt;/li&gt;
&lt;li&gt;Word&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So we can simply write the following codes.&lt;br&gt;
First we create a document creator class that creates required document&lt;br&gt;
type based on the parameter passed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class DocumentCreator
{
    public IDocument CreateDocument(string type)
    {
        IDocument doc = null!;
        if (type == "pdf")
        {
            doc = new Pdf();
        }
        else if (type == "word")
        {
            doc = new Word();
        }
        return doc;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here as we can see &lt;code&gt;CreateDocument&lt;/code&gt; return a &lt;code&gt;IDocument&lt;/code&gt; object. because&lt;br&gt;
we are dealing with different return type here. &lt;br&gt;
So, this is how we will have to define our concrete classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface IDocument
{
    void Open();
    void Close();
    void Save();
}

public class Pdf : IDocument
{
    public void Open()
    {
        //logic
    }

    public void Close()
    {
        //logic
    }
    public void Save()
    {
        //logic
    }
}

public class Word : IDocument
{
    public void Open()
    {
        //logic
    }

    public void Close()
    {
        //logic
    }
    public void Save()
    {
        //logic
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally we have our client code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Client
{
    public static void ClientMain()
    {
        DocumentCreator creator = new DocumentCreator();
        Document pdf = creator.CreateDocument("pdf");
        Document word = creator.CreateDocument("word");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks good. But what if our client wants to add a new document type. Let's say &lt;br&gt;
Excel. We will have to add a new class &lt;code&gt;Excel&lt;/code&gt; and modify &lt;code&gt;CreateDocument&lt;/code&gt; function in the &lt;code&gt;DocumentCreator&lt;/code&gt; class like following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class DocumentCreator
    {
        public IDocument CreateDocument(string type)
        {
            IDocument doc = null!;
            if (type == "pdf")
            {
                doc = new Pdf();
            }
            else if (type == "word")
            {
                doc = new Word();
            }
            else if (type == "excel") 
            {
                doc = new Excel();
            }
            return doc;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this breaks the &lt;code&gt;Open Closed&lt;/code&gt; principle. The class DocumentCreator is not&lt;br&gt;
closed for modification. &lt;/p&gt;

&lt;p&gt;This is where &lt;code&gt;Factory Method&lt;/code&gt; pattern comes in to save the day!&lt;br&gt;
First, let's see how we implement this pattern for two types of documents. &lt;/p&gt;

&lt;p&gt;We will define the &lt;code&gt;Document&lt;/code&gt; interface, &lt;code&gt;Pdf&lt;/code&gt; and &lt;code&gt;Word&lt;/code&gt; class just like previous example. No &lt;br&gt;
change here. &lt;/p&gt;

&lt;p&gt;But this time along with concrete classes we will define some concrete factory classes and a common &lt;code&gt;abstract&lt;/code&gt; factory class like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract class DocumentFactory
{
    public abstract IDocument CreateDocument();
}

public class PdfFactory : DocumentFactory
{
    public override IDocument CreateDocument()
    {
        return new PdfDocument();
    }
}

public class WordFactory : DocumentFactory
{
    public override IDocument CreateDocument()
    {
        return new PdfDocument();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is how the client code will use the factories&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Client
{
    public static void ClientMain()
    {
        DocumentFactory pdfFactory = new PdfFactory();
        DocumentFactory wordFactory = new WordFactory();

        IDocument pdf = pdfFactory.CreateDocument();
        IDocument word = wordFactory.CreateDocument();

        pdf.Open();
        word.Open();

        pdf.Close();
        word.Close();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see now we have such a setup that if we need to add a new document type we don't &lt;br&gt;
need to modify any classes. We just create a new class and factory like following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Excel : IDocument
{
    public void Open()
    {
        //logic
    }

    public void Close()
    {
        //logic
    }
    public void Save()
    {
        //logic
    }
}

public class ExcelFactory : DocumentFactory
{
    public override IDocument CreateDocument()
    {
        return new Excel();
    }
}

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

&lt;/div&gt;



&lt;p&gt;That makes our code closed for modification but open for extension. Below are some more details about this desing pattern.&lt;/p&gt;

&lt;p&gt;The Factory Method pattern is a creational pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. Here's when and why you should use it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;When to Use:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you don't know ahead of time what exact types of objects you need to create&lt;/li&gt;
&lt;li&gt;When you want to delegate the responsibility of object creation to subclasses&lt;/li&gt;
&lt;li&gt;When you want to provide a way to extend the application's code with new types without modifying existing code&lt;/li&gt;
&lt;li&gt;When you have a class that needs to create different types of objects based on some condition&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problems it Solves:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decouples object creation from the code that uses the objects&lt;/li&gt;
&lt;li&gt;Makes the code more flexible and extensible&lt;/li&gt;
&lt;li&gt;Follows the Open/Closed Principle (open for extension, closed for modification)&lt;/li&gt;
&lt;li&gt;Eliminates the need for complex conditional logic in object creation&lt;/li&gt;
&lt;li&gt;Provides a way to encapsulate object creation logic&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the example above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;IDocument&lt;/code&gt; is the product interface that defines what a document can do&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Pdf&lt;/code&gt; and &lt;code&gt;Word&lt;/code&gt; are concrete products&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DocumentFactory&lt;/code&gt; is the abstract creator with the factory method&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PdfFactory&lt;/code&gt; and &lt;code&gt;WordFactory&lt;/code&gt; are concrete creators&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Real-world examples where Factory Method is useful:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;UI Framework Components:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract class ButtonFactory
{
    public abstract IButton CreateButton();
}

// Different factories for Windows, Mac, Web buttons
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Database Connections:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract class DbConnectionFactory
{
    public abstract IDbConnection CreateConnection();
}

// Separate factories for SQL Server, Oracle, MySQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Payment Processing:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract class PaymentProcessorFactory
{
    public abstract IPaymentProcessor CreateProcessor();
}

// Different factories for PayPal, Stripe, Square
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Benefits of using Factory Method:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Easy to add new product types without changing existing code&lt;/li&gt;
&lt;li&gt;Single Responsibility Principle - separates product creation code from the product usage code&lt;/li&gt;
&lt;li&gt;Helps manage complexity in applications with multiple product variants&lt;/li&gt;
&lt;li&gt;Makes the code more testable by allowing mock object creation&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>csharp</category>
      <category>designpatterns</category>
      <category>backend</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>🔎Exploring Design Patterns: Thread Safe Singleton Pattern in C#</title>
      <dc:creator>Abdullah Al Mahmud</dc:creator>
      <pubDate>Mon, 28 Oct 2024 10:00:06 +0000</pubDate>
      <link>https://dev.to/abdullahal_mahmud/implementing-thread-safe-singleton-pattern-in-c-22b5</link>
      <guid>https://dev.to/abdullahal_mahmud/implementing-thread-safe-singleton-pattern-in-c-22b5</guid>
      <description>&lt;h2&gt;
  
  
  Singleton (Thread Safe)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private static readonly Lazy&amp;lt;DbConnectionThreadSafe&amp;gt; _instance = null!;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using &lt;code&gt;Lazy&amp;lt;T&amp;gt;&lt;/code&gt; Because
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Thread safe by default&lt;/li&gt;
&lt;li&gt;Provides lazy initialization
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static DbConnectionThreadSafe()
{
    _instance = new Lazy&amp;lt;DbConnectionThreadSafe&amp;gt;(
        () =&amp;gt; new DbConnectionThreadSafe(_connectionString),
        LazyThreadSafetyMode.ExecutionAndPublication
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down this static constructor and the Lazy initialization:&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;static&lt;/span&gt; &lt;span class="nf"&gt;DbConnection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;// This is a static constructor&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_lazyInstance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Lazy&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;DbConnection&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DbConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_connectionString&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;  &lt;span class="c1"&gt;// Lambda expression&lt;/span&gt;
        &lt;span class="n"&gt;LazyThreadSafetyMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExecutionAndPublication&lt;/span&gt;  &lt;span class="c1"&gt;// Thread safety mode&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;Let's examine each part:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Static Constructor&lt;/strong&gt; (&lt;code&gt;static DbConnection()&lt;/code&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A static constructor is called automatically before any static members are referenced&lt;/li&gt;
&lt;li&gt;It runs only once per app domain&lt;/li&gt;
&lt;li&gt;It's thread-safe by default (CLR guarantees this)&lt;/li&gt;
&lt;li&gt;Used to initialize static fields&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lazy&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Lazy&amp;lt;T&amp;gt;&lt;/code&gt; is a class that provides lazy initialization&lt;/li&gt;
&lt;li&gt;The actual object isn't created until it's first accessed&lt;/li&gt;
&lt;li&gt;When you access &lt;code&gt;Lazy&amp;lt;T&amp;gt;.Value&lt;/code&gt; for the first time, it creates the instance&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lambda Expression&lt;/strong&gt; (&lt;code&gt;() =&amp;gt; new DbConnection(_connectionString)&lt;/code&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is a factory function that creates the DbConnection instance&lt;/li&gt;
&lt;li&gt;It's only executed when the Lazy.Value is first accessed&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;=&amp;gt;&lt;/code&gt; syntax defines a lambda expression (anonymous function)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;LazyThreadSafetyMode.ExecutionAndPublication&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This enum value specifies how the Lazy instance handles thread safety&lt;/li&gt;
&lt;li&gt;Three possible modes:
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// No thread safety
LazyThreadSafetyMode.None

// Locks initialization
LazyThreadSafetyMode.ExecutionAndPublication

// Multiple threads can attempt initialization
LazyThreadSafetyMode.PublicationOnly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The execution flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the class is first referenced, the static constructor runs&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;_lazyInstance&lt;/code&gt; is created, but the DbConnection instance is not yet created&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;Instance&lt;/code&gt; is first accessed:

&lt;ul&gt;
&lt;li&gt;The lambda expression runs&lt;/li&gt;
&lt;li&gt;Creates new DbConnection with the connection string&lt;/li&gt;
&lt;li&gt;Stores it in the Lazy instance&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Subsequent accesses to &lt;code&gt;Instance&lt;/code&gt; return the same stored instance&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>designpatterns</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
