Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creational Design Patterns: An Abstract Factory Pattern

DZone's Guide to

Creational Design Patterns: An Abstract Factory Pattern

Learn how to use the abstract factory pattern in Java to create objects without using a constructor.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

The abstract factory pattern is a creational pattern that is one of the most popular patterns, along with the builder and the factory pattern. Creational patterns are used to create objects without directly creating objects with a constructor.

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. The intent of employing the pattern is to insulate the creation of objects from their usage and to create families of related objects without having to depend on their concrete classes.

By using that pattern, the client doesn’t have to be aware of the objects and their implementation details. It is a responsibility of the abstract factory’s implementation to implement the objects and handle all the details successfully.

So, let’s get into action and start solving problems!

Supposing we are responsible for the canning process of various products, there are two objects that have to be created: the can’s main body and the can’s top. Also, considering the fact that we might have various forms of canning, we might as well have various factories that can handle the canning process. For example, we might have a factory for beer canning or a factory for food canning.

In regards to the description above, it seems that abstract factory is the way to go. We do have a family of objects that we want to hide the construction of these objects. We will start by adding two interfaces regarding the functionality of a can’s top and a can’s body.

package com.gkatzioura.design.creational.abstractfactory;

public interface CanTop {

    void open();

}
package com.gkatzioura.design.creational.abstractfactory;

public interface CanBody {

    void fill();

}


Then, we shall create an abstract factory that will provide the methods to create those objects.

package com.gkatzioura.design.creational.abstractfactory;

public abstract class CanningFactory {

    public abstract CanTop createTop();

    public abstract CanBody createBody();

}


As mentioned above, we have cases of beer canning. Thus, we will have implementations of the CanTop and the CanBody class.

package com.gkatzioura.design.creational.abstractfactory.beer;

import com.gkatzioura.design.creational.abstractfactory.CanTop;

public class BeerCanTop implements CanTop {

    public void open() {

    }
}
package com.gkatzioura.design.creational.abstractfactory.beer;

import com.gkatzioura.design.creational.abstractfactory.CanBody;

public class BeerCanBody implements CanBody {

    public void fill() {

    }
}


Then, we shall implement a beer canning factory.

package com.gkatzioura.design.creational.abstractfactory.beer;

import com.gkatzioura.design.creational.abstractfactory.CanBody;
import com.gkatzioura.design.creational.abstractfactory.CanTop;
import com.gkatzioura.design.creational.abstractfactory.CanningFactory;

public class BeerCanningFactory extends CanningFactory {

    public CanTop createTop() {
        return new BeerCanTop();
    }

    public CanBody createBody() {
        return new BeerCanBody();
    }

}


The other case is food canning. We will provide implementations of the CanTop and CanBody class for this case, too.

package com.gkatzioura.design.creational.abstractfactory.food;

import com.gkatzioura.design.creational.abstractfactory.CanBody;

public class FoodCanBody implements CanBody {

    public void fill() {

    }
}
package com.gkatzioura.design.creational.abstractfactory.food;

import com.gkatzioura.design.creational.abstractfactory.CanTop;

public class FoodCanTop implements CanTop {

    public void open() {

    }
}


As the last step, we will provide the abstract factory implementation for cases of food canning.

package com.gkatzioura.design.creational.abstractfactory.food;

import com.gkatzioura.design.creational.abstractfactory.CanBody;
import com.gkatzioura.design.creational.abstractfactory.CanTop;
import com.gkatzioura.design.creational.abstractfactory.CanningFactory;

public class FoodCanningFactory extends CanningFactory {

    public CanTop createTop() {
        return new FoodCanTop();
    }

    public CanBody createBody() {
        return new FoodCanBody();
    }

}


What we just did was using the abstract factory pattern in order to create a family of objects regarding the canning process. We insulated the creation process from the usage of the CanTop and CanBody. Also, we were able to create a family of objects without depending on their concrete classes.

You can find the source code at GitHub.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,design pattens ,tutorial ,abstract factory pattern ,object

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}