Mastering Design Patterns with Examples — Factory Method and Abstract Factory Patterns

Creating objects with factories

Larry | Peng Yang
Computer Science Fundamentals

--

Photo by Ella Ivanescu on Unsplash

Overview

Welcome to the fourth chapter of this design patterns series. Let’s first review the design principles we have learned.

  1. Encapsulate what changes
  2. Program to interface, not implementation
  3. Favor composition over inheritance
  4. Loosely coupled designs between objects that interact
  5. 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.

--

--