{"id":359,"date":"2026-06-29T18:56:15","date_gmt":"2026-06-29T18:56:15","guid":{"rendered":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/?p=359"},"modified":"2026-06-29T20:57:01","modified_gmt":"2026-06-29T20:57:01","slug":"strategy-when-behavior-needs-to-be-flexible","status":"publish","type":"post","link":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/2026\/06\/29\/strategy-when-behavior-needs-to-be-flexible\/","title":{"rendered":"Strategy &#8211; When Behavior Needs to Be Flexible"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/wp-content\/uploads\/2026\/06\/Designer-2-1024x683.png\" alt=\"\" class=\"wp-image-400\" srcset=\"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/wp-content\/uploads\/2026\/06\/Designer-2-1024x683.png 1024w, https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/wp-content\/uploads\/2026\/06\/Designer-2-300x200.png 300w, https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/wp-content\/uploads\/2026\/06\/Designer-2-768x512.png 768w, https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/wp-content\/uploads\/2026\/06\/Designer-2.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In many applications, you don\u2019t just create objects \u2014 you also need to define <strong>how they behave<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And that behavior can change.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Discount calculation, payment processing, sorting logic, validation rules\u2026<br>Hardcoding all of this quickly turns your code into a jungle of <code>if\/else<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s where the <strong>Strategy pattern<\/strong> comes in.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Problem: Too Many Conditionals<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s say you&#8217;re implementing a discount system:<\/p>\n\n\n<p><!--ScriptorStartFragment--><\/p>\n<pre style=\"padding-left: 80px;\">public class DiscountService {<br \/><br \/>\u00a0\u00a0\u00a0 public double applyDiscount(double amount, String type) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (type.equals(\"regular\")) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return amount * 0.9;<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } else if (type.equals(\"premium\")) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return amount * 0.8;<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } else if (type.equals(\"vip\")) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return amount * 0.7;<br \/><br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } else {<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return amount;<br \/> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/> \u00a0\u00a0 }<br \/>}<!--ScriptorEndFragment--><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Problems with this are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Every new strategy means modifying this method<\/li>\n\n\n\n<li>Violates Open\/Closed Principle<\/li>\n\n\n\n<li>Hard to test each variation independently<\/li>\n\n\n\n<li>Grows into a maintenance nightmare<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">The Solution: Strategy<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Extract each behavior into its own class and make them interchangeable. Strategy implementation example would be:<\/p>\n\n\n<p><!--ScriptorStartFragment--><\/p>\n<pre style=\"padding-left: 80px;\">public interface DiscountStrategy {<br \/><br \/>\u00a0\u00a0\u00a0 double apply(double amount);<br \/><br \/>}<!--ScriptorEndFragment--><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Then the next step:<\/p>\n\n\n<p style=\"padding-left: 80px;\"><!--ScriptorStartFragment--><\/p>\n<pre class=\"scriptor-paragraph\" style=\"padding-left: 80px;\">public class RegularDiscount implements DiscountStrategy {<br \/><br \/>\u00a0\u00a0\u00a0 public double apply(double amount) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return amount * 0.9;<br \/><br \/>\u00a0\u00a0\u00a0 }<br \/><br \/>}<br \/><br \/>public class PremiumDiscount implements DiscountStrategy {<br \/><br \/>\u00a0\u00a0\u00a0 public double apply(double amount) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return amount * 0.8;<br \/><br \/>\u00a0\u00a0\u00a0 }<br \/><br \/>}<br \/><br \/>public class VipDiscount implements DiscountStrategy {<br \/><br \/>\u00a0\u00a0\u00a0 public double apply(double amount) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return amount * 0.7;<br \/><br \/>\u00a0\u00a0\u00a0 }<br \/><br \/>}<!--ScriptorEndFragment--><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The next step:<\/p>\n\n\n<p><!--ScriptorStartFragment--><\/p>\n<pre class=\"scriptor-paragraph\" style=\"padding-left: 80px;\">public class DiscountService {<br \/><br \/>\u00a0\u00a0\u00a0 private final DiscountStrategy strategy;<br \/><br \/><br class=\"scriptor-paragraph\" \/><br \/><br \/>\u00a0\u00a0\u00a0 public DiscountService(DiscountStrategy strategy) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 this.strategy = strategy;<br \/><br \/>\u00a0\u00a0\u00a0 }<br \/><br \/><br class=\"scriptor-paragraph\" \/><br \/><br \/>\u00a0\u00a0\u00a0 public double applyDiscount(double amount) {<br \/><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return strategy.apply(amount);<br \/><br \/>\u00a0\u00a0\u00a0 }<br \/><br \/><br class=\"scriptor-paragraph\" \/><br \/><br \/>}<!--ScriptorEndFragment--><\/pre>\n\n\n<p class=\"wp-block-paragraph\">And the Usage of Strategy woul be:<\/p>\n\n\n<p>DiscountStrategy strategy = new PremiumDiscount();<br \/>DiscountService service = new DiscountService(strategy);<\/p>\n<p>double result = service.applyDiscount(100);<\/p>\n\n\n<p class=\"wp-block-paragraph\">No conditionals. Just behavior injection.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">When Should You Use Strategy?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Strategy is perfect when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You have <strong>multiple ways to perform an operation<\/strong><\/li>\n\n\n\n<li>You want to <strong>switch behavior at runtime<\/strong><\/li>\n\n\n\n<li>You want to avoid <strong>if\/else or switch blocks<\/strong><\/li>\n\n\n\n<li>You need to <strong>isolate algorithms<\/strong><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">When NOT to Use Strategy<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid Strategy when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You only have <strong>one or two simple variations<\/strong><\/li>\n\n\n\n<li>Behavior rarely changes<\/li>\n\n\n\n<li>You don\u2019t need runtime flexibility<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In many applications, you don\u2019t just create objects \u2014 you also need to define how they behave. And that behavior can change. Discount calculation, payment processing, sorting logic, validation rules\u2026Hardcoding all of this quickly turns your code into a jungle of if\/else. That\u2019s where the Strategy pattern comes in. The Problem: Too Many Conditionals Let\u2019s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-pp"],"_links":{"self":[{"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":6,"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"predecessor-version":[{"id":401,"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/posts\/359\/revisions\/401"}],"wp:attachment":[{"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/patryk-sobilo.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}