February 2019
Beginner
694 pages
18h 4m
English
Let's now move on to the Factory class itself. This class has a single, well-defined responsibility: given a date of birth, figure out whether it is less than two years ago, less than 18 years ago, or more than 18 years ago. Based on these decisions, return an instance of either an Infant, Child, or Adult class, as follows:
class PersonFactory { getPerson(dateOfBirth: Date) : IPerson { let dateNow = new Date(); // defaults to now. let currentMonth = dateNow.getMonth() + 1; let currentDate = dateNow.getDate(); let dateTwoYearsAgo = new Date( dateNow.getFullYear() - 2, currentMonth, currentDate); let date18YearsAgo = new Date( dateNow.getFullYear() - 18, currentMonth, currentDate); if (dateOfBirth >= dateTwoYearsAgo) { return ...Read now
Unlock full access