Thursday, 2 July 2015

How does Abstract Factory Design Pattern work in java

Abstract Factory Design Pattern (AFDP) falls under the creational type. It provides one of the best ways to create the object. It is also known as the factory of factories.

Lets see in details how Abstract Factory Design Pattern works with the example of "Vehicle" and "Manufacturer" of the Vehicle.

Here is the overall architecture of our example.


Step 1: Create the interface of the Vehicle.

package com.designpattern.abstractfactory;

public interface Vehicle {
void speed();
}

Step 2: Create the implementation of the "Vehicle"

package com.designpattern.abstractfactory;

public class TwoWheeler implements Vehicle {
public void speed() {
System.out.println("My speed is 110km/hr");
}
}

package com.designpattern.abstractfactory;

public class FourWheeler implements Vehicle {
public void speed() {
System.out.println("My speed is 180km/hr");
}
}

Step 3: Create the interface for the "Manufacturer"

package com.designpattern.abstractfactory;

public interface Manufacturer {
void logo();
}

Step 4: Create the implementation of the "Manufacturer"

package com.designpattern.abstractfactory;

public class German implements Manufacturer{
public void logo() {
System.out.println("German Logo");
}
}


package com.designpattern.abstractfactory;

public class Korean implements Manufacturer{
public void logo() {
System.out.println("Korean Logo");
}
}

Step 5: Create the "AbstractFactory" class 

package com.designpattern.abstractfactory;

public abstract class AbstractFactory {
abstract Vehicle getVehicle(String vehicleType);
abstract Manufacturer getManufacturer(String manufacturerType);
}

Step 6 : Create the implementation class of "AbstractFactory"

package com.designpattern.abstractfactory;

public class VehicleFactory extends AbstractFactory{

@Override
Manufacturer getManufacturer(String manufacturerType) {
// TODO Auto-generated method stub
return null;
}
@Override
Vehicle getVehicle(String vehicleType) {
if(null == vehicleType) {
return null;
} else if ("TwoWheeler".equalsIgnoreCase(vehicleType)) {
return new TwoWheeler();
} else if ("FourWheeler".equalsIgnoreCase(vehicleType)) {
return new FourWheeler();
} else {
return null;
}
}

}


package com.designpattern.abstractfactory;

public class ManufacturerFactory extends AbstractFactory{

@Override
Manufacturer getManufacturer(String manufacturerType) {
if(null == manufacturerType) {
return null;
} else if ("German".equalsIgnoreCase(manufacturerType)) {
return new German();
} else if ("Korean".equalsIgnoreCase(manufacturerType)) {
return new Korean();
} else {
return null;
}
}
@Override
Vehicle getVehicle(String vehicleType) {
// TODO Auto-generated method stub
return null;
}

}


Step 7: Create FactoryProducer class 

package com.designpattern.abstractfactory;

public class FactoryProducer {
public static AbstractFactory getFactory(String option) {
if("Vehicle".equalsIgnoreCase(option)) {
return new VehicleFactory();
} else {
return new ManufacturerFactory();
}
}
}


Step 8: Create a Demo class

package com.designpattern.abstractfactory;

public class Demo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractFactory abstractFactory1 = FactoryProducer.getFactory("Vehicle");
Vehicle vehicle1 = abstractFactory1.getVehicle("TwoWheeler");
vehicle1.speed();
Vehicle vehicle2 = abstractFactory1.getVehicle("FourWheeler");
vehicle2.speed();
AbstractFactory abstractFactory2 = FactoryProducer.getFactory("Manufacturer");
Manufacturer manufacturer1 = abstractFactory2.getManufacturer("German");
manufacturer1.logo();
Manufacturer manufacturer2 = abstractFactory2.getManufacturer("Korean");
manufacturer2.logo();
}

}


Step 9: Check out the output

Here is the output for "Demo" class

My speed is 110km/hr
My speed is 180km/hr
German Logo
Korean Logo


Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics

No comments:

Post a Comment