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.
Setup
If you haven't yet, set up the github.com/LearningTypeScript/projects repository locally.
shellgit clone https://github.com/LearningTypeScript/projects learning-typescript-projectscd learning-typescript-projectsnpm i
shellgit clone https://github.com/LearningTypeScript/projects learning-typescript-projectscd learning-typescript-projectsnpm i
Open your editor in this project's directory:
shellcode projects/classes/horror-factory
shellcode projects/classes/horror-factory
In one terminal, run the TypeScript compiler via the tsc script.
For example, to start the TypeScript compiler in watch mode:
shellnpm run tsc -- --watch
shellnpm run tsc -- --watch
In another terminal, run Jest via the test script.
For example, to start tests in watch mode:
shellnpm run test -- --watch
shellnpm run test -- --watch
Specification
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.
Notes
Note: your terminal should be in the
horror-factorydirectory, not the root repository's directory.
- Because there is now only one
Horrorclass, don't useabstractorprotected. Stick with true#privacy.