Mastering Design Patterns with Examples — Factory Method and Abstract Factory Patterns
Creating objects with factories
Overview
Welcome to the fourth chapter of this design patterns series. Let’s first review the design principles we have learned.
- Encapsulate what changes
- Program to interface, not implementation
- Favor composition over inheritance
- Loosely coupled designs between objects that interact
- Open/Closed principle
In this post, we will be learning another two creational design patterns — The Factory Method Pattern and Abstract Factory Pattern along with another design principle — Dependency Inversion Principle.
In Java, 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.
Use factory method pattern in Pizza Store problem
Let’s take a look at the Pizza Store problem in the Head First Design Pattern book.