Extract EngineService into separate service

This commit is contained in:
Marty Oehme 2018-08-27 20:50:12 +02:00
parent 04d61bfa02
commit a92b864dfa
8 changed files with 86 additions and 67 deletions

View file

@ -5,6 +5,7 @@ import 'package:rules_of_living/components/controls_component.dart';
import 'package:rules_of_living/components/header_component.dart'; import 'package:rules_of_living/components/header_component.dart';
import 'package:rules_of_living/components/simulation_component.dart'; import 'package:rules_of_living/components/simulation_component.dart';
import 'package:rules_of_living/service/configuration_service.dart'; import 'package:rules_of_living/service/configuration_service.dart';
import 'package:rules_of_living/service/control_service.dart';
import 'package:rules_of_living/service/engine_service.dart'; import 'package:rules_of_living/service/engine_service.dart';
@Component( @Component(
@ -20,8 +21,16 @@ import 'package:rules_of_living/service/engine_service.dart';
ControlsComponent, ControlsComponent,
ConfigurationComponent ConfigurationComponent
], ],
providers: [materialProviders, ClassProvider(EngineService), ClassProvider(ConfigurationService)], providers: [
styleUrls: const ['package:angular_components/app_layout/layout.scss.css', 'app_component.css'], materialProviders,
ClassProvider(EngineService),
ClassProvider(ConfigurationService),
ClassProvider(EngineService)
],
styleUrls: const [
'package:angular_components/app_layout/layout.scss.css',
'app_component.css'
],
) )
class AppComponent { class AppComponent {
var name = "World"; var name = "World";

View file

@ -1,6 +1,6 @@
import 'package:angular/angular.dart'; import 'package:angular/angular.dart';
import 'package:angular_components/angular_components.dart'; import 'package:angular_components/angular_components.dart';
import 'package:rules_of_living/service/engine_service.dart'; import 'package:rules_of_living/service/control_service.dart';
@Component( @Component(
selector: 'sim-controls', selector: 'sim-controls',
@ -15,27 +15,27 @@ import 'package:rules_of_living/service/engine_service.dart';
styleUrls: const ["controls_component.css"], styleUrls: const ["controls_component.css"],
) )
class ControlsComponent { class ControlsComponent {
final EngineService engine; final ControlService ctrl;
ControlsComponent(this.engine); ControlsComponent(this.ctrl);
void onStartClicked() { void onStartClicked() {
engine.toggleRunning(); ctrl.toggleRunning();
} }
void onStepClicked() { void onStepClicked() {
engine.step(); ctrl.step();
} }
void onResetClicked() { void onResetClicked() {
engine.reset(); ctrl.reset();
} }
void onRandomClicked() { void onRandomClicked() {
engine.addRandomPattern(); ctrl.addRandomPattern();
} }
void onClearClicked() { void onClearClicked() {
engine.clear(); ctrl.clear();
} }
} }

View file

@ -1,7 +1,7 @@
import 'dart:html' as html; import 'dart:html' as html;
import 'package:angular/angular.dart'; import 'package:angular/angular.dart';
import 'package:rules_of_living/service/engine_service.dart'; import 'package:rules_of_living/service/control_service.dart';
@Component( @Component(
selector: 'gol-simulation', selector: 'gol-simulation',
@ -10,9 +10,9 @@ import 'package:rules_of_living/service/engine_service.dart';
providers: [], providers: [],
) )
class SimulationComponent implements OnInit { class SimulationComponent implements OnInit {
final EngineService engineService; final ControlService ctrl;
SimulationComponent(this.engineService); SimulationComponent(this.ctrl);
@override @override
void ngOnInit() { void ngOnInit() {
@ -29,6 +29,6 @@ class SimulationComponent implements OnInit {
the canvas did not load correctly :( the canvas did not load correctly :(
''', canvas.width / 2 - 50, canvas.height / 2); ''', canvas.width / 2 - 50, canvas.height / 2);
engineService.canvas = canvas; ctrl.canvas = canvas;
} }
} }

View file

@ -3,7 +3,7 @@ import 'dart:math';
import 'package:rules_of_living/service/engine_service.dart'; import 'package:rules_of_living/service/engine_service.dart';
class ConfigurationService { class ConfigurationService {
final EngineService engineService; final EngineService _es;
bool showGrid; bool showGrid;
@ -17,10 +17,10 @@ class ConfigurationService {
int get simSpeed => _simSpeed; int get simSpeed => _simSpeed;
void set simSpeed(int val) { void set simSpeed(int val) {
_simSpeed = val; _simSpeed = val;
engineService.engine.stepsPerSecond = simSpeed; _es.engine.stepsPerSecond = simSpeed;
} }
ConfigurationService(this.engineService) { ConfigurationService(this._es) {
showGrid = false; showGrid = false;
simSpeed = 5; simSpeed = 5;
} }
@ -30,8 +30,8 @@ class ConfigurationService {
} }
void setGridSize({int x, int y}) { void setGridSize({int x, int y}) {
x = x ?? engineService.engine.gridSize.x; x = x ?? _es.engine.gridSize.x;
y = y ?? engineService.engine.gridSize.y; y = y ?? _es.engine.gridSize.y;
engineService.engine.gridSize = Point(x, y); _es.engine.gridSize = Point(x, y);
} }
} }

View file

@ -0,0 +1,43 @@
import 'dart:html' as html;
import 'package:rules_of_living/service/engine_service.dart';
class ControlService {
EngineService _es;
ControlService(this._es);
void set canvas(html.CanvasElement canvas) => _es.engine.canvas = canvas;
html.CanvasElement get canvas => _es.engine.canvas;
void run() {
_es.engine.running = true;
}
void stop() {
_es.engine.running = false;
}
void toggleRunning() {
_es.engine.running = !_es.engine.running;
}
void step() {
_es.engine.step();
}
void reset() {
_es.engine.reset();
}
void addRandomPattern() {
_es.engine.running = false;
_es.engine.addPattern();
}
void clear() {
_es.engine.clear();
}
bool get isRunning => _es.engine.running;
}

View file

@ -1,48 +1,15 @@
import 'dart:html' as html;
import 'package:rules_of_living/src/Engine.dart'; import 'package:rules_of_living/src/Engine.dart';
class EngineService { class EngineService {
Engine _engine; Engine _uncachedEngineAccess;
Engine get engine => _engine ?? getEngine(Engine()); Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine());
void set engine(Engine newEngine) {
Engine getEngine(Engine engine) { _uncachedEngineAccess = newEngine;
_engine = engine;
return _engine;
} }
void set canvas(html.CanvasElement canvas) => engine.canvas = canvas; Engine _setCachedAndReturn(Engine newEngine) {
html.CanvasElement get canvas => engine.canvas; engine = newEngine;
return newEngine;
void run() {
engine.running = true;
} }
void stop() {
engine.running = false;
}
void toggleRunning() {
engine.running = !engine.running;
}
void step() {
engine.step();
}
void reset() {
engine.reset();
}
void addRandomPattern() {
engine.running = false;
engine.addPattern();
}
void clear() {
engine.clear();
}
bool get isRunning => engine.running;
} }

View file

@ -1,11 +1,11 @@
import 'dart:math'; import 'dart:math';
import 'package:mockito/mockito.dart';
import 'package:rules_of_living/service/configuration_service.dart';
import 'package:rules_of_living/service/engine_service.dart';
import 'package:rules_of_living/src/Engine.dart'; import 'package:rules_of_living/src/Engine.dart';
@TestOn('browser') @TestOn('browser')
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:rules_of_living/service/configuration_service.dart';
import 'package:rules_of_living/service/engine_service.dart';
import 'package:mockito/mockito.dart';
class MockEngine extends Mock implements Engine {} class MockEngine extends Mock implements Engine {}
@ -16,7 +16,7 @@ void main() {
setUp(() { setUp(() {
me = MockEngine(); me = MockEngine();
engineService = EngineService(); engineService = EngineService();
engineService.getEngine(me); engineService.engine = me;
sut = ConfigurationService(engineService); sut = ConfigurationService(engineService);
}); });

View file

@ -14,8 +14,8 @@ void main() {
sut = EngineService(); sut = EngineService();
}); });
group("Dependency Injection", () { group("Dependency Injection", () {
test("EngineService accesses the Engine defined in getEngine", () { test("EngineService can be passed a custom Engine", () {
sut.getEngine(me); sut.engine = me;
Engine result = sut.engine; Engine result = sut.engine;
expect(result, equals(me)); expect(result, equals(me));
@ -33,7 +33,7 @@ void main() {
}); });
test("caching can be overriden by providing a custom engine", () { test("caching can be overriden by providing a custom engine", () {
Engine first = sut.engine; Engine first = sut.engine;
sut.getEngine(me); sut.engine = me;
Engine second = sut.engine; Engine second = sut.engine;
expect(second, isNot(equals(first))); expect(second, isNot(equals(first)));
}); });