MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Strategy & State Patterns0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
9.1Singleton Pattern & Module Pattern9.2Factory & Builder Patterns9.3Observer & Pub/Sub Pattern9.4Strategy & State Patterns9.5Proxy & Decorator Patterns
HomeTutorialsJavaScript for AdvancedStrategy & State Patterns
Proxy & Decorator Patterns

Strategy & State Patterns

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

As business logic expands, conditional statements (if...else if...else or massive switch blocks) become unmaintainable anti-patterns. The Strategy Pattern and the State Pattern replace conditional branching with polymorphic object delegation.


1. The Strategy Pattern

The Strategy Pattern defines a family of interchangeable algorithms, encapsulates each one into a separate object, and makes them swappable at runtime.

The Problem: Massive Conditional Branching

1// ANTI-PATTERN: Hard to maintain, violates Open/Closed Principle
2function calculateShipping(order, carrier) {
3 if (carrier === 'fedex') return order.weight * 1.5 + 5;
4 if (carrier === 'ups') return order.weight * 1.8 + 2;
5 if (carrier === 'dhl') return order.weight * 2.2 + 8;
6 // Adding new carrier requires editing this function!
7}

The Strategy Solution:

1// Strategy 1: FedEx
2const fedexStrategy = {
3 calculate: (weight) => weight * 1.5 + 5
4};
5 
6// Strategy 2: UPS
7const upsStrategy = {
8 calculate: (weight) => weight * 1.8 + 2
9};
10 
11// Strategy 3: DHL Express
12const dhlStrategy = {
13 calculate: (weight) => weight * 2.2 + 8
14};
15 
16// Context Object
17class ShippingCalculator {
18 constructor(strategy) {
19 this.strategy = strategy;
20 }
21 
22 setStrategy(strategy) {
23 this.strategy = strategy;
24 }
25 
26 calculateCost(order) {
27 return this.strategy.calculate(order.weight);
28 }
29}
30 
31// Swapping strategies dynamically at runtime:
32const shipping = new ShippingCalculator(fedexStrategy);
33console.log('FedEx Cost:', shipping.calculateCost({ weight: 10 })); // $20
34 
35shipping.setStrategy(dhlStrategy);
36console.log('DHL Cost:', shipping.calculateCost({ weight: 10 })); // $30

2. The State Pattern

The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

Media Player State Machine Example:

1 ┌──────────────┐
2 ┌────►│ PausedState │◄────┐
3Play │ └──────┬───────┘ │ Pause
4 │ │ Play │
5┌────┴──────┐ ▼ ┌─────┴─────┐
6│StoppedState│ │PlayingState│
7└───────────┘ └───────────┘

3. Key Difference Between Strategy & State

DimensionStrategy PatternState Pattern
IntentChange how an operation is accomplished (algorithm swapping)Change what the object does based on internal lifecycle state
AwarenessStrategies are generally unaware of each otherState objects frequently trigger transitions to neighboring states
Client ControlClient chooses and injects the strategyTransitions occur internally as actions execute

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What software engineering principle is best satisfied by the Strategy Pattern?
Question 2
What is the primary characteristic of the State Pattern?
Question 3
How does the Strategy pattern eliminate massive if/else chains?
Question 4
In the State pattern, who typically manages transitions between states?
Question 5
Can the active strategy in a context object be changed at runtime?
Next Lesson

Proxy & Decorator Patterns

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Observer & Pub/Sub PatternProxy & Decorator Patterns

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext