Originator.dart 737 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import 'package:gobang/flyweight/Position.dart';
  2. import 'package:gobang/memorandum/CareTaker.dart';
  3. import 'Memo.dart';
  4. class Originator {
  5. Originator._();
  6. static Originator? _originator;
  7. static getInstance(){
  8. if(_originator == null){
  9. _originator = Originator._();
  10. }
  11. return _originator;
  12. }
  13. List<Position> _state = [];
  14. List<Position> get state => _state;
  15. CareTaker _careTaker = CareTaker();
  16. add(Position position) {
  17. _state.add(position);
  18. _careTaker.add(_save());
  19. }
  20. _from(Memo memo) {
  21. this._state = memo.state;
  22. }
  23. Memo _save() {
  24. return Memo()..state = this._state;
  25. }
  26. clean(){
  27. _state = [];
  28. }
  29. undo(){
  30. Memo memo = _careTaker.getLast();
  31. _from(memo);
  32. }
  33. }