How to implement AbstractFactory pattern in Java?

Photo by Pixabay on Pexels.com

Let’s say you run a dress factory.

And you want to create an application to manage manufacturing dress.

You have a separate factory for manufacturing shirts and another factory for manufacturing pants.

These are the factories which are going to do the actual manufacturing.

But you create two more virtual factories (which don’t physically exist) , one to create shirts and pants for Kids and another to create shirts and pants for Women.

Thus you club together two products of the same family (Shirt and Pant of the family Dress) and create a virtual/ abstract factory for that.

That explains the Abstract Factory pattern.

Abstract Factory don’t do the actual creation of the products.

They take care of creating different products which belong to the same family.

And they delegate the creation of the products to the specific Product Factory.

That is why they are also called Factory of Factories.

Getting back to our problem, lets create the client code :

package creational.abstractfactory.pattern;

public class Client {
	
	
	public static void main(String a[]) {
		
		
		DressFactory kidsFactory = new KidsDressFactory();
		
		kidsFactory.createShirt("small", "contemporary", true, "blue");
		kidsFactory.createPant("small", "contemporary", false, "white");
		
		kidsFactory.packDress();

				
		
		DressFactory womensFactory = new WomenDressFactory();
		
		womensFactory.createShirt("medium", "classical", false, "yellow");
		womensFactory.createPant("medium", "classical", false, "blue");
		
		womensFactory.packDress();
		
	}

}

We create two abstract factories – KidsDressFactory and WomenDressFactory both of which take care of creating shirts and pants and packing them.

They both inherit the interface ‘DressFactory’.

The client code abstracts everything else.

All we can see in the client code is that the two factories take care of creating shirts and pants for Kids and Women.

Now let’s get behind the scenes.

Lets see the KidsDressFactory:

package creational.abstractfactory.pattern;

public class KidsDressFactory extends DressFactory {

	@Override
	public void createShirt(String sizeType, String style, boolean isFullHand, String color) {
		
		System.out.println("Creating Kids Shirt");

		ShirtFactory shirtFactory = new KidsShirtFactory();
		this.shirt = shirtFactory.createShirt(sizeType, style, isFullHand, color);

	}

	@Override
	public void createPant(String sizeType, String style, boolean isHalfPant, String color) {

		System.out.println("Creating Kids Pant");
		
		PantFactory pantFactory = new KidsPantFactory();


		this.pant = pantFactory.createPant(sizeType, style, isHalfPant, color);
	}

	@Override
	public void packDress() {
		
		
		System.out.println();
		System.out.println("Packing kids dress with shirt: " + this.shirt + "\n and pant:" + this.pant);
		System.out.println();

	}

}

The KidsDressFactory , an abstract factory creates shirts and pants for kids.

But they don’t do the actual manufacturing. They delegate it to KidsShirtFactory and KidsPantFactory respectively. We call ‘factory methods’ on these factories. Abstract factory uses Factory Design Pattern!.

Let’s look at the actual factory which makes the shirts for kids:

package creational.abstractfactory.pattern;

public class KidsShirtFactory implements ShirtFactory {

	@Override
	public Shirt createShirt(String sizeType, String style, boolean isFullHand, String color) {

		Shirt shirt = new KidsShirt(sizeType, style, isFullHand, color);

		return shirt;
	}

}

There you see , the actual manufacturing takes place here.

The Shirt factories follow a common interface ‘ShirtFactory’:

package creational.abstractfactory.pattern;

public interface ShirtFactory {

	public abstract Shirt createShirt(String sizeType, String style, boolean isFullHand, String color);

}

The same logic applies for manufacturing pants.

And all the above processes for manufacturing shirts and pants for kids also apply for manufacturing shirts and pants for women. These are taken care of by factory methods inside WomenShirtFactory and WomenPantFactory. Both these factories are in turn called by WomenDressFactory which delegate the manufacturing to them.

Shirt and Pant are interfaces for representing shirt and pant. KidsShirt, KidsPant and WomenShirt and WomenPant represent shirts and pants of kids and women respectively.

Below is the output on running the application :

Creating Kids Shirt
Creating Kids Pant

Packing kids dress with shirt: Shirt [sizeType=small, style=contemporary, isFullHand=true, color=blue]
 and pant:Pant [style=contemporary, sizeType=small, color=white, isHalfPant=false]

Creating Womens Shirt
Creating Womens Pant

Packing Womens dress with shirt: Shirt [sizeType=medium, style=classical, isFullHand=false, color=yellow]
 and pant:Pant [style=classical, sizeType=medium, color=blue, isHalfPant=false]


That’s it.

We have implemented Abstract Factory Design pattern.

The Gang of Four describe the Abstract Factory Design pattern as:

Provide an interface for creating families of related or dependent objects without specifying their concrete classes

And they suggest to use this pattern when :

– a system should be independent of its products are created , composed and represented ( in our case the client code does not know how the shirts and products are created)

– a system should be configured with multiple families of products ( we have different Dress Factories for kids and women , we can add one more for Men)

– a family of related product objects is designed to be used together and you need to enforce this constraint (Dress Factory is used to create dresses which consist of both shirts and pants which are to be used together)

– you want to provide a class library of products , and you want to reveal just their interfaces and not their implementations ( the client code is unaware of how the shirts and pants are constructed , it just knows that they are constructed by the same abstract factory which reveals that the dress family contains ‘shirts’ and ‘pants’)

The full code is available here :

https://github.com/vijaysrj/designPatternsGoF/tree/master/src/creational/abstractfactory/pattern


Posted

in

by

Tags:

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading