<?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: Dhanasai Tholeti</title>
    <description>The latest articles on DEV Community by Dhanasai Tholeti (@dhanasai_tholeti).</description>
    <link>https://dev.to/dhanasai_tholeti</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%2F1105723%2Ffffaa5a2-fb1f-4242-a704-437ddede429b.jpg</url>
      <title>DEV Community: Dhanasai Tholeti</title>
      <link>https://dev.to/dhanasai_tholeti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhanasai_tholeti"/>
    <language>en</language>
    <item>
      <title>Mastering the Strategy Pattern with a Car Example 🚗⚙️</title>
      <dc:creator>Dhanasai Tholeti</dc:creator>
      <pubDate>Thu, 13 Feb 2025 15:48:23 +0000</pubDate>
      <link>https://dev.to/dhanasai_tholeti/mastering-the-strategy-pattern-with-a-car-example-39ko</link>
      <guid>https://dev.to/dhanasai_tholeti/mastering-the-strategy-pattern-with-a-car-example-39ko</guid>
      <description>&lt;p&gt;In the world of software design, flexibility and maintainability are crucial. One of the best ways to achieve this is through the &lt;strong&gt;Strategy Pattern&lt;/strong&gt;—a behavioral design pattern that allows us to define a family of algorithms, encapsulate each one, and make them interchangeable.&lt;/p&gt;

&lt;p&gt;Let’s break it down using a &lt;strong&gt;car example&lt;/strong&gt;! 🚘  &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is the Strategy Pattern?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Strategy Pattern&lt;/strong&gt; enables an object’s behavior to be selected at runtime. Instead of hardcoding logic inside an object, we delegate it to separate strategy classes that encapsulate different behaviors. This promotes the &lt;strong&gt;Open-Closed Principle&lt;/strong&gt;—where code is open for extension but closed for modification.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Real-World Example: Cars and Their Behaviors 🚗⚡️⛽&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Consider a car that can have different &lt;strong&gt;transmission types&lt;/strong&gt; (manual, automatic, semi-automatic) and &lt;strong&gt;fuel types&lt;/strong&gt; (gasoline, diesel, electric). Instead of tightly coupling the car’s logic to these behaviors, we use the &lt;strong&gt;Strategy Pattern&lt;/strong&gt; to define separate classes for each.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;🔹 Implementing Transmission Strategies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Each transmission type is an independent class that implements the &lt;code&gt;TransmissionStrategy&lt;/code&gt; interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;TransmissionStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ManualTransmission&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;TransmissionStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Changing gear manually.&lt;/span&gt;&lt;span class="dl"&gt;"&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;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AutomaticTransmission&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;TransmissionStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Changing gear automatically.&lt;/span&gt;&lt;span class="dl"&gt;"&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;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SemiAutomaticTransmission&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;TransmissionStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Changing gear semi-automatically.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;🔹 Implementing Fuel Strategies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Similarly, different fuel types adhere to a &lt;code&gt;FuelStrategy&lt;/code&gt; interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FuelStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;refuel&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Gasoline&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;FuelStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;refuel&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Refueling gasoline...&lt;/span&gt;&lt;span class="dl"&gt;"&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;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Diesel&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;FuelStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;refuel&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Refueling diesel...&lt;/span&gt;&lt;span class="dl"&gt;"&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;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Electric&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;FuelStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;refuel&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charging the battery...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;🔹 The Car Class Using Strategies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Car&lt;/code&gt; class now &lt;strong&gt;delegates&lt;/strong&gt; behavior to the appropriate strategy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TransmissionStrategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FuelStrategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TransmissionStrategy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FuelStrategy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;transmission&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fuel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;setTransmission&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TransmissionStrategy&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;transmission&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;setFuel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FuelStrategy&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fuel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;refuel&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fuel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refuel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;changeGear&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;
  
  
  &lt;strong&gt;Why Use the Strategy Pattern? 🤔&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;✅ &lt;strong&gt;Flexibility&lt;/strong&gt;: We can change the car’s transmission and fuel type dynamically at runtime.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Encapsulation&lt;/strong&gt;: Each behavior is encapsulated separately, making it easy to add new strategies.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Maintainability&lt;/strong&gt;: Avoids complex if-else conditions, making the code clean and modular.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Reusability&lt;/strong&gt;: Strategies can be reused across different contexts without modifications.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Example Usage 🚀&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now, let’s create different cars and modify their behaviors at runtime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;manualCar&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;Car&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;ManualTransmission&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;Gasoline&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;manualCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: Changing gear manually.&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;automaticCar&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;Car&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;AutomaticTransmission&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;Electric&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;automaticCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: Changing gear automatically.&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;semiAutomaticCar&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;Car&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;SemiAutomaticTransmission&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;Diesel&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;semiAutomaticCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: Changing gear semi-automatically.&lt;/span&gt;

&lt;span class="nx"&gt;manualCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setTransmission&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;AutomaticTransmission&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Switching to automatic&lt;/span&gt;
&lt;span class="nx"&gt;semiAutomaticCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setFuel&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;Electric&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Switching to electric&lt;/span&gt;

&lt;span class="nx"&gt;manualCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;changeGear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: Changing gear automatically.&lt;/span&gt;
&lt;span class="nx"&gt;semiAutomaticCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refuel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: Charging the battery...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Final Thoughts 💡&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Strategy Pattern&lt;/strong&gt; is a powerful tool for designing flexible and reusable software. By decoupling behaviors into independent strategies, we make our applications &lt;strong&gt;more adaptable to change&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;If you found this insightful, drop a &lt;strong&gt;like&lt;/strong&gt; and &lt;strong&gt;share&lt;/strong&gt; this with your network! 🚀💬  &lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>designpatterns</category>
      <category>codequality</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>A Game-Changing Next.js Feature That Will Make Your Life Easier! 🚀</title>
      <dc:creator>Dhanasai Tholeti</dc:creator>
      <pubDate>Mon, 10 Feb 2025 11:57:48 +0000</pubDate>
      <link>https://dev.to/dhanasai_tholeti/a-game-changing-nextjs-feature-that-will-make-your-life-easier-3gco</link>
      <guid>https://dev.to/dhanasai_tholeti/a-game-changing-nextjs-feature-that-will-make-your-life-easier-3gco</guid>
      <description>&lt;p&gt;As frontend developers, we all know the struggle:  &lt;/p&gt;

&lt;p&gt;You add &lt;strong&gt;tons of &lt;code&gt;console.log&lt;/code&gt; statements&lt;/strong&gt; while debugging, making sure your data is just right. Then comes the tedious part—&lt;strong&gt;removing them one by one before deploying to production&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;But what if I told you there’s an easier way?  &lt;/p&gt;

&lt;h3&gt;
  
  
  Meet Next.js’s Hidden Gem: Automatic Console Log Removal
&lt;/h3&gt;

&lt;p&gt;I recently discovered a &lt;strong&gt;cool Next.js config setting&lt;/strong&gt; that removes all &lt;code&gt;console.log&lt;/code&gt; statements from your production build automatically—without any manual effort!  &lt;/p&gt;

&lt;p&gt;Here’s how it works:  &lt;/p&gt;

&lt;h4&gt;
  
  
  🔹 Remove All &lt;code&gt;console.log&lt;/code&gt;s Effortlessly
&lt;/h4&gt;

&lt;p&gt;Simply add this snippet to your &lt;strong&gt;&lt;code&gt;next.config.js&lt;/code&gt;&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;restOfTheConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;compiler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;removeConsole&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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 will &lt;strong&gt;strip out all console logs&lt;/strong&gt; from your production build, keeping your codebase cleaner and optimized!  &lt;/p&gt;

&lt;h4&gt;
  
  
  🔹 Want to Keep Error Logs? No Problem!
&lt;/h4&gt;

&lt;p&gt;If you still want to &lt;strong&gt;retain error logs&lt;/strong&gt; while removing everything else, tweak the config like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;restOfTheConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;compiler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;removeConsole&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setting, only error logs will be preserved while other logs are removed—making debugging easier without cluttering your production code.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Why This Matters?
&lt;/h3&gt;

&lt;p&gt;✅ &lt;strong&gt;No more manual cleanup&lt;/strong&gt; of &lt;code&gt;console.log&lt;/code&gt;s before deployment&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Better performance&lt;/strong&gt; by reducing unnecessary logs&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Cleaner production code&lt;/strong&gt; while still keeping error tracking intact  &lt;/p&gt;

&lt;p&gt;This small yet powerful Next.js feature &lt;strong&gt;streamlines your workflow&lt;/strong&gt; and saves you time. If you found this helpful, drop a ❤️ and share it with fellow developers!  &lt;/p&gt;

&lt;p&gt;Have you used this feature before? Let me know in the comments! 👇&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>frontend</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>🚀 Git Cherry-Pick: The Hidden Gem You Need to Know! 🚀</title>
      <dc:creator>Dhanasai Tholeti</dc:creator>
      <pubDate>Mon, 03 Feb 2025 14:39:57 +0000</pubDate>
      <link>https://dev.to/dhanasai_tholeti/git-cherry-pick-the-hidden-gem-you-need-to-know-427h</link>
      <guid>https://dev.to/dhanasai_tholeti/git-cherry-pick-the-hidden-gem-you-need-to-know-427h</guid>
      <description>&lt;p&gt;Last Friday, I made a small typo fix and accidentally pushed it straight to production instead of the testing environment. Since it was already live, I had to merge the change back into testing.  &lt;/p&gt;

&lt;p&gt;At first, I thought of pulling production into testing—but that felt unnecessary. Then, while digging through Git’s documentation, I found a &lt;strong&gt;game-changing&lt;/strong&gt; command: &lt;code&gt;git cherry-pick&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;🍒 &lt;strong&gt;What is Git Cherry-Pick?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
It lets you &lt;strong&gt;pick and apply specific commits&lt;/strong&gt; from one branch to another &lt;em&gt;without&lt;/em&gt; pulling or rebasing. Neat, right?  &lt;/p&gt;

&lt;p&gt;Here’s a quick guide on how to use it:  &lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Scenario&lt;/strong&gt;: You have two branches, A (target) and B (source), and you need a specific commit from B in A.  &lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Steps&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
1️⃣ Checkout your target branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git checkout A
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2️⃣ View commit history of source branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git log B &lt;span class="nt"&gt;--oneline&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3️⃣ Copy the commit hash you want to merge.  &lt;/p&gt;

&lt;p&gt;4️⃣ Apply the commit to your target branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git cherry-pick &amp;lt;commit-hash&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5️⃣ Want multiple commits? No problem!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git cherry-pick &amp;lt;commit-hash1&amp;gt; &amp;lt;commit-hash2&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6️⃣ If conflicts occur:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="c"&gt;# Resolve conflicts&lt;/span&gt;
   git add &lt;span class="nb"&gt;.&lt;/span&gt;
   git cherry-pick &lt;span class="nt"&gt;--continue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And just like that, &lt;strong&gt;you've merged the changes you needed without messing up the Git history!&lt;/strong&gt; 😎  &lt;/p&gt;

&lt;p&gt;💡 Have you used &lt;code&gt;git cherry-pick&lt;/code&gt; before? Share your experience below! 👇  &lt;/p&gt;

</description>
      <category>git</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Comprehensive Guide to Structuring Your React App Project</title>
      <dc:creator>Dhanasai Tholeti</dc:creator>
      <pubDate>Sun, 31 Dec 2023 01:55:51 +0000</pubDate>
      <link>https://dev.to/dhanasai_tholeti/a-comprehensive-guide-to-structuring-your-react-app-project-2a9d</link>
      <guid>https://dev.to/dhanasai_tholeti/a-comprehensive-guide-to-structuring-your-react-app-project-2a9d</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Structuring a React app project is crucial for maintaining code organization, scalability, and modularity. By dividing your app into multiple folders and files, you can easily navigate and manage different components, styles, utilities, and other resources. In this article, we will explore the benefits of structuring your React app and provide a step-by-step guide on how to effectively structure your project for optimal development experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Structuring Your React App
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code Organization and Readability
&lt;/h3&gt;

&lt;p&gt;A well-structured project allows developers to easily locate and understand different parts of the codebase. By dividing your app into meaningful folders and files, you can create a logical hierarchy that reflects the app's architecture. This makes it easier for both you and your team to navigate, maintain, and update the codebase, resulting in improved code readability and organization.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modularity and Reusability
&lt;/h3&gt;

&lt;p&gt;Structuring your React app promotes modularity and reusability. By breaking down your app into smaller components, you can create self-contained and reusable pieces of code. This allows you to easily reuse components across different parts of your app or even in future projects, saving time and effort in development.&lt;br&gt;
Scalability and Collaboration&lt;/p&gt;

&lt;p&gt;As your React app grows, maintaining scalability becomes essential. A well-structured project provides a solid foundation for scaling your app without introducing complexity. It allows for easy integration of new features, reduces the risk of code conflicts during collaboration, and facilitates efficient teamwork among developers.&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%2F9lsgd0lpg6xu5aaac3d8.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%2F9lsgd0lpg6xu5aaac3d8.png" alt="Image description" width="320" height="743"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Individual Files:
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Main.tsx
&lt;/h3&gt;

&lt;p&gt;The Main.tsx file serves as the entry point for your React application. Its primary function is to render the App component within the index.html file. This file acts as the starting point for your application's component tree.&lt;/p&gt;
&lt;h3&gt;
  
  
  App.tsx
&lt;/h3&gt;

&lt;p&gt;The App.tsx file is responsible for rendering the entire application's routes along with their respective layouts. It serves as the central point for routing and composition of different pages and components.&lt;/p&gt;
&lt;h3&gt;
  
  
  Layout.tsx
&lt;/h3&gt;

&lt;p&gt;The Layout.tsx file is responsible for rendering shared components, such as a navbar, that need to be displayed across multiple pages or components within your application. It helps maintain consistency in the overall design and layout.&lt;/p&gt;
&lt;h3&gt;
  
  
  index.css
&lt;/h3&gt;

&lt;p&gt;The index.css file is where the root styles of your application are stored. If you are using a CSS framework like Tailwind CSS, separate CSS files may not be necessary. However, if you choose to use custom CSS, you can define the global styles in this file.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Folder Structure:
&lt;/h2&gt;
&lt;h3&gt;
  
  
  assets folder
&lt;/h3&gt;

&lt;p&gt;The assets folder is used to store static files such as fonts, images, or any other external resources required by your application. Keeping them separate from the source code allows for easier management and scalability.&lt;/p&gt;
&lt;h3&gt;
  
  
  components folder
&lt;/h3&gt;

&lt;p&gt;The components folder is where reusable components are stored. It is recommended to create a separate folder for each component if you are using CSS, SCSS, or styled components. This folder should contain an index.tsx file for rendering the JSX and an index.css file for styling the component. If you are using css-in-js then you don't have to create a separate directory for each component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;components/
├── Component1/
│   ├── index.tsx
│   └── index.css
└── Chat/
    ├── ChatRender.tsx
    └── ChatInput.tsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally, it is beneficial to group related components into subfolders within the components directory. For example, if you have a chat UI, you can create a separate folder called Chat and group all chat-related components within it.&lt;/p&gt;

&lt;h3&gt;
  
  
  configs folder
&lt;/h3&gt;

&lt;p&gt;The configs folder is used to store application configurations. If you have multiple APIs or backend services, you can store their configurations in separate files and export them for use throughout the application. This allows for easy management and modification of configuration settings.&lt;/p&gt;

&lt;h3&gt;
  
  
  hooks folder
&lt;/h3&gt;

&lt;p&gt;The hooks folder is where reusable custom hooks live. Hooks are abstracted functions that encapsulate common functionality and can be shared across different components. To differentiate them from regular TypeScript files, you can consider using the .hook.ts extension.&lt;/p&gt;

&lt;h3&gt;
  
  
  libs/utils folder
&lt;/h3&gt;

&lt;p&gt;The libs or utils folder is a handy directory that houses helper functions, types, and any other utility files. It is recommended to organize this folder by creating subdirectories for data, types, and other relevant categories.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;libs/
├── Data/
│   └── dummyData.json
└── Types/
    ├── user.type.ts
    └── device.type.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This separation allows for easy accessibility and organization of external data, such as dummy data, and reusable types.&lt;/p&gt;

&lt;h3&gt;
  
  
  pages folder
&lt;/h3&gt;

&lt;p&gt;The pages folder is where all the routable pages or screens of your application reside. Each page is typically composed of one or more components, representing a specific view or functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pages/
├── LoginScreen.tsx
├── SignupScreen.tsx
├── HomeScreen.tsx
└── NotFoundScreen.tsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  routes folder
&lt;/h3&gt;

&lt;p&gt;The routes folder is where you can manage and define the routes of your application. You can implement route guards or any other operations related to routing within this directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  services folder
&lt;/h3&gt;

&lt;p&gt;The services folder is used for integrating external services, such as state management with Redux or WebSocket connections. It is recommended to organize services into subdirectories based on functionality or type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services/
├── state/
│   ├── slices/
│   └── store.ts
└── websockets/
    └── socketManager.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This folder structure allows for better separation and organization of different services used within your application.&lt;/p&gt;

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

&lt;p&gt;No matter the size of your project, it is crucial to adhere to the aforementioned folder structure in order to ensure code reusability, maintainability, and collaboration. Remember, it is essential to customize the folder structure to suit your specific requirements.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
