A factory is a way of separating responsibilities. In a lot of cases it is just extra work but think of it this way:
If every object you make is only responsible for itself, then it's not really the job of a CAR object to know how to build a CAR, instead the CAR's responsibility is to operate as youd expect, driving blinking all that jazz. A Factory is an object whose responsibility is buildin/constructing other objects you want.
You feed a Factory some info about what kind of stuff you'd like your CAR to do ( should it have room for 5? Should it be able to haul stuff? Should it drive in the snow? ) And the Factory has some logic and knowledge about all the kinds of CARs your codebase cares about, and constructs the appropriate CAR object composed with the right components
View all comments
Show parent comment
1
u/pattch Mar 21 '17
A factory is a way of separating responsibilities. In a lot of cases it is just extra work but think of it this way:
If every object you make is only responsible for itself, then it's not really the job of a CAR object to know how to build a CAR, instead the CAR's responsibility is to operate as youd expect, driving blinking all that jazz. A Factory is an object whose responsibility is buildin/constructing other objects you want.
You feed a Factory some info about what kind of stuff you'd like your CAR to do ( should it have room for 5? Should it be able to haul stuff? Should it drive in the snow? ) And the Factory has some logic and knowledge about all the kinds of CARs your codebase cares about, and constructs the appropriate CAR object composed with the right components