Reputation: 117
I found this useful gist that introduced me into Builder Pattern Design.
How would one implement this code to build a "Car" object with required and optional parameters based on configurations without writing dedicated builders?
Scenario 1
Build a car with 3 wheels, no body and no engine.
Scenario 2
Build a car with 2 wheels, with a body and an engine.
Scenario 3
Build a car with 1 wheel, no body and no engine.
Scenrion N Some other random combination.
And let's imagine the "Car" object requires at least one wheel to be built.
I have stumbled upon a blog post that mentions a state machine approach but it still seems a bit messy to me.
The post: https://blog.jayway.com/2012/02/07/builder-pattern-with-a-twist/
Upvotes: 0
Views: 426
Reputation: 954
what about following:
inject Configuration Context as an argument into method getCar() in class Director then build appropriate Car object there
comments in gist are self explanatory:
/* Builder is responsible for constructing the smaller parts /
/ Director is responsible for the whole process */
Upvotes: 1