I am wondering how deep use factory pattern in a design. Below the case.
I am going to build message objects. There are several types of messages so I will apply factory pattern for this implementation. message201 creation example
MessageFactoryCreator mfc = new MessageFactoryCreator();
AbstractFactoryMessage factoryMessage201 = new FactoryMessage201();
AbstractMessage msg201 = mfc.createMessage(factoryMessage201);
A message is composed by 3 elementes: header, package and variables.
- variable is a simple object with some attributes.
- package is actually a set of variables
- header is actually a set of variables
I also design factory patter for header and package creation as there are several kind of headers and packages. And depending the message it applies one header and several packages (different messages can have same packages).
PackageFactoryCreator pfc = new PackageFactoryCreator();
pfc.createPackage(new FactoryPackage11()
public class PackageFactoryCreator {
public AbstractPackage createPackage(AbstractFactoryPackage factoryPackage) {
return factoryPackage.createPackage();
}
}
As said package is a set of variables. Different packages can contain same variables. Here my question: Should I use factory pattern for variable creation? Below two examples with and without factory: Without facotry the package constructor looks like
public class PackageMessage11 extends AbstractPackage{
private NidPackage nidPackage;
private NcCdTrain ncCdTrain;
public PackageMessage11() {
this.nidPackage = new NidPackage();
this.ncCdTrain = new NcCdTrain();
this.setNumPck(11);
}
With facotry looks like:
public class PackageMessage11 extends AbstractPackage{
private NidPackage nidPackage;
private NcCdTrain ncCdTrain;
public PackageMessage11() {
VariableFactoryCreator vfc = new VariableFactoryCreator();
this.nidPackage = vfc.createVariable(new FactoryVariableNidPackage());
this.ncCdTrain = vfc.createVariable(new FactoryVariableNcCdTrain());
I am thinking about memory. For variable creattion I have to create a concrete factory for every single object. But without factory I have only to instatiate with new. And, the code is enough clear without using factory for variable creation. So my conclusion is factory is not necessary for variable creation within packages. Can anyone help to realize if factory is necessary or not for variable creation??
thanks a lot Kind regads
Aucun commentaire:
Enregistrer un commentaire