2016-01-06 20:28:25 +01:00
|
|
|
/**
|
|
|
|
* Poodinis Dependency Injection Framework
|
2019-07-14 11:40:16 +02:00
|
|
|
* Copyright 2014-2019 Mike Bierlee
|
2016-01-06 20:28:25 +01:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* The full terms of the license can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import poodinis;
|
|
|
|
|
|
|
|
import std.stdio;
|
|
|
|
|
|
|
|
interface Engine {
|
|
|
|
public void engage();
|
|
|
|
}
|
|
|
|
|
|
|
|
class FuelEngine : Engine {
|
|
|
|
public void engage() {
|
|
|
|
writeln("VROOOOOOM!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ElectricEngine : Engine {
|
|
|
|
public void engage() {
|
|
|
|
writeln("hummmmmmmm....");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class HybridCar {
|
|
|
|
alias KilometersPerHour = int;
|
|
|
|
|
|
|
|
@Autowire!FuelEngine
|
2016-02-09 21:06:07 +01:00
|
|
|
private Engine fuelEngine;
|
2016-01-06 20:28:25 +01:00
|
|
|
|
|
|
|
@Autowire!ElectricEngine
|
2016-02-09 21:06:07 +01:00
|
|
|
private Engine electricEngine;
|
2016-01-06 20:28:25 +01:00
|
|
|
|
|
|
|
public void moveAtSpeed(KilometersPerHour speed) {
|
|
|
|
if (speed <= 45) {
|
|
|
|
electricEngine.engage();
|
|
|
|
} else {
|
|
|
|
fuelEngine.engage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
2016-08-08 22:17:17 +02:00
|
|
|
auto dependencies = new shared DependencyContainer();
|
2016-01-06 20:28:25 +01:00
|
|
|
|
|
|
|
dependencies.register!HybridCar;
|
|
|
|
dependencies.register!(Engine, FuelEngine);
|
|
|
|
dependencies.register!(Engine, ElectricEngine);
|
|
|
|
|
|
|
|
auto car = dependencies.resolve!HybridCar;
|
|
|
|
|
|
|
|
car.moveAtSpeed(10); // Should print "hummmmmmmm...."
|
|
|
|
car.moveAtSpeed(50); // Should print "VROOOOOOM!"
|
|
|
|
}
|