so one of the advantages of using a factory class is that it organizes all the instantiations into one place so if you want to add a new type you can just easily go into the factory class you don't have to search in files, but in factory method pattern you place all those instantiations in different subclasses. won't the code be more difficult to find if you implement a factory method?
example:
//abstract superclass
abstract ball createBall (string ballType) {
public void play () {...}
abstract ball createBall (string ballType);
}
public normalBall extends ball {
abstract ball createBall (string ballType) {
if (ballType == "basketball")
return new normalBasketBall;
else if (ballType == "football")
return new normalFootBall;
}
}
public proBall extends ball {
abstract ball createBall (string ballType) {
if (ballType == "basketball")
return new proBasketBall;
else if (ballType == "football")
return new proFootBall;
}
}
consider this has many subclasses and you want to add a new ball to all the subclasses wont you have to hunt them all down and make changes in all of them? where as in a factory you know where all the object creations are and you just go there? also can you give me more advantages of factory method over factory class? P.S. English is my second language if you guys can keep your answers simple and easy to comprehend it would be much appreciated. Thanks.
Aucun commentaire:
Enregistrer un commentaire