Dependency injection examples

Report a typo

Match pseudocode chunks with types of DI used in them.

Example 1:

class Robot is
  Legs legs = new Legs() 

  method walk() is
    legs.walk()

class Control is
  Robot bob = new Robot()
  bob.walk()

Example 2:

class Robot is
  Legs legs
  
  constructor of Robot(Legs legs) is
    this.legs = legs

  method walk() is ...

class Control is
  Robot bob = new Robot(new Legs())
  bob.walk()

Example 3:

class Robot is
  Legs legs

  method setLegs(Legs legs) is
    this.legs = legs

  method walk()
    legs.walk()

class Control is
  Robot bob = new Robot()
  bob.setLegs(new Legs())
  bob.walk()

Example 4:

interface Robot is
  method walk()

class BigRobot implements Robot is
  method walk() is ...

class Control is
  Robot bob
  
  method controlRobot(Robot bob)
    this.bob = bob

  method walk() is
    bob.walk()
Match the items from left and right columns
Example 1
Example 2
Example 3
Example 4
Setter injection
Method injection
Constructor injection
No DI
___

Create a free account to access the full topic