Design Patterns: Factory Pattern, Part 1
Tags: uncategorized
Published: 2017-10-30 00:15:02

---
title: "Design Patterns: Factory Pattern, Part 1"
published: true
tags: Uncategorized
canonical_url: https://henricodesjava.blog/2017/10/29/design-patterns-factory-pattern-part-1/
---

Hi everyone. In Chapter four of&nbsp;&nbsp;[Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124/ref=sr_1_1?ie=UTF8&qid=1505068308&sr=8-1&keywords=head+first+design+patterns)&nbsp;we learn about the Factory pattern. The text begins by talking about a programming idiom known as the â€˜the Simple Factory’ as a warm up to two real Factory patterns. The first is known as “Factory Method and the second “Abstract Factory”.

Lets begins by introducing the problem that the factory pattern is solving. When we write code that uses the java keyword _new_, we are calling constructors to concrete classes and essentially creating a hard dependency on a concrete class. The snippet below shows how a concrete dependency for three different duck classes is created, this can quickly get out of hand. (chapter 1 talked about ducks a lot).

![Screen Shot 2017-10-22 at 4.47.35 PM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-22-at-4-47-35-pm1.png?w=266&h=203)

The Factory Patterns can help us clean this up and decouple these dependencies. A new design principle is introduced to help define this design mistake above.

**Dependency Inversion Principle:** Depend upon abstractions. Do not depend on concrete classes.

Lets give an example to help illustrate the design pattern. I’m going to use the same pizza shop example in the text. Lets say we have our own pizza shop, and we got some code for making pizzas that looks like this.

![Screen Shot 2017-10-23 at 10.38.08 PM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-23-at-10-38-08-pm2.png?w=367&h=337)

As soon as we start adding new pizza types, this class can easily get out of hand with _if-statements.&nbsp;_As we learned in earlier chapters we need to identify what part of the this code is likely to change, and what is likely not to change, and separate them.

![Screen Shot 2017-10-23 at 10.52.43 PM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-23-at-10-52-43-pm.png?w=505&h=323)

What the Factory patterns suggest we do is move all the constructors that are likely to change and put them in class of their own. Furthermore, what if our pizza store needs to be able to create groups of different types of pizzas, say if we had to make different kind of pizzas for the New York and another different type for Chicago (see below).

![Screen Shot 2017-10-24 at 11.40.17 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-24-at-11-40-17-am.png?w=900)

The factory patterns we’ll introduce allow us to organize and decouple the client from the concrete classes. All factory patterns encapsulates object creation, but lets give a formal definition for the first of our factory patterns.

**Factory Method Pattern:** &nbsp; Defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Lets take a look at a class diagram with the factory method pattern design for our pizza store problem.

First a class diagram for the group known as the “creator classes. These are the classes that are responsible for pizza creation. The abstract creator contains the common code between all pizza creations, that is prepare() bake() cut() box() methods. It also contains the two abstract methods createPizza() and orderPizza(). The concrete creators is where we use the Java keyword _new_ and instantiate classes (known as product classes).

![Screen Shot 2017-10-25 at 12.42.17 PM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-25-at-12-42-17-pm.png?w=433&h=353)

Next we have the Product Classes. These are the classes that we instantiate in the concrete creators. The abstract product serves as a generic type so that the creators don’t need to know what kind of pizza they are making, they just need to know that it is of pizza type.

![Screen Shot 2017-10-25 at 12.49.12 PM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-25-at-12-49-12-pm.png?w=483&h=421)

Now lets take a look at some code for this Factory Method Pattern implementation for our New York and Chicago pizza stores.

Abstract creator:

![Screen Shot 2017-10-29 at 11.14.29 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-29-at-11-14-29-am.png?w=479&h=200)

New York City concrete creator:

![Screen Shot 2017-10-29 at 11.16.03 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-29-at-11-16-03-am.png?w=346&h=214)

Chicago concrete creator:

![Screen Shot 2017-10-29 at 11.20.10 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-29-at-11-20-10-am.png?w=411&h=209)

Next, our group of product classes. First the abstract product. Note that the common code between all pizzas is kept here. Obviously we don’t have real implementation for these, so we simply print to standard output.

![Screen Shot 2017-10-29 at 11.24.25 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-29-at-11-24-25-am.png?w=922)

I won’t show all the concrete products, but here are a two, for a New York Pizza store, and a Chicago pizza store.

![Screen Shot 2017-10-29 at 11.27.24 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-29-at-11-27-24-am.png?w=351&h=147)

![Screen Shot 2017-10-29 at 11.28.07 AM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-29-at-11-28-07-am.png?w=451&h=198)

Each concrete product class gets to decide the name, the type of dough, sauce and toppings it has. Here all we see are strings being set, but you get the idea. Then what happens with these ingredients is out of the concrete product’s control (it’s in the abstract product).

Finally lets take a look at a client class making use of this awesome factory pattern. The nyStore and the chicagoStore are our two “factories for New York style and Chicago style pizzas. If you run this code below, you’ll see print statements that describe the preparation with each pizza’s specific ingredients and then each pizzas common baking, cutting, and boxing steps.

![Screen Shot 2017-10-29 at 12.35.01 PM.png](https://henricodesjava.files.wordpress.com/2017/10/screen-shot-2017-10-29-at-12-35-01-pm.png?w=920)

If you are interested in looking and running this example, take a look at this repo [here](https://github.com/bethrobson/Head-First-Design-Patterns/tree/master/src/headfirst/designpatterns/factory/pizzafm). Thats it! This Factory Method pattern relies on inheritance to delegate object creation. My next post will be part two on the topic of Factory patterns where I”ll talk about the Abstract Factory pattern, which relies on composition instead of inheritance. Thanks for reading&nbsp;