
The Gilded Rose Kata: Composition Over Inheritance
The Gilded Rose refactoring kata is a classic coding exercise that challenges developers to refactor legacy code while adding new functionality. Most solutions reach for inheritance as the primary design pattern, but I want to show you a different approach: composition over inheritance . In this article, I’ll walk you through my solution that leverages composition and the Strategy pattern to create a more flexible and maintainable design. By the end, you’ll see why composition often leads to better software architecture. Credit to Emily Bache’s GitHub repository for the excellent kata resources. The Problem Here’s the legacy code we need to refactor: class GildedRose ( object ): def __init__ ( self , items ): self . items = items def update_quality ( self ): for item in self . items : if item . name != " Aged Brie " and item . name != " Backstage passes to a TAFKAL80ETC concert " : if item . quality > 0 : if item . name != " Sulfuras, Hand of Ragnaros " : item . quality = item . qualit
Continue reading on Dev.to Tutorial
Opens in a new tab

