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

14 thoughts on “Java Design Pattern – Abstract Factory Pattern”

  1. Right here is the perfect web site for anyone who
    wishes to find out about this topic. You understand a whole lot its almost hard to argue with you (not that I personally would
    want to…HaHa). You certainly put a new spin on a subject that’s been discussed for many years.
    Excellent stuff, just great!

  2. Νice blog һеre! Additionally your website loadѕ up veгy
    fast! What host are yоu thе use of? Can I am getting your
    aѕsociate link on your host? I want my web ѕite loaded up as quickly as yours lol

  3. You’re so interesting! I don’t suppose I’ve truly read anything like that before.

    So great to discover somebody with a few original thoughts on this topic.
    Seriously.. thank you for starting this up. This
    site is something that’s needed on the internet, someone with some
    originality!

  4. Aⅾmiring thee hard work yyou put іnto your site and іn depth information you present.
    It’s good to come across a blog every once in a wһile thаt
    isn’t the same olԁ rehasһed material. Fantastic read!
    I’ve saved your site and I’m including your RSS feeԁ
    to mүy Google account.

  5. I have to thank you for the efforts you’ve put in penning
    this site. I am hoping to view the same high-grade content by you later on as well.
    In truth, your creative writing abilities has encouraged me to get my own, personal site now 😉

  6. Link exchange is nothing else except it is just placing the other person’s blog link on your page at suitable place and other
    person will also do similar in support of you.

  7. I’ve been surfing on-line greater than three hours
    lately, yet I by no means found any fascinating article like yours.
    It’s lovely worth sufficient for me. Personally, if
    all web owners and bloggers made good content as you did, the internet will
    likely be a lot more helpful than ever before.

  8. Unquestionably believe that which you said. Your
    favorite reason appeared to be on the net the easiest thing to be aware of.
    I say to you, I definitely get annoyed while people think about worries that
    they plainly do not know about. You managed to hit the
    nail upon the top as well as defined out the whole thing without having side effect , people
    can take a signal. Will probably be back to get more.
    Thanks

  9. I know this if off topic but I’m looking into starting my own weblog and was curious
    what all is needed to get set up? I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web savvy so I’m not 100% positive.

    Any suggestions or advice would be greatly appreciated.
    Kudos

  10. Hey! I know this is somewhat off topic but I was wondering which blog platform are you using for this website?
    I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at options for another platform.
    I would be great if you could point me in the direction of a good platform.

Leave a Reply

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