Loading...

Intro

Consider an rpg game where users complete tasks to win items that increase the attributes of their character.

  • : represents data stored in item table of the database
  • : exposes attributes of an individual item
  • : loops over all user items to calculate total attributes

Problem

There are 2 problems in calculateTotalAttributes.ts:

  • code duplication: converting raw data to the right Item class
  • responsibility: the implementation of converting data to Item is not responsibility of calculateTotalAttributes

When a new item type is added, it is very easy for dev to forget to add cases in all places.

Solution

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.

  • : Implement conversion logic
  • : use the new factory method Item.fromDbData

Compare

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!