In this blog post, I’ll briefly describe the simple factory pseudopattern.
This post is part of the Design Patterns in Java Series. Check out the full list of covered patterns there.
First of all, as I mentioned in the article on the factory method pattern, I use the prefix pseudo because the simple factory isn’t actually considered a pattern in the Gang of Four book.
Nevertheless, I’ve decided to write a blog post on this topic because I think the simple factory will give you a good intuition for understanding the proper factory patterns.
Many online resources portray the simple factory as the factory method pattern, which is wrong.
You can read more about the terminology on Wikipedia.
The simple factory is essentially a class that, based on a given input, returns objects of different types.
In contrast to the factory method or the abstract factory, the simple factory is made up of just one concrete factory. This concrete factory doesn’t inherit from another abstraction layer.
Just to reiterate, the simple factory is not considered a genuine pattern. That being said, use this concept as a stepping stone to understanding the factory method and the abstract factory.
To illustrate this pattern, let’s consider we’re a company that builds computers. For the moment, we’re only producing laptops.
In the class diagram below, the concept of laptop is shown via the Laptop
interface. We have two types of laptops which are represented by the NormalLaptop
and GamingLaptop
concrete classes that implement the Laptop
interface.
The client code is using the LaptopFactory
concrete factory class to create the objects it needs via the createLaptop()
method.
Simple Factory Class Diagram
switch
statement, in the createLaptop()
method below. This can be avoided with the factory method pattern.As usual, let’s implement in Java the design from the class diagram we saw in the previous section.
The Laptop
interface would look something like this:
public interface Laptop { | |
void runTests(); | |
} |
The classes implementing the Laptop
interface are as follows:
public class NormalLaptop implements Laptop { | |
@Override | |
public void runTests() { | |
System.out.println("Running tests on a NormalLaptop..."); | |
} | |
} |
public class GamingLaptop implements Laptop { | |
@Override | |
public void runTests() { | |
System.out.println("Running tests on a GamingLaptop..."); | |
} | |
} |
Next, the object creation is done in LaptopFactory
based on information provided by the client code.
public class LaptopFactory { | |
// Objects are created here | |
public Laptop createLaptop(String laptopType){ | |
if (laptopType == null) { | |
return null; | |
} | |
switch (laptopType.toUpperCase()) { | |
case "NormalLaptop": | |
return new NormalLaptop(); | |
break; | |
case "GamingLaptop": | |
return new GamingLaptop(); | |
break; | |
default: | |
return null; | |
} | |
} | |
} |
Lastly, the client code receives an object depending on the requested type:
public class Client { | |
public static void main(String[] args) { | |
LaptopFactory laptopFactory = new LaptopFactory(); | |
// Get object of type GamingLaptop and run tests. | |
Laptop myLaptop = laptopFactory.createLaptop("GamingLaptop"); | |
myLaptop.runTests(); | |
} | |
} |
The output would then be:
Running tests on a GamingLaptop... |
In conclusion, the two ideas you have to bear in mind are:
The simple factory is not a real pattern.
The simple factory is just the concrete factory, without the extra factory class abstraction found in either the factory method or abstract factory.
Like this article? Stay updated by subscribing to my weekly newsletter: