I would like to implement an example of abstract factory, however concrete factories must be served as singletons.
Giving the example of Mr. Banas here : http://www.newthinktank.com/2012/09/abstract-factory-design-pattern/ I should modify UFOEnemyShipFactory and UFOBossEnemyShipFactory right ?
I tried something for UFOEnemyShipFactory but I'm not sure to be right :
public class UFOEnemyShipFactory implements EnemyShipFactory{
private UFOEnemyShipFactory(){};
private static UFOEnemyShipFactory firstInstance = null;
public static UFOEnemyShipFactory getInstance(){
if(firstInstance == null){
synchronized(UFOEnemyShipFactory.class){
if(firstInstance == null){
firstInstance = new UFOEnemyShipFactory();
}
}
}
return firstInstance;
}
public ESWeapon addESGun(){
return new ESUFOGun();
}
public ESEngine addESEingine() {
return new ESUFOEngine();
}
It seems a bit weird, I think that I'm not applying the needed modification in the correct class. If I'm totally wrong could you please give me a brief explanation (why am I wrong and which class(es) I have to modify and why ?)
Thanks In advance.