Abstract Factory Design Pattern

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Abstract Factory design pattern is also creational design pattern and it adds another layer of abstraction over Factory Pattern so you create an interface or abstract class for creating families of related objects without specifying their concrete classes. It means Abstract Factory class returns a factory of classes. So Abstract Factory Pattern is one level higher than the Factory Pattern.

When to use Abstract Factory design pattern

when you want to control multiple objects without any concern about conditions. This means when you want to have conditions about environment or whatever. For example if the Operating system is Windows this class should create and return Square class and if it is Mac OS, It should return Circle Class so by creating Abstract Factory you just call the get method and this class will manage and return the correct class.

Abstract Factory design pattern diagram:

abstract factory design pattern uml

Abstract Factory design pattern Example:

Step1: Create abstract class Mobile.java
package com.javatutsworld.abstractfactorypattern;

public abstract class Mobile {
    public abstract String getMemory();
    public abstract String getProcessor();
    public abstract String getSize();
     
    @Override
    public String toString(){
        return "Memory= "+this.getMemory()+", Processor="+this.getProcessor()+", Size="+this.getSize();
    }
}
Step2: Create Android.java and implement the abstract class method
package com.javatutsworld.abstractfactorypattern;

public class Android extends Mobile{
    private String memory;
    private String processor;
    private String size;
     
    public Android(String memory, String processor, String size){
        this.memory=memory;
        this.processor=processor;
        this.size=size;
    }
	@Override
	public String getMemory() {
		// TODO Auto-generated method stub
		return this.memory;
	}
	@Override
	public String getProcessor() {
		// TODO Auto-generated method stub
		return this.processor;
	}
	@Override
	public String getSize() {
		// TODO Auto-generated method stub
		return this.size;
	}
}
Step3: Create IPhone.java and implement the abstract class method
package com.javatutsworld.abstractfactorypattern;

public class IPhone extends Mobile{
    private String memory;
    private String processor;
    private String size;
     
    public IPhone(String memory, String processor, String size){
        this.memory=memory;
        this.processor=processor;
        this.size=size;
    }
	@Override
	public String getMemory() {
		// TODO Auto-generated method stub
		return this.memory;
	}
	@Override
	public String getProcessor() {
		// TODO Auto-generated method stub
		return this.processor;
	}
	@Override
	public String getSize() {
		// TODO Auto-generated method stub
		return this.size;
	}
}
Step4: Create MobileAbstractFactory.java and define createMobile() method

package com.javatutsworld.abstractfactorypattern;

public interface MobileAbstractFactory {
	Mobile createMobile();

}
Step5: Create AndroidFactory.java and implement interface method

package com.javatutsworld.abstractfactorypattern;

public class AndroidFactory implements MobileAbstractFactory{
    private String memory;
    private String processor;
    private String size;
     
    public AndroidFactory(String memory, String processor, String size){
        this.memory=memory;
        this.processor=processor;
        this.size=size;
    }

	@Override
	public Mobile createMobile() {
		// TODO Auto-generated method stub
		return new Android(memory, processor, size);
	}
    
   
}
Step6: Create IPhoneFactory.java and implement interface method
package com.javatutsworld.abstractfactorypattern;

public class IPhoneFactory implements MobileAbstractFactory{
    private String memory;
    private String processor;
    private String size;
     
    public IPhoneFactory(String memory, String processor, String size){
        this.memory=memory;
        this.processor=processor;
        this.size=size;
    }

	@Override
	public Mobile createMobile() {
		// TODO Auto-generated method stub
		return new IPhone(memory, processor, size);
	}
    
}
Step7: Create MobileFactory.java which will create sub classes
package com.javatutsworld.abstractfactorypattern;

public class MobileFactory {
	public static Mobile getMobile(MobileAbstractFactory factory){
		return factory.createMobile();
	}
}
Step8: Create MainApp.java which will test the abstract factory design pattern.

package com.javatutsworld.abstractfactorypattern;

public class MainApp {

	public static void main(String[] args) {
		testAbstractFactory();
	}

	private static void testAbstractFactory() {
		Mobile mbAndroid = MobileFactory.getMobile(new AndroidFactory("1 GB", "dual core", "5 inch"));
		Mobile mbIphone = MobileFactory.getMobile(new IPhoneFactory("2 GB", "octa core", "4 inch"));
		System.out.println("Android specification: "+mbAndroid);
		System.out.println("IPhone specification: "+mbIphone);
	}
}
Output
Android specification: Memory= 1 GB, Processor=dual core, Size=5 inch
IPhone specification: Memory= 2 GB, Processor=octa core, Size=4 inch
Here are methods which uses Abstract Factory Design Pattern in JDK
  • javax.xml.parsers.DocumentBuilderFactory#newInstance()
  • javax.xml.transform.TransformerFactory#newInstance()
  • javax.xml.xpath.XPathFactory#newInstance()
  • java.util.ResourceBundle#getBundle()
  • java.net.URL#openConnection()
  • java.sql.DriverManager#getConnection()
  • java.sql.Connection#createStatement()
  • java.sql.Statement#executeQuery()
  • java.text.NumberFormat#getInstance()
  • java.lang.management.ManagementFactory (all getXXX() methods)
  • java.nio.charset.Charset#forName()
  • java.net.URLStreamHandlerFactory#createURLStreamHandler(String) (Returns singleton object per protocol)
For more details please see
Head First Design Patterns
      Advertisements