Solution: Format Greetings in Multiple Styles
Implement interchangeable formatting strategies and delegate message creation through a configurable GreetingService context.
We'll cover the following...
We'll cover the following...
Solution explanation
Lines 2–22: We define three interchangeable formatting strategy classes:
CasualGreetingStrategy,FormalGreetingStrategy, andJsonGreetingStrategy.Each implements a
.format()method with its own message style.CasualGreetingStrategyuses an informal greeting.FormalGreetingStrategybuilds a professional message including the title.JsonGreetingStrategyoutputs a structured JSON string.
Lines 25–38: The
GreetingServiceclass ...