After my recent post about a Concrete Factory method: Singleton I decide to write more about object creational pattern in Java, and here it goes the Abstract Factory…
What is Abstract Factory ?
Abstract Factory is a method of creating families of objects (products) without having to specify their concrete classes (concrete factory) – maybe that’s why the name is Abstract Factory.
When to use Abstract Factory ?
- When how the object is created should be transparent to the client.
- When the application need the object should be configured with one of multiple families of the products.
- When you want to provide a set of objects that have relationships and the contracts revealed but not the implementations.
How to implement Abstract Factory pattern ?
- Abstract Factory: you will have to write a class or just an interface to define the creation methods for the abstract products.
- Abstract Product: you will write a class or an interface that describe how the product will be and how the product will be used by other application.
- Concrete Factory: a subclass of the Abstract Factory, this is the class where you implement the creation of the products.
- Concrete Product: a subclass of the Abstract Product, this is the class where you implement the specification of the what and how given by the abstract product.
in this tutorial, I’d like to write about Vehicle Factory where makes car and truck. I will make different factories for Germany (Audi and Mercedes-Benz) and Italy (Ferrari and Iveco)