- Java Design Patterns
- Factory Pattern: Java Design Patterns Part 17
Click on text below to jump to specific point in the video (except iOS)
This is John from caveofprogramming.com. And this is a tutorial on the factory pattern in Java. I am in Eclipse and I have a main method set up already here.
This is basically for use in a situation where you want to create lots of objects. Or you want to create one of several possible objects which all implement
the same interface or derive from the same parent.It is complicated to create these objects since you have to make some kind of complex choice when deciding which of these objects to instantiate. A good example - lots of constructor parameters. If it is possible to categorize the objects or specify in a simple way, use factory pattern.
Let us look at some examples.
This is a very very important pattern and often in interviews and exams you often get asked about the factory pattern. The DAO pattern often uses the dao factory. It is not so easy to find the factory pattern in the Java API but one which comes to mind is BorderFactory. If you have used Swing, you know what this is. If you have a Swing application which has borders around them and the borders could be described in different ways. BorderFactory has factory methods which you call and they return something that implements the Border Interface. These factory methods are manufacturing objects that all implement this interface.
Let us take a look at a simple example of implementing the Factory pattern. I will consider a somewhat silly examples. We are modeling a bunch of different animals in software. Create an interface called Animal and let us specify some methods like speak(). I will have a couple of objects which implement them. Let us start with Dog class which implements Animal and add the speak method. I will also have a Cat class. Now these are very simple to construct and don't have any overloaded constructors. Imagine that this class requires lots of configuration before use e.g. via constructor parameters.
We can simplify this using the Factory pattern. Let us create a new class called AnimalFactory. We can make the class abstract (optional). Let us have a method createAnimal(). In the BorderFactory example, we see that it has lots of different methods, and sometimes they take parameters.
Sometimes one method in your Factory which takes an int or enum to specify what object you are creating. Often you see that the Factory pattern using public static final int to configure the method, not enum. We can make createAnimal take an int and switch on the type and create the objects. This will usually be more complex in a real example - maybe making a tricky decision. Now to use this I go to my App and call Animal animal = AnimalFactory.createAnimal(AnimalFactory.CAT)
Thats the basic idea behind factory pattern - there are lots of variations on this. The basic idea is just sort of static method which takes some parameter and it goes through a decision process and returns an object of the appropriate type. It is common to make the class name AbstractAnimal.
We will look at further patterns later in the series. Video outline created using VideoJots. Click and drag lower right corner to resize video. On iOS
devices you cannot jump to video location by clicking on the outline.