Junior — Middle
Selgitage juhtimise pööramise põhimõtet taksosüsteemi näite kaudu.
sobes.tech AI
Vastus AI-lt
Inversion of Control (IoC) is a principle where the management of object creation and binding is delegated to an external container or framework, rather than being implemented directly in the code.
Example in a taxi calling system:
- Instead of the
TaxiServiceclass creatingDriverorCarobjects itself, these dependencies are passed to it from outside (e.g., via the constructor). - The IoC container is responsible for creating and binding all objects.
public class TaxiService {
private Driver driver;
// Dependency is passed from outside
public TaxiService(Driver driver) {
this.driver = driver;
}
public void callTaxi() {
driver.driveToCustomer();
}
}
Here, TaxiService does not create Driver itself but receives it from outside. This simplifies testing, extension, and maintenance of the code, as dependencies can be changed without modifying the class itself.