From the course: Design Patterns: Creational

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Design a factory

Solution: Design a factory

From the course: Design Patterns: Creational

Start my 1-month free trial

Solution: Design a factory

(upbeat electronic music) - [Instructor] Here's how we organized the classes in the design to use a factory pattern for this challenge solution. The document class contains a createElement factory method that creates a concrete HTMLElement object based on the string tagName that you passed to the method. When you write code in the browser using JavaScript, you can call this method on the document object and get back an element. The type of the object you get back is the abstract HTMLElement class. The HTMLElement class implements a click method so you can handle click events in the same way on any HTMLElement you create. You aren't dependent on the specific type of element to handle click events. So your code to handle click events will work, no matter which concrete type of element you're working with and the code to create the elements is encapsulated in the createElement method. If you need to add a new type of…

Contents