PART 2Design Patterns
CHAPTER 7The Factory Pattern
Often a class or object will contain other objects within it. When these member objects need to be created, it is tempting to just instantiate them normally, using the new
keyword and the class constructor. The problem is that this creates a dependency between the two classes. In this chapter, we look at a pattern that will help decouple these two classes, and instead use a method to decide which specific class to instantiate. We discuss the simple factory pattern, which uses a separate class (often a singleton) to create instances, and the more complex factory pattern, which uses subclasses to decide what concrete class to instantiate as a member object.
The Simple Factory
The simple factory ...
Get Pro JavaScript™ Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.