Problem: Format Greetings in Multiple Styles
Problem statement
Your team is building a notification system that sends personalized greetings. Different teams want the message formatted differently:
Marketing prefers a casual tone.
HR needs a formal version.
The API team wants the data in JSON format.
Message formatters often rely on conditionals to handle different styles, which makes them brittle as formats evolve. Every new style risks breaking existing logic. Build a flexible greeting service using the Strategy Pattern, allowing the output style to be swapped cleanly at runtime.
Goal
Implement both elements of the pattern:
Strategies:
Create
CasualGreetingStrategy,FormalGreetingStrategy, andJsonGreetingStrategyclasses.Each must define a
.format(user)method that returns a message string or object in its respective style.
Context:
Implement
GreetingServiceto delegate message formatting to whichever strategy is active.Allow switching strategies with the
.setStrategy()method.
Constraints
No
iforswitchstatements inGreetingService.Each strategy must be self-contained—no shared formatting helpers.
Adding a new format should not require modifying existing strategies or the service.
Sample output
The examples below illustrate what the output should look like:
const user = { name: 'Alice', title: 'Engineer' };const service = new GreetingService(new CasualGreetingStrategy());console.log('Casual:', service.greet(user));/* Expected output:Casual: Hey Alice! Welcome back.*/service.setStrategy(new FormalGreetingStrategy());console.log('Formal:', service.greet(user));/* Expected output:Formal: Good afternoon, Engineer Alice.*/service.setStrategy(new JsonGreetingStrategy());console.log('JSON:', service.greet(user));/* Expected output:JSON: {"greeting":"Hello","name":"Alice","title":"Engineer"}*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Format Greetings in Multiple Styles
Problem statement
Your team is building a notification system that sends personalized greetings. Different teams want the message formatted differently:
Marketing prefers a casual tone.
HR needs a formal version.
The API team wants the data in JSON format.
Message formatters often rely on conditionals to handle different styles, which makes them brittle as formats evolve. Every new style risks breaking existing logic. Build a flexible greeting service using the Strategy Pattern, allowing the output style to be swapped cleanly at runtime.
Goal
Implement both elements of the pattern:
Strategies:
Create
CasualGreetingStrategy,FormalGreetingStrategy, andJsonGreetingStrategyclasses.Each must define a
.format(user)method that returns a message string or object in its respective style.
Context:
Implement
GreetingServiceto delegate message formatting to whichever strategy is active.Allow switching strategies with the
.setStrategy()method.
Constraints
No
iforswitchstatements inGreetingService.Each strategy must be self-contained—no shared formatting helpers.
Adding a new format should not require modifying existing strategies or the service.
Sample output
The examples below illustrate what the output should look like:
const user = { name: 'Alice', title: 'Engineer' };const service = new GreetingService(new CasualGreetingStrategy());console.log('Casual:', service.greet(user));/* Expected output:Casual: Hey Alice! Welcome back.*/service.setStrategy(new FormalGreetingStrategy());console.log('Formal:', service.greet(user));/* Expected output:Formal: Good afternoon, Engineer Alice.*/service.setStrategy(new JsonGreetingStrategy());console.log('JSON:', service.greet(user));/* Expected output:JSON: {"greeting":"Hello","name":"Alice","title":"Engineer"}*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.