samedi 3 avril 2021

How write subfactories linked to their general factory (avoiding these subfactories to be linked to another factories)?

I program in Java with the Spring Framework. But i have a question about design pattern.

I have a class X with 3 parts, 3 "big" parts: A B C

class X {
   private A a;
   private B b;
   private C c;
   // getters and setters
}

I make a factory for this class with 3 big privates methods to construct the 3 parts of the object of type X.

@Service
class XFactoryImpl implements XFactoryImpl {

    @Override
    X createX(InputData inputData) {
       X x = new X();
       x.setA(createA(inputData);
       x.setB(createB(inputData);
       x.setC(createC(inputData);
       return x;
    }

    private A createA(InputData inputData) {
       A a = new A();
       // lot of code lines
      return a;
    }

    private B createB(InputData inputData) {
       B b = new B();
       // lot of code lines
       return b;
    }

    private C createC(InputData inputData) {
       C c = new C();
       // lot of code lines
      return c;
    }

}

The problem is the following : as the 3 private methods createA, createB and createC are big methods, the factory implementation class is very big.

One solution could be to create 3 "subfactories" and inject these 3 subfactories into class FactoryX, one for class A objects, one for class B objects, one for C objects.

But the class A has no meaning outside of class X : A is necessary a type attribute of a class X object. Idem for class B and class C.

I do not want to create a Factory for objects of class A without linking this factory to Factory X class X, and especially forbid the calling of this factoryA outside from the factoryX (because calling this FactoryA NOT to create an object of class X has absolutely no sense, no meaning as A is only in my project a type of an attribute of class X).

If i create a factory for class A, is it possible to link it absolutely with class X ? If yes, how please ? If not, which design pattern could i use ?

With Builder, i would have have an enormous Builder.

Thank you in advance,

Thomas

Aucun commentaire:

Enregistrer un commentaire