Created
March 10, 2024 17:27
-
-
Save fredgrott/ffc579d92578ca52a6b384cb4e007167 to your computer and use it in GitHub Desktop.
dart oop behavioral, memento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:memento/care_taker.dart'; | |
import 'package:memento/originator.dart'; | |
void main() { | |
var me = Originator("Returned from store"); | |
me.state = "Placing car keys down"; | |
var locationOfKeys = me.saveToMemento(); | |
var memory = CareTaker(); | |
memory.memento = locationOfKeys; | |
me.state = "Putting away groceries"; | |
me.state = "Noticed missing pickles"; | |
me.state = "Looking for car keys..."; | |
me.restoreFromMemento(memory.memento); | |
me.state = "Going back to store for pickles"; | |
/* | |
[Originator] Set state to Returned from store | |
[Originator] Set state to Placing car keys down | |
[Originator] Saving to memento... | |
[Memento] State "Placing car keys down" has been saved! | |
[Originator] Set state to Putting away groceries | |
[Originator] Set state to Noticed missing pickles | |
[Originator] Set state to Looking for car keys... | |
[Originator] Restoring from memento... | |
[Memento] Providing saved state "Placing car keys down"... | |
[Originator] Restored to Placing car keys down. | |
[Originator] Set state to Going back to store for pickles | |
*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:memento/memento.dart'; | |
class CareTaker { | |
late Memento memento; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Memento { | |
late String _state; | |
Memento(String s) { | |
_state = s; | |
print("[Memento] State \"$s\" has been saved!"); | |
} | |
String get state { | |
print("[Memento] Providing saved state \"$_state\"..."); | |
return _state; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:memento/memento.dart'; | |
class Originator { | |
late String _state; | |
// NOTE: This uses the state setter on init to get a handy print | |
Originator(String s) { | |
state = s; | |
} | |
String get state => _state; | |
set state(String newState) { | |
_state = newState; | |
print("[Originator] Set state to $newState"); | |
} | |
Memento saveToMemento() { | |
print("[Originator] Saving to memento..."); | |
return Memento(state); | |
} | |
void restoreFromMemento(Memento m) { | |
print("[Originator] Restoring from memento..."); | |
_state = m.state; | |
print("[Originator] Restored to $state."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment