Factory
Factory
Creational Patterns are concerned with object creation problems faced during
software design. Object creation often results in design problems, creational
patterns solve this problem by controlling the object creation.
Factory pattern
1
When the parent classes choose the creation of objects to its sub-
classes.
Step 1
Create an interface.
Shape.java
void draw();
2
Step 2
Create concrete classes implementing the same interface.
Rectangle.java
@Override
Square.java
@Override
Circle.java
@Override
3
Step 3
Create a Factory to generate object of concrete class based on given
information.
ShapeFactory.java
if(shapeType == null){
return null;
if(shapeType.equalsIgnoreCase("CIRCLE")){
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return null;
4
Step 4
Use the Factory to get object of concrete class by passing an information such
as type.
FactoryPatternDemo.java
shape1.draw();
shape2.draw();
shape3.draw();
5
Step 5
Verify the output.
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Another example in Factory pattern. Let`s understand the problem first then
we will solve the problem using Factory Pattern.
Consider the below UML diagram , where we have cake abstract class and
concrete sub-classes BlackForest, LitchiGateaux, BlueBerry, Pineapple.
Let’s see what goes inside these classes. Cake is the abstract class, all
different cake classes inherit from this class. Cake class has three properties;
name of the cake, type of the cake (whether cake contains egg or it is egg
less) and the last one is price.
6
Cake.Java
String name;
String type;
int price;
return name;
return type;
return price;
this.name=name;
this.price=price;
this.type=type;
7
System.out.println("I am "+name+" Cake");
myFans();
public BlackForest(){
setName("Black Forest");
setType("Eggless");
setPrice(800);
System.out.println("---BlackForest Recipe---");
8
BlueBerry.Java
BlueBerry(){
setName("Blue Berry");
setType("Egg");
setPrice(700);
System.out.println("---BlueBerry Recipe---");
System.out.println("Coat Berries");
}
}
LitchiGateaux.Java
LitchiGateaux(){
9
setName("Litchi Gateaux");
setType("Eggless");
setPrice(750);
System.out.println("---LitchiGateaux Recipe---");
import java.util.Scanner;
scanner.close();
if (choice.equals("BlackForest")) {
else if (choice.equals("BlueBerry")) {
10
}
else if (choice.equals("LitchiGateaux")) {
else if(choice.equals("Pineapple")){
cake=new Pineapple();
cake.aboutCake();
Notice the code in CakeTest class, we have got several concrete classes
BlackForest, BlueBerry, LitchiGateaux, Pineapple being instantiated, and the
decision of which class to instantiate is made at run-time depending on the
user’s choice.
When you see code like the above and when the time comes for changes or
extension, you will have to reopen the code and examine what needs to be
added or deleted. Suppose I don’t want to offer pineapple cake, I have to
reopen the code and delete some codes. Similarly, if later I decide to add 10
new cake types, I have to reopen the code and type 10 more “else if”
statements. Which means your code is not “closed for modifications”.
Note : Your design should always be “open for extension” but “closed
for modifications”.
The code in the CakeTest will certainly vary as we decide to delete some
cake type or add new cake types.
if (choice.equals("BlackForest")) {
else if (choice.equals("BlueBerry")) {
11
else if (choice.equals("LitchiGateaux")) {
else if(choice.equals("Pineapple")){
cake=new Pineapple();
So, anyone that needs a cake object can directly call factory class.
Now that we have understood the problem in our design, let’s separate the
code that vary.
Encapsulating Object Creation
12
CakeFactory.Java
Cake cake=null;
if (cakeName.equals("BlackForest")) {
else if (cakeName.equals("BlueBerry")) {
else if (cakeName.equals("LitchiGateaux")) {
else if(cakeName.equals("Pineapple")){
cake=new Pineapple();
return cake;
import java.util.Scanner;
13
String choice = scanner.nextLine();
scanner.close();
cake=cakeFactory.createCake(choice);
cake.aboutCake();
1. Our object creation code is at one place and any class that requires a
particular cake type can use factory class. We need not duplicate object
creation code at all the places.
2. The classes that requires particular cake object do not require to have
knowledge of all cake types. All they need to do is call the method in
the factory class passing the argument.
3. Now, our code is dynamic because we can swap in and out different
factory implementations. Let’s see how to do that.
14