Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Class representing an originator (instance where content will be saved).
- * @class
- */
- class Originator {
- /**
- * Create an originator.
- */
- constructor(content) {
- this._content = content;
- }
- /**
- * Get the originator content.
- * @return {array} The originator content value.
- */
- get content() {
- return this._content;
- }
- }
- /**
- * Class representing a caretaker (instance where content will be saved).
- * @class
- */
- class CareTaker {
- /**
- * Create an careTaker.
- */
- constructor() {
- this._content = [];
- }
- /**
- * Set the originator content.
- * @param {any} value - some new value to add
- * @return {array} updated caretaker content value.
- */
- setValue(value) {
- this._content.push(value);
- return this._content;
- }
- /**
- * Get current caretaker content
- * @return {array} current caretaker content
- */
- getValues() {
- return this._content;
- }
- /**
- * Return new instance of originator with current content
- * @return {Originator} originator instance
- */
- save() {
- return new Originator(this._content);
- }
- /**
- * Restore caretaker content from originator instance
- * @param {Originator} - originator instance
- */
- restore(originator) {
- this._content = originator.content;
- }
- }
- // ============================================================================
- // USAGE
- // ============================================================================
- const caretaker = new CareTaker();
- caretaker.setValue('First value');
- caretaker.setValue('Second value');
- caretaker.setValue('Third value');
- console.log(caretaker.getValues())
- // ['First value', 'Second value', 'Third value']
- const originator = caretaker.save();
- console.log(originator.content)
- // ['First value', 'Second value', 'Third value']
- caretaker.setValue('Some another value');
- console.log(caretaker.getValues())
- // ['First value', 'Second value', 'Third value', 'Some another value']
- caretaker.restore(originator);
- console.log(caretaker.getValues());
- // ['First value', 'Second value', 'Third value']
Add Comment
Please, Sign In to add comment