Lecture 31: Strategy Design Pattern in Spring

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-03-17

Lecture 31

Strategy Design Pattern in Spring

Week 8 | Unit IV: Spring Framework Basics
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor

Learning Objectives


  • Understand Strategy pattern
  • Encapsulate interchangeable algorithms
  • Wire and select strategy at runtime

Prerequisites


  • Core Java OOP concepts and interface-based design
  • Basic understanding of Servlets/JSP request flow from Unit III
  • Revision of Lecture 30 before moving into Strategy Design Pattern in Spring

Syllabus Mapping


  • Unit IV topic focus: Strategy Design Pattern in Spring
  • CO alignment: implementation understanding + architecture reasoning
  • Assessment alignment: short definitions + long implementation/design questions

Agenda


  • 5-minute recap from previous lecture
  • Concept deep dive: Strategy Design Pattern in Spring
  • Code/configuration walkthrough and output analysis
  • Debug checklist and exam-oriented summary

Introduction


Strategy isolates behavior variants behind one contract.

Think About It


Can pricing logic switch without changing controller?

Strategy Pattern


Define strategy interface and inject concrete strategy

Code Walkthrough


interface PricingStrategy { double total(double amount); }
class FestiveStrategy implements PricingStrategy { public double total(double a){ return a * 0.9; } }
class RegularStrategy implements PricingStrategy { public double total(double a){ return a; } }

Memory Booster


Syllabus memory points for Strategy Design Pattern in Spring:

  • Core recall: interface-based interchangeable algorithms
  • Exam compare: Hardcoded conditional logic vs strategy selection
  • Practical anchor: Create two strategies and switch implementation at runtime

Live Demo


Live implementation for Strategy Design Pattern in Spring:

?? Open Demo: Lecture 31 - Strategy Design Pattern in Spring

Demo checklist: - implement the key flow for this lecture - verify expected output for one success case - trigger one failure case and explain the fix

Resources & References


Structured Debug Checklist


  1. verify the primary API usage for Strategy Design Pattern in Spring is correct (imports, method names, config)
  2. check request/bean/session flow and object lifecycle assumptions
  3. inspect server logs for the first exception (not just the final symptom)
  4. reproduce one failing case and one passing case before finalizing fixes

Exam Preparation Questions: Short


  • Define Strategy Design Pattern in Spring with one practical use case.
  • Write/identify the key API or construct: interface-based interchangeable algorithms.
  • Differentiate: Hardcoded conditional logic vs strategy selection.
  • Mention one common implementation error and correction.

Exam Preparation Questions: Long


  • Explain Strategy Design Pattern in Spring with architecture/flow and implementation steps.
  • Write a structured answer comparing two approaches used in this topic.
  • Discuss debugging strategy for this topic with likely failure points.

Practice Task


  • Implement: Create two strategies and switch implementation at runtime.
  • Add console/log output to validate flow step-by-step.
  • Document one bug you encountered and the exact fix.

Checklist


Can you:

  • explain Strategy Design Pattern in Spring in your own words?
  • implement a basic example end-to-end?
  • identify and fix one common runtime issue?

Next Lecture


  • Topic: Lecture 32 - AOP & Bean Configuration Styles (XML and Annotation)
  • Preparation required: revise this lecture summary and code walkthrough

Questions?

Next: Lecture 32