jenniferemshepherd / dawn

procedurally generated socio-genetic simulation
2 stars 1 forks source link

Pass objects to constructors #40

Closed rcvink closed 6 years ago

rcvink commented 6 years ago

We could pass more of our objects to the constructors of other modules.

For example, in our dawn.js, we are doing this:

var decoratedEngine = new DecoratedEngine();
var eventController = new EventController(); 
var cellRepository = new CellRepository();
var animator = new Animator();
eventController.register(decoratedEngine.matterEngine(), 'afterUpdate', animator, cellRepository);

but it might be better if we could do this instead:

var decoratedEngine = new DecoratedEngine();
var eventController = new EventController(decoratedEngine.matterEngine()); 
var cellRepository = new CellRepository();
var animator = new Animator(cellRepository);
eventController.register('afterUpdate', animator);

In other words:

That way we don't have to pass these objects in every time we call methods. See how the eventController.register method now only needs two arguments - an event string and a listener/handler/operator. Instead of also passing in an engine every time we register a listener (I've started calling them listeners now I'm afraid). A similar simplification is achieved by having the animator have its own cellRepository, instead of passing that as an argument.

A similar pattern could be followed for the cellFactory stuff. I.e. cellFactory could have a cellRepository. This would have the same benefit if we use our eventController to register a listener for collisions.

I think we we were concerned that we'd have to update each object's cellRepository if we do things this way (I know I was). But actually, this is why we pass objects in, instead of static arrays - so that these objects are updated across the entire app.