Abstract Factory Pattern

Abstract Factory Pattern

In java abstract factory pattern is a design pattern which provides the technique to encapsulate group of individual factories where common theme are available.

In regular usage, it is where client creates concrete implementation of abstract factory pattern and then uses generic interfaces to create related concrete objects that are part of theme. Client does not need to know where concrete objects it gets from each of these internal factories, because it uses only generic interfaces of their related products. Abstract factory pattern separates details of its implementation of set of objects from their general usage.

Abstract Factory Pattern Example:

  • Create an abstract factory pattern interface which will have one method to return factory of software company
public interface SoftwareCompanyFactory {

	SoftwareCompany createSoftware();

}

 

  • Abstract Factory Pattern: Create class and implement SoftwareCompanyFactory interface
public class AppleFactory implements SoftwareCompanyFactory {

	@Override
	public SoftwareCompany createSoftware() {
		return new Apple();
	}

}

 

public class MicrosoftFactory implements SoftwareCompanyFactory{

	@Override
	public SoftwareCompany createSoftware() {
		return new Microsoft();
	}

}

 

  • Create factory interface which will have one method to return factory of software company product
public interface SoftwareCompany {
	String developSoftware();
}

 

  • Create class and implement SoftwareCompany interface
public class Apple implements SoftwareCompany {

	@Override
	public String developSoftware() {
		return "Mac operating system";
	}

}

 

public class Microsoft implements SoftwareCompany {

	@Override
	public String developSoftware() {
		return "Window operating system";
	}

}

 

  • Create client interaction class to get object at runtime based on demand
public class SoftwareFactory {

	SoftwareFactory(SoftwareCompanyFactory softwareCompanyFactory){
		SoftwareCompany softwareCompany=softwareCompanyFactory.createSoftware();
		System.out.println(softwareCompany.developSoftware());
	}

}

 

  • Now create client and use abstract factory:
public class SoftwareFactoryImplemetation {

	public static void main(String[] args) {

		//you could use abstract factory
		new SoftwareFactory(createSoftware("Apple"));

		// or directly get abstract factory runtime and get it's product
		SoftwareCompanyFactory softwareCompanyFactory=createSoftware("Apple");
		System.out.println(softwareCompanyFactory.createSoftware().developSoftware());

	}

	public static SoftwareCompanyFactory createSoftware(String companyName){

		if (companyName.equalsIgnoreCase("Microsoft")) {
			return new MicrosoftFactory();
		} else {
			return new AppleFactory();
		}

	}

}

 

  • That’s it. Below is list of all design patterns link:

Creational Patterns:

Structural Patterns:

Behavioral Patterns:

 

Leave a Reply

Your email address will not be published. Required fields are marked *