Project Technical Lead
Java design pattern: Abstract Factory
In this article, we will look at the latest Java design pattern from the category of patterns for creating objects (creational patterns) – Abstract Factory.
Read more – a series of design patterns:
- Design pattern Singleton
- Design pattern Builder
- Design pattern Prototype
- Design pattern Factory
- Design pattern Adapter
What is the Abstract Factory design pattern?
The Abstract Factory design pattern is one of the so-called creational design patterns in software engineering. The goal of this pattern is to provide an interface for creating families of related objects without specifying their specific classes.
What problem does the Abstract Factory design pattern solve?
Abstract Factory solves the problem of creating instances of classes that share a common interface or are part of the same class family. The Abstract Factory design pattern is based on the principle of abstraction. Provides an interface that defines methods for creating objects. Specific implementations of this interface are represented by individual factories that create instances of specific classes. It helps decouple the process of creating objects from using them, and makes it easy to replace an entire family of objects with another without changing the code that uses those objects.
Example of Abstract Factory implementation in Java
Now we will show how to implement the Abstract Factory pattern in Java. This example illustrates how the Abstract Factory pattern allows the creation of families of related objects without the need to specify their specific classes in code. In this case, it’s a chocolate and nougat factory from the manufacturer Lindt.
Interface CokoladaAbstractFactory – defines two methods vyrobCokoladu() and vyrobNugat(), which are to return objects of type Cokolada and Nugat of the corresponding factories. This interface represents an abstract chocolate factory.
package designpatterns;
// Rozhranie pre abstraktnu vyrobnu cokoladu
public interface CokoladaAbstractFactory {
Cokolada vyrobCokoladu();
Nugat vyrobNugat();
}
LindtFactory class – implements the CokoladaAbstractFactory interface and provides implementations of the vyrobCokoladu() and vyrobNugat() methods. This class is a concrete implementation of an abstract chocolate factory for the manufacturer Lindt.
package designpatterns;
// Vyrobca Lindt cokolady implementuje AbstractFactory
public class LindtFactory implements CokoladaAbstractFactory {
public Cokolada vyrobCokoladu() {
return new LindtCokolada();
}
public Nugat vyrobNugat() {
return new LindtNugat();
}
}
Interface Cokolada – defines the zobrazTyp() method, which is implemented in the LindtCokolada class. This interface represents the type of chocolate.
package designpatterns;
// Rozhranie pre cokoladu
public interface Cokolada {
void zobrazTyp();
}
LindtCokolada class – implements the Chocolate interface and defines the zobrazTyp() method that prints information about the chocolate.
package designpatterns;
// Konkretna implementacia Lindt cokolady
public class LindtCokolada implements Cokolada {
public void zobrazTyp() {
System.out.println("Lindt čokoláda");
}
}
Nugat interface – defines the zobrazTyp() method, which is implemented in the LindtNugat class. This interface represents a type of nougat.
package designpatterns;
// Rozhranie pre nugat
public interface Nugat {
void zobrazTyp();
}
LindtNugat class – implements the Nugat interface and defines the zobrazTyp() method that prints information about the nugat.
package designpatterns;
// Konkretna implementacia Lindt nugatu
public class LindtNugat implements Nugat {
public void zobrazTyp() {
System.out.println("Lindt nugát");
}
}
Class Main: Contains the main() method, where the use of the abstract factory is demonstrated. A LindtFactory instance is created, which is of type CokoladaAbstractFactory. Subsequently, the Cokolada and Nugat objects are created using this abstract factory. Finally, the zobrazTyp() methods are called on these objects, which prints out the chocolate and nougat information.
import designpatterns.Cokolada;
import designpatterns.CokoladaAbstractFactory;
import designpatterns.LindtFactory;
import designpatterns.Nugat;
// Priklad pouzitia Abstract Factory
public class Main {
public static void main(String[] args) {
// Vytvorenie abstraknej tovarne na cokoladu
CokoladaAbstractFactory cokoladaFactory = new LindtFactory();
// Vyroba cokolady a nugatu pomocou abstraktnej tovarne
Cokolada cokolada = cokoladaFactory.vyrobCokoladu();
Nugat nugat = cokoladaFactory.vyrobNugat();
// A zobrazenie informacii o nich
cokolada.zobrazTyp();
nugat.zobrazTyp();
}
}
Note that by using the Abstract Factory design pattern, you can simply replace the LindtFactory instance with any other instance implementing the CokoladaAbstractFactory interface and the rest of the code will remain unchanged. For example, we could create a MilkaFactory to produce Milka chocolate.
We have prepared the files with the above example in the form of code that you can run directly in Java. Download the Java Abstract Factory code here.
If you’re looking for a job and you’re a Java developer, read more about developer salary and check out our job offers .