I have seen a programm which uses the builder design pattern.
To my understanding the builder pattern is used to create a whole object with different components. It's used to avoid long parameterlists which are not readable.
For example when i create a car object, I want to inside different types of an engine.
CarBuilder.addEngine(new dieselEngine).build();
How the car and the engine behaves is implemented in the car-class and the engine-class.
But now I have seen builder used to alter the logic of the object like:
CarBuilder.addEngine(new dieselEngine.addStartStopSystem().addCatalysis()).build();
You build very big builderS in which you are forced to use methods which alter the behaviour of this whole object you build. The builders get very large and I don't know which method to call when I want to have a certain behaviour by this object.
My opinion is that you shouldn't use any logic inside these builders. I would create an object which extends the main object and alter the behaviour there and hand it over in the builder.
What are your opinions? Should builders be used this way?
Aucun commentaire:
Enregistrer un commentaire