Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • Software and Tools
    • School Learning
    • Practice Coding Problems
  • Go Premium
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App

Strategy Method | JavaScript Design Pattern

Last Updated : 08 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Strategy Method or Strategy Pattern in JavaScript helps solve the problem of needing to use different methods or behaviors in your code and easily switch between them. Strategy Method is a behavioral design pattern in JavaScript that defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows the client to choose an algorithm from a family of algorithms at runtime, without altering the code that uses these algorithms.

Important Topics for the Strategy Method Design Pattern in Javascript Design Patterns

  • Example of Strategy Method in JavaScript Design Patterns
  • Use Cases of Strategy Method in JavaScript Design Patterns
  • Advantages of the Strategy Method in JavaScript Design Patterns
  • Disadvantages of the Strategy Method in JavaScript Design Patterns
  • Conclusion

Example of Strategy Method in JavaScript Design Patterns

The problem statement is essentially to create a flexible and extensible discount system for a shopping cart, allowing for different discount strategies to be easily added and applied to different shopping carts. This pattern is useful for scenarios where you want to decouple the discount logic from the shopping cart and make it easy to change or extend the discount strategies.

Key Component of Strategy Method:

  • Context: This is the class or component that uses the Strategy. It maintains a reference to a Strategy object and can switch between different strategies at runtime.
  • Strategy Interface: An interface or an abstract class that defines a set of methods that concrete strategies must implement.
  • Concrete Strategies: These are the individual implementations of the Strategy Interface. They encapsulate specific algorithmic behaviors.

Step-by-step Implementation of the above Problem:

Strategy Interface: DiscountStrategy is an abstract class with a method calculateDiscount(orderTotal) that needs to be implemented by concrete discount strategies.

JavaScript
// Strategy Interface
class DiscountStrategy {
  calculateDiscount(orderTotal) {
    // To be implemented by concrete strategies
  }
}

Concrete Strategies: There are three concrete discount strategies implemented in the code:

  • NoDiscount: Provides no discount (returns 0).
  • TenPercentDiscount: Applies a 10% discount on the order total.
  • TwentyPercentDiscount: Applies a 20% discount on the order total.
JavaScript
// Concrete Strategies
class NoDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return 0;
  }
}
class TenPercentDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return orderTotal * 0.1;
  }
}

class TwentyPercentDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return orderTotal * 0.2;
  }
}

  


Context: The ShoppingCart class represents the shopping cart and takes a discount strategy as a parameter during initialization. It allows items to be added to the cart and calculates the total order price, taking into account the selected discount strategy.

JavaScript
class ShoppingCart {
  constructor(discountStrategy) {
    this.discountStrategy = discountStrategy;
    this.items = [];
  }

  addItem(item) {
    this.items.push(item);
  }

  calculateTotal() {
    const orderTotal = this.items.reduce((total, item) => total + item.price, 0);
    return orderTotal - this.discountStrategy.calculateDiscount(orderTotal);
  }
}


Example Usage: Three different shopping carts (cart1, cart2, and cart3) are created with different discount strategies and items. The code calculates and prints the total price for each cart after applying the selected discount strategy.

JavaScript
// Example usage
const noDiscount = new NoDiscount();
const tenPercentDiscount = new TenPercentDiscount();
const twentyPercentDiscount = new TwentyPercentDiscount();

const cart1 = new ShoppingCart(noDiscount);
cart1.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 1 Total:', cart1.calculateTotal());

const cart2 = new ShoppingCart(tenPercentDiscount);
cart2.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 2 Total:', cart2.calculateTotal());

const cart3 = new ShoppingCart(twentyPercentDiscount);
cart3.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 3 Total:', cart3.calculateTotal());

Code Implementation of the above Problem:

JavaScript
// Strategy Interface
class DiscountStrategy {
  calculateDiscount(orderTotal) {
    // To be implemented by concrete strategies
  }
}

// Concrete Strategies
class NoDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return 0;
  }
}

class TenPercentDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return orderTotal * 0.1;
  }
}

class TwentyPercentDiscount extends DiscountStrategy {
  calculateDiscount(orderTotal) {
    return orderTotal * 0.2;
  }
}

// Context
class ShoppingCart {
  constructor(discountStrategy) {
    this.discountStrategy = discountStrategy;
    this.items = [];
  }

  addItem(item) {
    this.items.push(item);
  }

  calculateTotal() {
    const orderTotal = this.items.reduce((total, item) => total + item.price, 0);
    return orderTotal - this.discountStrategy.calculateDiscount(orderTotal);
  }
}

// Example usage
const noDiscount = new NoDiscount();
const tenPercentDiscount = new TenPercentDiscount();
const twentyPercentDiscount = new TwentyPercentDiscount();

const cart1 = new ShoppingCart(noDiscount);
cart1.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 1 Total:', cart1.calculateTotal());

const cart2 = new ShoppingCart(tenPercentDiscount);
cart2.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 2 Total:', cart2.calculateTotal());

const cart3 = new ShoppingCart(twentyPercentDiscount);
cart3.addItem({ name: 'Item 1', price: 50 });
console.log('Cart 3 Total:', cart3.calculateTotal());

Output
Cart 1 Total: 50
Cart 2 Total: 45
Cart 3 Total: 40

Diagrammatic explanation of the Above Example

Untitled-(3)

Lets understand the Diagram

  1. Context: This represents the class that contains a reference to the Strategy interface. It is responsible for configuring and using the strategies. The "Context" class has a private instance variable called "strategy" that refers to the Strategy interface. It also contains a method "execute()" which delegates the work to the strategy object.
  2. Strategy: This is an interface or an abstract class that defines the method "algorithm()". It represents the common interface for all supported algorithms. Concrete strategy classes implement this interface.
  3. ConcreteStrategy1, ConcreteStrategy2, ...: These are the concrete classes that implement the Strategy interface. Each of these concrete classes encapsulates a specific algorithm. For instance, "ConcreteStrategy1" and "ConcreteStrategy2" represent two different algorithms. They both implement the "algorithm()" method defined in the Strategy interface.

The arrows in the diagram illustrate the relationships between the classes.

  • The association between "Context" and "Strategy" shows that the "Context" class has a reference to the Strategy interface.
  • The arrows pointing from the "Context" to the "ConcreteStrategy" classes indicate that the "Context" can use any of the concrete strategies interchangeably by switching the current strategy.
  • The "execute()" method in the "Context" class is the entry point for using the strategies.

Overall, this structure allows the "Context" class to utilize different algorithms without being dependent on their specific implementations. It enables the client to switch between different algorithms easily at runtime, promoting flexibility and maintainability.

Use Cases of Strategy Method in JavaScript Design Patterns

  • Algorithm Selection: It allows you to choose an algorithm at runtime, so that system can employ various algorithms interchangeably depending on specific needs. Sorting, searching, and other algorithmic operations frequently use this.
  • Behavioral Variations: The Strategy pattern offers a tidy method to encapsulate each algorithm and switch them out as needed when you have a collection of related behaviours or algorithms that must change independently. Applications where various behaviours must be implemented based on specific conditions may find this helpful.
  • Complex Decision-making Logic: It helps in avoiding complex conditional statements by encapsulating each condition or decision-making logic into its own strategy. This simplifies the code, making it more maintainable and easier to understand.
  • Dynamic Runtime Behavior: The Strategy pattern allows you to change the behavior of an object dynamically at runtime, providing flexibility and adaptability to the system.

Advantages of the Strategy Method in JavaScript Design Patterns

  • Flexibility: It allows for easy addition or modification of strategies without altering the client code.
  • Isolation: We can isolate the specific implementation details of the algorithms from the client’s code.
  • Reusability: Strategies can be reused across different contexts.
  • Clean Code: Promotes a clean and modular design by separating the algorithm from the context.
  • Maintainability: Encapsulating each algorithm or behavior within its own class, makes it easier to modify, test, and extend the codebase without affecting other parts of the code.
  • Easy Testing: With the Strategy pattern, it becomes easier to test individual algorithms independently of the context.
  • Run-time Switching: It is possible that application can switch the strategies at the run-time.

Disadvantages of the Strategy Method in JavaScript Design Patterns

  • Increased Number of Classes: If there are many strategies, it can lead to a larger number of classes, which can be hard to manage.
  • Creating Extra Objects: In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.
  • Awareness among clients: Difference between the strategies should be clear among the clients to able to select a best one for them.
  • Complexity: Implementing the Strategy pattern can introduce additional classes and complexity into your code. Each strategy has its own class, and managing these classes can become complex for large systems.
  • Increased Code Size: The additional classes and interfaces introduced by the Strategy pattern can lead to an increase in code size.

Conclusion

The Strategy Method is a powerful design pattern that facilitates dynamic algorithm selection at runtime, ensuring code flexibility and reusability. By separating algorithms from clients, it encourages clean, maintainable code that aligns with the Open/Closed Principle. Despite its benefits, it's important to manage the potential complexity and increased class count, which may impact system architecture. Overall, the Strategy Method is valuable for encapsulating various behaviors and enabling algorithm interchangeability without client code modification, making it an effective approach for robust software development.


R

rudra1807raj
Improve
Article Tags :
  • Design Pattern
  • Geeks Premier League
  • System Design
  • Geeks Premier League 2023
  • JavaScript Design Patterns

Similar Reads

    JavaScript Design Patterns Tutorial
    Design patterns in Javascipt are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best
    7 min read

    Creational Software Design Patterns in JavaScript

    Builder Method | JavaScript Design Pattern
    The Builder design pattern is a creational design pattern used to construct complex objects by separating the construction process from the actual representation. It's especially useful when an object requires multiple steps or configurations to be created. Important Topics for the Builder Design Pa
    9 min read
    Prototype Method - JavaScript Design Pattern
    A Prototype Method is a JavaScript design pattern where objects share a common prototype object, which contains shared methods. The prototype method allows you to reuse the properties and methods of the prototype object, and also add new ones as needed. The prototype method is useful for performance
    3 min read
    Abstract Factory Pattern | JavaScript Design Patterns
    Abstract Factory Pattern is to abstract the process of object creation by defining a family of related factory methods, each responsible for creating a different type of object. These factory methods are organized within an abstract factory interface or class, and the client code uses this interface
    6 min read

    Behavioural Software Design Patterns in JavaScript

    Template Method | JavaScript Design Patterns
    Template Method is a behavioral design pattern that defines the skeleton of an algorithm in a base class while allowing subclasses to implement specific steps of the algorithm without changing its structure. It promotes code reusability and provides a way to enforce a consistent algorithm structure
    10 min read
    State Method Design Patterns in JavaScript
    State method or State Design Patterns is a pattern that allows an object to alter its behavior when internal state changes occur. This pattern is used when an object wants to change its state dynamically. When we want to change behavior of object it internally uses if-else block to perform actions.
    4 min read
    Iterator Method | JavaScript Design Pattern
    Iterator design pattern is a behavioral design pattern that provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It separates the responsibility of accessing and traversing the elements from the aggregate object. This pattern is wi
    4 min read
    Strategy Method | JavaScript Design Pattern
    Strategy Method or Strategy Pattern in JavaScript helps solve the problem of needing to use different methods or behaviors in your code and easily switch between them. Strategy Method is a behavioral design pattern in JavaScript that defines a family of algorithms, encapsulates each one, and makes t
    8 min read
`; $(commentSectionTemplate).insertBefore(".article--recommended"); } loadComments(); }); }); function loadComments() { if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences