Consider an rpg game where users complete tasks to win items that increase the attributes of their character.
There are 2 problems in calculateTotalAttributes.ts:
When a new item type is added, it is very easy for dev to forget to add cases in all places.
Let us expose a method from Item that contains the conversion logic. This can be used anytime we want to convert raw db data to Item class.
vs
Solved one is so much cleaner. If we had 10 types of items, the solved one doesn't change but the old one would have each case handled.
We just used the "Factory Method" pattern.
🥱 Boring Defintion: Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
🌱 Lesson Complete!