Factory Design Pattern
Parent Class or Super Class
In Factory design pattern, Parent class can be an interface or abstract class or a normal java class.
In our example, we have parent class as abstract class with overridden toString() method. Any details regarding Factory design pattern please refer below:
ElectronicDevices.java
package com.gaurav.designpattern.factory;
/**
* @author gaurav
*
*/
public abstract class ElectronicDevices {
public abstract String getProcessorSpeed();
public abstract String getStorageCapacity();
@Override
public String toString() {
return "ProcessorSpeed = "
+ this.getProcessorSpeed()
+ ", StorageCapacity = "
+ this.getStorageCapacity()+", Device Name is = "+this.getClass().getSimpleName();
}
}
Child or Sub Classes are as follows:-
Desktop.java
package com.gaurav.designpattern.factory;
/**
* @author gaurav
*
*/
public class Desktop extends ElectronicDevices{
private String processorSpeed;
private String storageCapacity;
public Desktop(String pspeed, String storageCapacity){
this.processorSpeed = pspeed;
this.storageCapacity = storageCapacity;
}
@Override
public String getProcessorSpeed() {
return processorSpeed;
}
@Override
public String getStorageCapacity() {
return storageCapacity;
}
}
Laptop.java
package com.gaurav.designpattern.factory;
/**
* @author gaurav
*
*/
public class Laptop extends ElectronicDevices{
private String processorSpeed;
private String storageCapacity;
public Laptop(String pspeed, String storageCapacity){
this.processorSpeed = pspeed;
this.storageCapacity = storageCapacity;
}
@Override
public String getProcessorSpeed() {
return processorSpeed;
}
@Override
public String getStorageCapacity() {
return storageCapacity;
}
}
Tablet.java
package com.gaurav.designpattern.factory;
/**
* @author gaurav
*
*/
public class Tablet extends ElectronicDevices{
private String processorSpeed;
private String storageCapacity;
public Tablet(String pspeed, String storageCapacity){
this.processorSpeed = pspeed;
this.storageCapacity = storageCapacity;
}
@Override
public String getProcessorSpeed() {
return processorSpeed;
}
@Override
public String getStorageCapacity() {
return storageCapacity;
}
}
Now the basic implementation of the factory class
DeviceFactory.java
package com.gaurav.designpattern.factory;
/**
* @author gaurav
*
*/
public class DeviceFactory {
private static String monitorScreenType = "Touch Screen";
public static ElectronicDevices getDeviceType(int screenSize,
String processorSpeed, String screenType,
boolean isChargeableBatteryAvailable, String storageCapacity) {
if ((screenSize > 7 && screenSize < 10)
&& monitorScreenType.equalsIgnoreCase(screenType)
&& isChargeableBatteryAvailable == true)
return new Tablet(processorSpeed, storageCapacity);
else if ((screenSize > 10 && screenSize < 15.6)
|| monitorScreenType.equalsIgnoreCase(screenType)
&& isChargeableBatteryAvailable == true)
return new Laptop(processorSpeed, storageCapacity);
else if ((screenSize > 16 && screenSize < 32)
|| monitorScreenType.equalsIgnoreCase(screenType)
&& isChargeableBatteryAvailable == false);
return new Desktop(processorSpeed, storageCapacity);
}
}
Now the factory design pattern demonstration class:
FactoryDesignPatternDemo.java
package com.gaurav.designpattern.factory;
/**
* @author gaurav
*
*/
public class FactoryDesignPatternDemo {
public static void main(String[] args) {
ElectronicDevices electronicDevices1 = DeviceFactory.getDeviceType(22,"Intel Core i5-4570 Quad-Core","Touch Screen", false, "1TB");
ElectronicDevices electronicDevices2 = DeviceFactory.getDeviceType(15,"Intel Core i7 processor","Touch Screen", true, "500 GB");
ElectronicDevices electronicDevices3 = DeviceFactory.getDeviceType(9,"1.2 GHz Quad Core Processor","Touch Screen", true, "64 GB");
System.out.println("First Electronic device details are -->"+electronicDevices1);
System.out.println("Second Electronic device details are -->"+electronicDevices2);
System.out.println("Third Electronic device details are -->"+electronicDevices3);
}
}
Result:-
First Electronic device details are -->ProcessorSpeed = Intel Core i5-4570 Quad-Core, StorageCapacity = 1TB, Device Name is = Desktop
Second Electronic device details are -->ProcessorSpeed = Intel Core i7 processor, StorageCapacity = 500 GB, Device Name is = Laptop
Third Electronic device details are -->ProcessorSpeed = 1.2 GHz Quad Core Processor, StorageCapacity = 64 GB, Device Name is = Tablet
Note :- Depends on the input parameter, different subclass is created and returned as output.
No comments:
Post a Comment