Skip to Content
Head First Design Patterns, 2nd Edition
book

Head First Design Patterns, 2nd Edition

by Eric Freeman, Elisabeth Robson
December 2020
Beginner
672 pages
15h 24m
English
O'Reilly Media, Inc.
Content preview from Head First Design Patterns, 2nd Edition

Chapter 4. Baking with OO Goodness: The Factory Pattern

Images

Get ready to bake some loosely coupled OO designs. There is more to making objects than just using the new operator. You’ll learn that instantiation is an activity that shouldn’t always be done in public and can often lead to coupling problems. And we don’t want that, do we? Find out how Factory Patterns can help save you from embarrassing dependencies.

Images

When you see “new,” think “concrete.”

Yes, when you use the new operator you are certainly instantiating a concrete class, so that’s definitely an implementation and not an interface. And you make a good observation: that tying your code to a concrete class can make it more fragile and less flexible.

Images

When we have a whole set of related concrete classes, often we end up writing code like this:

Images

Here we’ve got several concrete classes being instantiated, and the decision of which to instantiate is made at runtime depending on some set of conditions.

When you see code like this, you know that when it comes time for changes or extensions, you’ll have to reopen this code ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Designing Data-Intensive Applications, 2nd Edition

Designing Data-Intensive Applications, 2nd Edition

Martin Kleppmann, Chris Riccomini
Design Patterns: Elements of Reusable Object-Oriented Software

Design Patterns: Elements of Reusable Object-Oriented Software

Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

Publisher Resources

ISBN: 9781492077992Errata PageSupplemental Content