Java Design Pattern – Abstract Factory Pattern

Abstract factory pattern is a Super-Factory of a group familiar factories. We can use it to expose API for integration code and hidden the logic implementation. Abstract Factory gives us a way to manage and decouple design in case Program has more than one Factory having the same Interface.

The tutorial will guide you a sample for how to program with Abstract Factory Pattern.

I. Technology

-Java 8
– Eclipse: Version: Mars.2 Release (4.5.2)

II. Overview – Abstract Factory Pattern

– Program has a MobileFactory interface that define API. SamsungFactory and AppleFactory are 2 classes that implement Mobile Factory.
– Product includes: SmartPhone & Table. AppleFactory produces: Iphone6, IPhone6Plus, IPadPro, IPadMini4. SamsungFactory produces: GalaxyJ7, GalaxyOn7, GalaxyTabA, GalaxyTabS2.

– Class Diagram

java design pattern - abstract factory pattern uml

– Project Structure:
java design pattern abstract factory project structure

III.Practices
1. Create Product API

– SmartPhone interface

public interface SmartPhone {
	public String info();
}

– Define SmartPhone List

public enum SmartPhoneType {
	Iphone6, Iphone6Plus, GalaxyOn7, GalaxyJ7
}

– Tablet interface

public interface Tablet {
	public String info();
}

– Define Tablet List

public enum TabletType {
	IPadPro, IPadMini4, GalaxyTabA, GalaxyTabS2
}
2. Create Smart Phone Product Concrete class

SmartPhone products:

– Iphone6

public class Iphone6 implements SmartPhone{
	
	@Override
	public String info() {
		String msg = String.format("This is %s - %s."
								,"iPhone 6s", "4.7-inch display");
		return msg;
	}
}

– Iphone 6 Plus:

public class Iphone6Plus implements SmartPhone{
	
	@Override
	public String info() {
		String msg = String.format("This is %s - %s."
								,"iPhone 6s Plus", "5.5-inch display");
		return msg;
	}
}

– GalaxyJ7:

public class GalaxyJ7 implements SmartPhone{
	
	@Override
	public String info() {
		String msg = String.format("This is %s - %s."
								, "Samsung Galaxy J7", "5.5-inches display");
		return msg;
	}
}

– GalaxyOn7

public class GalaxyOn7 implements SmartPhone{
	
	@Override
	public String info() {
		String msg = String.format("This is %s - %s."
								, "Samsung Galary On7", "5.5-inches display");
		return msg;
	}
}
3. Create Table product

– IPad Pro

public class IPadPro implements Tablet{

	@Override
	public String info() {
		String msg = String.format("This is %s - %s."
								, "iPad Pro", "12.9-inches display");
		return msg;
	}
}

– IPadMini4

public class IPadMini4 implements Tablet{

	@Override
	public String info() {
		String msg = String.format("This is %s - %s."
								, "iPad mini 4", "7.9-inches display");
		return msg;
	}
}

– GalaxyTabA

public class GalaxyTabA implements Tablet{
	
	@Override
	public String info() {
		String msg = String.format("This is %s - %s."
								, "Galaxy Tab A", "9.7-inches display");
		return msg;
	}
}

– GalaxyTabS2

public class GalaxyTabS2 implements Tablet{

	@Override
	public String info() {
		String msg = String.format("This is %s - %s."
								, "Samsung - Galaxy Tab S2 8.0", "8.0-inches display");
		return msg;
	}
}
3. Create Mobile Factory Interface
public interface MobileFactory {
	public SmartPhone createSmartPhone(SmartPhoneType type);
	public Tablet createTablet(TabletType type);
}

– Define Mobile Factory List

public enum MobileFactoryType {
	AppleFactory, SamsungFactory
}
4. Implement Abstract Mobile Factory

Create 2 Mobile Factory
– Implement AppleFactory

public class AppleFactory implements MobileFactory{
	@Override
	public SmartPhone createSmartPhone(SmartPhoneType type) {
		SmartPhone phone = null;
		switch(type){
			case Iphone6: 
				phone = new Iphone6();
				break;
			case Iphone6Plus:
				phone = new Iphone6Plus();
				break;
			default:
				break;
		}
		return phone;
	}

	@Override
	public Tablet createTablet(TabletType type) {
		Tablet tablet = null;
		switch(type){
			case IPadMini4:
				tablet = new IPadMini4();
				break;
			case IPadPro:
				tablet = new IPadPro();
				break;
			default:
				break;
		}
		return tablet;
	}

}

-Implement Samsung Mobile Factory

public class SamsungFactory implements MobileFactory{

	@Override
	public SmartPhone createSmartPhone(SmartPhoneType type) {
		SmartPhone phone = null;
		switch(type){
			case GalaxyJ7: 
				phone = new GalaxyJ7();
				break;
			case GalaxyOn7:
				phone = new GalaxyOn7();
				break;
			default:
				break;
		}
		return phone;
	}

	@Override
	public Tablet createTablet(TabletType type) {
		Tablet tablet = null;
		switch(type){
			case GalaxyTabA:
				tablet = new GalaxyTabA();
				break;
			case GalaxyTabS2:
				tablet = new GalaxyTabS2();
				break;
			default:
				break;
		}
		return tablet;
	}

}
5. Create Mobile Factory Producer& Implement Main Class
public class MobileProducer {
	public static MobileFactory getMobileFactory(MobileFactoryType type){
		MobileFactory factory = null;
		switch(type){
			case AppleFactory: 
				factory = new AppleFactory();
				break;
			case SamsungFactory:
				factory = new SamsungFactory();
				break;
			default:
				break;
		}
		return factory;
	}
}

– Main Class

public class MainClass {
	public static void main(String[] args) {
		
		List phones = new ArrayList();
		List tables = new ArrayList();
		
		/*
		 * Apple Factory
		 */
		MobileFactory appleFactory = MobileProducer.getMobileFactory(MobileFactoryType.AppleFactory);
		// Phones
		phones.add(appleFactory.createSmartPhone(SmartPhoneType.Iphone6));
		phones.add(appleFactory.createSmartPhone(SmartPhoneType.Iphone6Plus));
		// Tablet
		tables.add(appleFactory.createTablet(TabletType.IPadMini4));
		tables.add(appleFactory.createTablet(TabletType.IPadPro));
		
		/*
		 * Sumsung Factory
		 */
		MobileFactory sumsungFactory = MobileProducer.getMobileFactory(MobileFactoryType.SamsungFactory);
		// Phones
		phones.add(sumsungFactory.createSmartPhone(SmartPhoneType.GalaxyJ7));
		phones.add(sumsungFactory.createSmartPhone(SmartPhoneType.GalaxyOn7));
		// Tablet
		tables.add(sumsungFactory.createTablet(TabletType.GalaxyTabA));
		tables.add(sumsungFactory.createTablet(TabletType.GalaxyTabS2));
		
		/*
		 * Display Phone Info
		 */
		System.out.println("==========PHONE INFO=============");
		phones.forEach(phone->System.out.println(phone.info()));
		
		/*
		 * Display Tablet Info 
		 */
		System.out.println("==========TABLET INFO=============");
		tables.forEach(table->System.out.println(table.info()));
	}
}
IV. Result
==========PHONE INFO=============
This is iPhone 6s - 4.7-inch display.
This is iPhone 6s Plus - 5.5-inch display.
This is Samsung Galaxy J7 - 5.5-inches display.
This is Samsung Galary On7 - 5.5-inches display.
==========TABLET INFO=============
This is iPad mini 4 - 7.9-inches display.
This is iPad Pro - 12.9-inches display.
This is Galaxy Tab A - 9.7-inches display.
This is Samsung - Galaxy Tab S2 8.0 - 8.0-inches display.
V. Source Code

AbstractFactoryDesignPattern



By grokonez | October 15, 2016.

Last updated on June 4, 2017.



Related Posts


Got Something To Say:

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

*