Skip to content

Commit 06ccb2c

Browse files
committed
Divide Originator into Recovery-Backup classes.
1 parent ec43b4b commit 06ccb2c

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

patterns/memento/memento_editor/editor/editor.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import '../memento_pattern/originator.dart';
55
import '../shapes/shapes.dart';
66
import 'manipulator.dart';
77

8-
class Editor extends ClassicApp with Manipulator, Shapes, Originator {
8+
class Editor extends ClassicApp
9+
with Manipulator, Shapes, BackupOriginator, RecoveryOriginator {
910
@override
1011
void onPaint(Canvas canvas, Size canvasSize) {
1112
_paintBackground(canvas, canvasSize);

patterns/memento/memento_editor/memento_pattern/originator.dart

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,22 @@ import '../shapes/shapes.dart';
66
import '../shapes/shape.dart';
77
import 'snapshot.dart';
88

9-
mixin Originator implements Shapes {
10-
Snapshot backup() {
11-
final data = _allocateBuffer();
12-
_writeShapes(data);
13-
_writeSelectedIndex(data);
14-
return _toSnapshot(data);
15-
}
9+
const _shapeByteSize = 16;
10+
const _selectedIndexByteSize = 4;
1611

17-
void restore(Snapshot snapshot) {
18-
final byteData = _fromSnapshotToByteData(snapshot);
19-
final newShapes = _readShapes(byteData);
20-
final selectedIndex = _readSelectedIndex(byteData);
21-
shapes.clear();
22-
shapes.addAll(newShapes);
23-
selectByIndex(selectedIndex);
12+
mixin BackupOriginator implements Shapes {
13+
Snapshot backup() {
14+
final buffer = _allocateBuffer();
15+
_writeShapes(buffer);
16+
_writeSelectedIndex(buffer);
17+
return _toSnapshot(buffer);
2418
}
2519

26-
static const _shapeByteSize = 16;
27-
static const _selectedIndexByteSize = 4;
28-
2920
ByteData _allocateBuffer() {
3021
final byteSize = shapes.length * _shapeByteSize + _selectedIndexByteSize;
3122
return ByteData(byteSize);
3223
}
3324

34-
ByteData _fromSnapshotToByteData(Snapshot snapshot) {
35-
final unBase = Base64Decoder().convert(snapshot);
36-
final byteData = ByteData.sublistView(unBase);
37-
return byteData;
38-
}
39-
4025
void _writeSelectedIndex(ByteData data) {
4126
late final int selectedIndex;
4227

@@ -59,12 +44,35 @@ mixin Originator implements Shapes {
5944
..setFloat32(byteOffset + 4, shape.y)
6045
..setInt32(byteOffset + 8, shape.color.value)
6146
..setFloat32(byteOffset + 12, shape.size);
62-
byteOffset += 16;
47+
byteOffset += _shapeByteSize;
6348
}
6449

6550
return byteOffset;
6651
}
6752

53+
Snapshot _toSnapshot(ByteData data) {
54+
return Base64Encoder().convert(
55+
data.buffer.asUint8List(),
56+
);
57+
}
58+
}
59+
60+
mixin RecoveryOriginator implements Shapes {
61+
void restore(Snapshot snapshot) {
62+
final byteData = _fromSnapshotToByteData(snapshot);
63+
final newShapes = _readShapes(byteData);
64+
final selectedIndex = _readSelectedIndex(byteData);
65+
shapes.clear();
66+
shapes.addAll(newShapes);
67+
selectByIndex(selectedIndex);
68+
}
69+
70+
ByteData _fromSnapshotToByteData(Snapshot snapshot) {
71+
final unBase = Base64Decoder().convert(snapshot);
72+
final byteData = ByteData.sublistView(unBase);
73+
return byteData;
74+
}
75+
6876
int _getNumberOfShapes(ByteData byteData) {
6977
return (byteData.lengthInBytes - _selectedIndexByteSize) ~/ _shapeByteSize;
7078
}
@@ -82,7 +90,7 @@ mixin Originator implements Shapes {
8290
byteData.getFloat32(byteOffset + 12),
8391
);
8492
shapes.add(shape);
85-
byteOffset += 16;
93+
byteOffset += _shapeByteSize;
8694
}
8795

8896
return shapes;
@@ -91,10 +99,4 @@ mixin Originator implements Shapes {
9199
int _readSelectedIndex(ByteData byteData) {
92100
return byteData.getInt32(byteData.lengthInBytes - _selectedIndexByteSize);
93101
}
94-
95-
Snapshot _toSnapshot(ByteData data) {
96-
return Base64Encoder().convert(
97-
data.buffer.asUint8List(),
98-
);
99-
}
100102
}

0 commit comments

Comments
 (0)