---
title: "JAVA - Factory Pattern"  
description: "Previously we learn JAVA : Singleton Pattern  Factory pattern is one of the most used design pattern in Java. This type of design pattern comes under"  
author: "Tarun Kumar"  
published: 2015-07-02  
updated: 2018-02-24  
canonical: https://www.mindstick.com/blog/10913/java-factory-pattern  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# JAVA - Factory Pattern

Previously we learn JAVA : [Singleton Pattern](https://www.mindstick.com/blog/10912/java-singleton-pattern) \

**[Factory](https://www.mindstick.com/blog/304231/factory-vs-service-in-angularjs-which-one-better) pattern** is one of the most used [design pattern](https://www.mindstick.com/forum/33927/how-to-implement-singleton-design-pattern-in-c-sharp) in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

*[[Define](https://yourviews.mindstick.com/audio/1110/lifestyles-choices-that-define-our-lives) an [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) for creating an object, but let the [subclasses](https://www.mindstick.com/interview/82/what-is-an-abstract-class) decide which [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) to instantiate. The Factory [method](https://www.mindstick.com/forum/166/webservice-method) lets a class defer instantiation to subclasses. ]*

Factory Method Pattern [Definition](https://yourviews.mindstick.com/view/70598/false-furore-over-the-leadership-definition-statement-of-army-chief-bipin-rawat) Diagram:

![JAVA - Factory Pattern](https://www.mindstick.com/blogs/a04fc1f8-0c5c-4051-bfa1-91ff7a06dc8e/images/8ff50061-fcf0-41f6-bb0c-51ffdd6ed365.png)\

Here we create a Shape interface and concrete classes implementing the Shape interface. A factory class ShapeFactory is defined.

FactoryPatternDemo is our demo class, it will use ShapeFactory to get a Shape object. It will pass information **(CIRCLE / RECTANGLE / SQUARE)** to ShapeFactory to get the type of object it needs.

## *Where Would I Use This Pattern?*

The idea behind the Factory Method pattern is that it allows for the case where a client doesn't know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job. The ***FactoryMethod*** builds on the concept of a simple Factory but lets the subclasses decide which implementation of the concrete class to use. You'll see factories used in logging frameworks, and in a lot of scenarios where the client doesn't need to know about the concrete implementations. It's a good approach to encapsulation.

\

Next, we will learn about : JAVA - [AbstractFactory Pattern](https://www.mindstick.com/Articles/1805/java-abstractfactory-pattern)

---

Original Source: https://www.mindstick.com/blog/10913/java-factory-pattern

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
