Skip to main content

Horror Factory

A Learning TypeScript > Classes 🍰 dessert project.

Hold on a moment! That abstract Horror class you wrote... It works, but have you heard of the factory pattern?

There's no need to make sub-classes for it when you can have a single Horror class take in its different behaviors as constructor parameters. Let's refactor the classes to do that.


If you haven't yet, set up the repository locally.

git clone learning-typescript-projects
cd learning-typescript-projects
npm i

Change your terminal directory to this project's:

cd projects/classes/horror-factory

In one terminal, start the TypeScript compiler in watch mode:

tsc --watch

In another terminal, run Jest via the test script. For example, to run tests in watch mode:

npm test -- --watch


The functional behavior of demons and sorcerers should work the same as in the entree project. However, instead of having Demon and Sorcerer classes, the exported Horror class should have a constructor that takes in an object containing name, isEvil, and getPowerFrom.

Then, create and export createDemon and createSorcerer functions instead of Demon and Sorcerer classes.


  • Because there is now only one Horror class, don't use abstract or protected. Stick with true # privacy.