Abstract Factory Design Pattern

Abstract factory design pattern is factory of factory design pattern. It is a creational design pattern in which we create multiple factory class and for creating factory object as well there will be main factory class. This is most complicated design pattern among other creational design patterns. Its better to understand factory pattern first as abstract factory is enhanced version of factory pattern. To simplify things, I will say we will be having one super factory class which will be responsible for creating factory object among several factory classes.




In abstract factory design pattern , we have multiple factory classes which are responsible for object creation. This factory classes are segregated based on the object categorization. Like in our case we have divided the object based on operating system of mobile device and then created factory classes for same. So there are two factory classes in our case Android Device Factory and Apple Device Factory, both of them are created from Abstract Factory class.so we will be using this abstract factory class only for creating objects.


Object which we will be creating is a Mobile Device , so the base interface will be MobileDevice using which the concret class will be created for actual mobile devices , In our case we have created devices like Android Device1,Apple Device 1 etc..

Let's cut the theory out and make some practical :)


MobileDevice.java

 package com.design.pattern.abstract_factory;

public interface MobileDevice {
	public void printDeviceType();
}
 

AndroidDevice1.java

 package com.design.pattern.abstract_factory;

public class AndroidDevice1 implements MobileDevice {

	@Override
	public void printDeviceType() {
		System.out.println("This is Android Device 1");
	}

}
 

AndroidDevice2.java

 package com.design.pattern.abstract_factory;

public class AndroidDevice2 implements MobileDevice {

	@Override
	public void printDeviceType() {
		System.out.println("This is Android Device 2");
	}

}
 

AppleDevice1.java

 package com.design.pattern.abstract_factory;

public class AppleDevice1 implements MobileDevice {

	@Override
	public void printDeviceType() {
		System.out.println("This is Apple Device 1");
	}

}
 

AppleDevice2.java

 package com.design.pattern.abstract_factory;

public class AppleDevice2 implements MobileDevice {

	@Override
	public void printDeviceType() {
		System.out.println("This is Apple Device 2");
	}

}
 

AbstractFactory.java

 package com.design.pattern.abstract_factory;

public abstract class AbstractFactory {
	abstract MobileDevice getMobileDevice(String deviceName) ;
}
 

AndroidDeviceFactory.java

 package com.design.pattern.abstract_factory;

public class AndroidDeviceFactory extends AbstractFactory {
	

	@Override
	MobileDevice getMobileDevice(String deviceName) {
		if(deviceName.equals("AndroidDevice1")) {
			return new AndroidDevice1();
		}else if(deviceName.equals("AndroidDevice2")) {
			return new AndroidDevice2();
		}else {
			return null;
		}
	}

}
 

AppleDeviceFactory.java

 package com.design.pattern.abstract_factory;

public class AppleDeviceFactory  extends AbstractFactory {

	@Override
	MobileDevice getMobileDevice(String deviceName) {
		if(deviceName.equals("AppleDevice1")) {
			return new AppleDevice1();
		}else if(deviceName.equals("AppleDevice2")) {
			return new AppleDevice2();
		}else {
			return null;
		}
	}

}
 

DeviceFactoryProducer.java

 package com.design.pattern.abstract_factory;

public class DeviceFactoryProducer {
	public static AbstractFactory getDeviceFactory(boolean needOpenSourceOS) {
		if (needOpenSourceOS) {
			return new AndroidDeviceFactory();
		} else {
			return new AppleDeviceFactory();
		}
	}
}
 

AbstractFactoryDemo.java

 package com.design.pattern.abstract_factory;

public class AbstractFactoryDemo {
	public static void main(String[] args) {
		AbstractFactory openSourceDeviceFactory=DeviceFactoryProducer.getDeviceFactory(true);
		
		MobileDevice android1=openSourceDeviceFactory.getMobileDevice("AndroidDevice1");
		android1.printDeviceType();
		
		MobileDevice android2=openSourceDeviceFactory.getMobileDevice("AndroidDevice1");
		android2.printDeviceType();
		
		AbstractFactory closedSourceDeviceFactory=DeviceFactoryProducer.getDeviceFactory(false);
		
		MobileDevice apple1=closedSourceDeviceFactory.getMobileDevice("AppleDevice1");
		apple1.printDeviceType();

		MobileDevice apple2=closedSourceDeviceFactory.getMobileDevice("AppleDevice2");
		apple2.printDeviceType();
	}
}
 


Share this:

CONVERSATION

0 comments:

Post a Comment