modèles de conception - Qu'est-ce que l'injection de dépendance

Mots clés : design-patternslanguage-agnosticdependency-injectionterminologydesign-patterns

meilleur 3 Réponses modèles de conception - Qu'est-ce que l'injection de dépendance

vote vote

93

public SomeClass() {     myObject = Factory.getObject(); } 
public SomeClass (MyClass myObject) {     this.myObject = myObject; } 
vote vote

82

class Car{   private Wheel wh = new NepaliRubberWheel();   private Battery bt = new ExcideBattery();    //The rest } 
class Car{   private Wheel wh; // Inject an Instance of Wheel (dependency of car) at runtime   private Battery bt; // Inject an Instance of Battery (dependency of car) at runtime   Car(Wheel wh,Battery bt) {       this.wh = wh;       this.bt = bt;   }   //Or we can have setters   void setWheel(Wheel wh) {       this.wh = wh;   } } 
vote vote

73

public class PersonService {   public void addManager( Person employee, Person newManager ) { ... }   public void removeManager( Person employee, Person oldManager ) { ... }   public Group getGroupByManager( Person manager ) { ... } }  public class GroupMembershipService() {   public void addPersonToGroup( Person person, Group group ) { ... }   public void removePersonFromGroup( Person person, Group group ) { ... } }  

Questions similaires