design patterns - Object graph or individual properties as method parameters? -
say have quite deep object graph, simplified example, this
class car { engine engine { get; set; } dashboard dashboard { get; set; } ienumerable<wheel> wheels { get; set; } } class engine { pump fuelpump { get; set; } motor startermotor { get; set; } } and sake of example, instead of car having start() method, want have carstarter in charge of doing this. imaginine car can big, nested object graph , carstarter needs access couple of properties, of several levels deep. should pass car object carstarter, or properties important? of these overloads in simplified example?
class carstarter { void start(car car) { car.dashboard.lights.switchon(); car.engine.fuelpump.run(); car.engine.startermotor.start(); } void start(dashboard dashboard, pump fuelpump, motor startermotor) { dashboard.lights.switchon(); fuelpump.run(); startermotor.start(); } } the former feels wrong requires carstarter have in depth knowledge structure of entire nested structure of car class , properties. it's opaque properties of car object need populated when passed carstarter in unit test.
the latter seems better option me, @ risk of ending many parameters.
we can create parameter object detailed here: http://sourcemaking.com/refactoring/introduce-parameter-object
Comments
Post a Comment