We will use Factory design pattern when we have one super class and many sub classes and we need to return instance of subclass based on input. Factory class will take the responsibility for object creation instead of client program
Super Class:
package com.sample.java.testing;
public abstract class FactoryPatternVehicleClass {
public abstract int getWheels();
public abstract String getEngineType();
@Override
public String toString(){
return this.getEngineType() + " has " + this.getWheels() + " wheels";
}
}
Subclass1:
package com.sample.java.testing;
public class FactoryPatternCarClass extends FactoryPatternVehicleClass {
int noOfWheels;
String engineType;
public FactoryPatternCarClass(int noOfWheels, String engineType) {
this.noOfWheels = noOfWheels;
this.engineType = engineType;
}
@Override
public int getWheels() {
return this.noOfWheels;
}
@Override
public String getEngineType() {
return this.engineType;
}
}
Subclass2:
package com.sample.java.testing;
public class FactoryPatternBusClass extends FactoryPatternVehicleClass {
int noOfWheels;
String engineType;
public FactoryPatternBusClass(int noOfWheels, String engineType) {
this.noOfWheels = noOfWheels;
this.engineType = engineType;
}
@Override
public int getWheels() {
return this.noOfWheels;
}
@Override
public String getEngineType() {
return this.engineType;
}
}
Factory class to return Objects:
package com.sample.java.testing;
public class FactoryPatternFactoryClass {
public static FactoryPatternVehicleClass getVehicleObject(
String objectType, int noOfWheels) {
if (null != objectType && objectType.equalsIgnoreCase("Bus")) {
return new FactoryPatternBusClass(noOfWheels, objectType);
} else if (null != objectType && objectType.equalsIgnoreCase("Car")) {
return new FactoryPatternBusClass(noOfWheels, objectType);
}
return null;
}
}
Client class to test:
package com.sample.java.testing;
public class FactoryPatternDemoTest {
public static void main(String a[]) {
FactoryPatternVehicleClass factoryPatternBusClass = FactoryPatternFactoryClass
.getVehicleObject("Bus", 6);
System.out.println(factoryPatternBusClass);
FactoryPatternVehicleClass factoryPatternCarClass = FactoryPatternFactoryClass
.getVehicleObject("Car", 4);
System.out.println(factoryPatternCarClass);
}
}
Output:
Bus has 6wheels
Car has 4wheels
Super Class:
package com.sample.java.testing;
public abstract class FactoryPatternVehicleClass {
public abstract int getWheels();
public abstract String getEngineType();
@Override
public String toString(){
return this.getEngineType() + " has " + this.getWheels() + " wheels";
}
}
Subclass1:
package com.sample.java.testing;
public class FactoryPatternCarClass extends FactoryPatternVehicleClass {
int noOfWheels;
String engineType;
public FactoryPatternCarClass(int noOfWheels, String engineType) {
this.noOfWheels = noOfWheels;
this.engineType = engineType;
}
@Override
public int getWheels() {
return this.noOfWheels;
}
@Override
public String getEngineType() {
return this.engineType;
}
}
Subclass2:
package com.sample.java.testing;
public class FactoryPatternBusClass extends FactoryPatternVehicleClass {
int noOfWheels;
String engineType;
public FactoryPatternBusClass(int noOfWheels, String engineType) {
this.noOfWheels = noOfWheels;
this.engineType = engineType;
}
@Override
public int getWheels() {
return this.noOfWheels;
}
@Override
public String getEngineType() {
return this.engineType;
}
}
Factory class to return Objects:
package com.sample.java.testing;
public class FactoryPatternFactoryClass {
public static FactoryPatternVehicleClass getVehicleObject(
String objectType, int noOfWheels) {
if (null != objectType && objectType.equalsIgnoreCase("Bus")) {
return new FactoryPatternBusClass(noOfWheels, objectType);
} else if (null != objectType && objectType.equalsIgnoreCase("Car")) {
return new FactoryPatternBusClass(noOfWheels, objectType);
}
return null;
}
}
Client class to test:
package com.sample.java.testing;
public class FactoryPatternDemoTest {
public static void main(String a[]) {
FactoryPatternVehicleClass factoryPatternBusClass = FactoryPatternFactoryClass
.getVehicleObject("Bus", 6);
System.out.println(factoryPatternBusClass);
FactoryPatternVehicleClass factoryPatternCarClass = FactoryPatternFactoryClass
.getVehicleObject("Car", 4);
System.out.println(factoryPatternCarClass);
}
}
Output:
Bus has 6wheels
Car has 4wheels
No comments:
Post a Comment