Search⌘ K
AI Features

Exercise: Console Color Factory

Explore how to create a Console Color Factory using creational design patterns in Node.js. Understand how to define base and subclass consoles to output colored text, and implement a factory function to instantiate the proper console based on input color. This lesson helps you apply creational patterns to improve modularity and scalability in Node.js applications.

Problem statement

Create a class called ColorConsole that has just one empty method called log(). Then, create three subclasses: RedConsole, BlueConsole, and GreenConsole. The log() method of every ColorConsole subclass will accept a string as input and will print that string to the console using the color that gives the name to the class. Then, create a factory function that takes color as input, such as 'red', and returns the related ColorConsole subclass. Finally, write a ...