Skip to content

Instantly share code, notes, and snippets.

@peterheard01
Last active March 22, 2016 14:03
Show Gist options
  • Save peterheard01/c7a4cb198e94ffff111e to your computer and use it in GitHub Desktop.
Save peterheard01/c7a4cb198e94ffff111e to your computer and use it in GitHub Desktop.
//coupled
function main(userInputtedString, userInputtedBoolean, userInputtedInt){
var car = new Vehicle();
var bus = new Vehicle();
var plane = new Vehicle();
var vehicles = [car,bus,plane];
forEach(vehicle in vehicles){
vehicle.updateSomeStuff(userInputtedString);
vehicle.updateSomeOtherStuff(userInputtedBoolean,userInputtedInt);
}
}
//decoupling step 1 : Inverting Dependencies (IOC)
function main(userInputtedString, userInputtedBoolean, userInputtedInt){
var options = new UserOptions(userInputtedString,userInputtedBoolean,userInputtedInt);
var car = new Vehicle(options); // --> inverted here
var bus = new Vehicle(options); // --> inverted here
var plane = new Vehicle(options); // --> inverted here
var vehicles = [car,bus,plane];
forEach(vehicle in vehicles){
vehicle.update();// <-- decoupled here
}
}
//decoupling step 2 : Using An IOC/DI Container
function main(UserOptions,userInputtedString,userInputtedBoolean,userInputtedInt){
UserOptions.setUserOptions(userInputtedString,userInputtedBoolean,userInputtedInt); // --> dependency injected not 'newed'
var car = new Vehicle(); // --> decoupled here
var bus = new Vehicle(); // --> decoupled here
var plane = new Vehicle(); // --> decoupled here
var vehicles = [car,bus,plane];
forEach(vehicle in vehicles){
vehicle.update();
}
}
//decoupling step 3 : Further Decoupling
function main(UserOptions, VehicleFactory, Vehicleupdater, UserParams){
userOptions.setUserOptions(UserParams.captureInputteParams()); // <-- decoupled here
vehicleupdater.updateVehicles(vehicleFactory.load()); // <-- decoupled here
}
//decoupling step 4 : 99% decoupled
function main(VehicleFacade){
VehicleFacade.update(); // <-- our enire app is now hidden from the main method
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment