Implement the Decorator Pattern
Advanced · Write the code · from Python Design Patterns: Factory, Strategy, Observer, and More
From the lesson: Python Design Patterns: Factory, Strategy, Observer, and More
Design patterns are proven recipes for solving common software problems. Think of them like architectural blueprints -- you wouldn't redesign plumbing from scratch for every house. Patterns give your code a shared vocabulary so other developers instantly understand your intent.
Read the lessonCreate a text processing pipeline using the Decorator pattern:
1. PlainText class with __init__(self, text) and render() returning the text as-is
2. BoldText class that wraps any text object and render() returns **<inner render>**
3. UpperText class that wraps any text object and render() returns the inner render in uppercase
Create a PlainText with 'hello', wrap it in BoldText, then wrap that in UpperText. Print the final render() result.
More Design Patterns exercises
- Build a Shape FactoryAdvanced
- Strategy Pattern with FunctionsAdvanced
- Build an Event SystemAdvanced
- Predict Singleton BehaviorAdvanced
- Template Method PatternAdvanced
All Python exercises · 50 practice problems · Advanced Python quiz