O'Reilly logo

Pro JavaScript™ Design Patterns by Dustin Diaz, Ross Harmes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required