I'm trying to implement the builder pattern but I encountered an issue. Here is the Class Diagram. Class Diagram
Let me briefly explain the context: I've this Assembler, which is the Director, who knows the structure of a circuit that needs to be done. The circuit can be composed of amplifiers, ports and filters. In order to create the whole structure, it's needed to create different instances of each component. The director exposes the method getCircuit() in which there are the necessary steps to create the structure.
If I'm not mistaken, I should pass to the builder the reference of the concrete builder that he needs. However, in all the examples I saw so far, there were no cases where the concrete builder is changed inside the method getCircuit() (usually called construct() ).
Finally, here is my question: Is it appropriate to make the director expose a method like setBuilder(...) which will be used by getCircuit() when it is necessary to change the concrete builder? Or is there a better approach?
Here is how I wrote the class Assembler. The implementation of getCircuit() is only an example of how I'd use setBuilder() that is changing the concrete builder.
public class Assembler {
private Manufacturer portBuilder;
private Manufacturer amplifierBuilder;
private Manufacturer filterBuilder;
private Manufacturer actualBuilder;
public Assembler(Manufacturer port, Manufacturer filter, Manufacturer amplifier) {
this.portBuilder=port;
this.amplifierBuilder=amplifier;
this.filterBuilder=filter;
}
public void setBuilder(Manufacturer manufacturer){
actualManufacturer = manufacturer;
}
public Manufacturer getCircuit(){
circuit = actualManufacturer.buildProduct();
actualManufacturer = setBuilder(amplifierBuilder);
setRelation(circuit, actualManufacturer.buildProduct() );
return circuit;
}
}
Aucun commentaire:
Enregistrer un commentaire