2016-01-06 20:28:25 +01:00
|
|
|
/**
|
|
|
|
* Poodinis Dependency Injection Framework
|
2023-01-11 00:01:51 +01:00
|
|
|
* Copyright 2014-2023 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;
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
interface Engine {
|
2023-05-17 23:03:02 +02:00
|
|
|
void engage();
|
2016-01-06 20:28:25 +01:00
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class FuelEngine : Engine {
|
2023-05-17 23:03:02 +02:00
|
|
|
void engage() {
|
2016-01-06 20:28:25 +01:00
|
|
|
writeln("VROOOOOOM!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class ElectricEngine : Engine {
|
2023-05-17 23:03:02 +02:00
|
|
|
void engage() {
|
2016-01-06 20:28:25 +01:00
|
|
|
writeln("hummmmmmmm....");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class HybridCar {
|
2016-01-06 20:28:25 +01:00
|
|
|
alias KilometersPerHour = int;
|
|
|
|
|
2023-03-07 01:29:54 +01:00
|
|
|
@Inject!FuelEngine private Engine fuelEngine;
|
2016-01-06 20:28:25 +01:00
|
|
|
|
2023-03-07 01:29:54 +01:00
|
|
|
@Inject!ElectricEngine private Engine electricEngine;
|
2016-01-06 20:28:25 +01:00
|
|
|
|
2023-05-17 23:03:02 +02:00
|
|
|
void moveAtSpeed(KilometersPerHour speed) {
|
2023-03-06 23:24:18 +01:00
|
|
|
if (speed <= 45) {
|
2016-01-06 20:28:25 +01:00
|
|
|
electricEngine.engage();
|
2023-03-06 23:24:18 +01:00
|
|
|
} else {
|
2016-01-06 20:28:25 +01:00
|
|
|
fuelEngine.engage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
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!"
|
|
|
|
}
|