Skip to content

Instantly share code, notes, and snippets.

@girisagar46
Forked from bhavyakaria/car-di.java
Last active October 22, 2018 06:58
Show Gist options
  • Save girisagar46/5f050a7dbff1b72ae65a4595fc410fd7 to your computer and use it in GitHub Desktop.
Save girisagar46/5f050a7dbff1b72ae65a4595fc410fd7 to your computer and use it in GitHub Desktop.
class Car{
private Wheels wheel;
private Battery battery;
/*Somewhere in our codebase we instatiate the objects required by this class.
There are two methods for implementing DI:
1. Constructor based
2. Setter based
*/
// Constructor Based
Car(Wheel wh, Battery bt) {
this.wh = wh;
this.bt = bt;
}
// Setter Based
void setWheel(Battery bt){
this.bt = bt;
}
...
...
// Rest of code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment