In the last tutorial we looked at how we could implement both abstract classes and interfaces, this tutorial will look to describe the Factory Method Design Pattern.

Intent

"Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."

Motivation

The Factory Method pattern is defined as a creational pattern which uses Factory methods to deal with the problem of creating objects without specifying the exact class of object that will be created. It is a creational pattern which is used to ‘manufacture’ objects, hence it’s name and it allows applications a flexibility in choosing what specific type of concrete object to create when that object is needed. This is perfect for game developers as it can lead to dynamic enemy generation during game-play as using this design pattern allows us to build a class that will chose for us what sort of ‘enemy’ to spawn next.

When to Use the Factory Pattern

When you won’t know until run-time what specific concrete class you will need to implement. When all of the potential classes have the same superclass To help organize code by centralizing which chooses the subclass to one class.

UML Diagram

Factory Method Design Pattern Tutorial

Implementation

EnemyFactory Class:

This class is an example of a Factory that generates a new enemy based on a random number. Note how this is done at run-time rather than hard-coded at the start.

import java.util.Random;


public class EnemyFactory {
	
	public Enemy generateEnemy(){
		Enemy enemy = null;
		Random random = new Random();
		
		int typeOfEnemy = random.nextInt(10);
		
		if(typeOfEnemy < 2)
			enemy = new Sniper();
		else if(typeOfEnemy >= 2 && typeOfEnemy < 6)
			enemy = new Infantry();
		else if(typeOfEnemy >= 6)
			enemy = new Tank();
		else
			return null;
		
		return enemy;
	}

}

Enemy Class:

Our Abstract Enemy class.

public abstract class Enemy {

	double health;
	int Ammo;
	
	public abstract void attack();
	
	public Double getHealth(){
		return this.health;
	}
	
	public void setHealth(double newHealth){
		this.health = newHealth;
	}
	
}

Our Concrete Classes:

public class Tank extends Enemy{

	double health;
	int ammo;
	
	@Override
	public void attack() {
		// TODO Auto-generated method stub
		System.out.println("Tank Attack!");
	}

}

Infantry Class


public class Infantry extends Enemy{

	double health;
	int ammo;
	
	@Override
	public void attack() {
		// TODO Auto-generated method stub
		System.out.println("Infantry Attack!");
	}

}

Sniper Class

public class Sniper extends Enemy{
	
	double health;
	int ammo;
	
	@Override
	public void attack() {
		System.out.println("Sniper Attack");
	}
	
}

Main Class:

And in order to pull all this code together we have our Main class which features the main method.

public class FactoryMain {
	
	public static void main(String args[]){
		EnemyFactory enemyFactory = new EnemyFactory();
		
		Enemy enemy = enemyFactory.generateEnemy();
		
		enemy.attack();
	}

}