Skip to content

Commit 792c910

Browse files
author
Badacadabra
committed
Add Memento (ES5 + ES6 + CoffeeScript + TypeScript)
1 parent d48b3db commit 792c910

File tree

7 files changed

+299
-0
lines changed

7 files changed

+299
-0
lines changed
2.07 KB
Binary file not shown.
21.4 KB
Loading

doc/GoF/Behavioral/Memento/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Synopsis
2+
3+
I found an interesting article on the Web and I would like to save it in my browser for later reading. For some reasons, I always use private browsing.
4+
5+
# Problem
6+
7+
With private browsing, history is not kept by the browser. Of course we can use back and forward buttons to navigate in the current history during the browsing session, but with this technique, it would not be possible to identify easily our favorite webpages. Furthermore, interesting webpages would not be there anymore if we close the browser and open it again.
8+
9+
# Solution
10+
11+
Here we have to use bookmarks to save interesting webpages and the Memento pattern allows us to simulate that behavior. Implementing this pattern requires:
12+
13+
* A concrete "originator" (the browser) which is responsible for the action
14+
* A concrete "memento" (a bookmark) which keeps the link to the interesting webpage
15+
* A concrete "caretaker" (the bookmarks manager) which keeps a list of bookmarks
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# ==============================
2+
# ORIGINATOR
3+
# ==============================
4+
5+
class Browser
6+
constructor: (@_currentPage) ->
7+
8+
saveCurrentPage: ->
9+
new Bookmark(@_currentPage)
10+
11+
getCurrentPage: ->
12+
@_currentPage
13+
14+
setCurrentPage: (@_currentPage) ->
15+
16+
# ==============================
17+
# MEMENTO
18+
# ==============================
19+
20+
class Bookmark
21+
constructor: (webpage) ->
22+
@_webpage = webpage
23+
24+
getPage: ->
25+
@_webpage
26+
27+
# ==============================
28+
# CARETAKER
29+
# ==============================
30+
31+
class BookmarksManager
32+
constructor: ->
33+
@_bookmarks = []
34+
35+
addBookmark: (bookmark) ->
36+
@_bookmarks.push(bookmark)
37+
38+
getBookmark: (index) ->
39+
@_bookmarks[index]
40+
41+
# ==============================
42+
# CLIENT CODE
43+
# ==============================
44+
45+
browser = new Browser "http://www.badacadabra.net"
46+
bookmark = browser.saveCurrentPage()
47+
bookmarksManager = new BookmarksManager
48+
49+
bookmarksManager.addBookmark bookmark
50+
browser.setCurrentPage "https://www.google.com"
51+
browser.setCurrentPage "https://www.amazon.com"
52+
bookmark = browser.saveCurrentPage()
53+
bookmarksManager.addBookmark bookmark
54+
browser.setCurrentPage "https://www.twitter.com"
55+
56+
console.log "Current page: #{browser.getCurrentPage()}"
57+
console.log "Latest bookmark: #{bookmark.getPage()}"
58+
console.log "First bookmark: #{bookmarksManager.getBookmark(0).getPage()}"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// ==============================
2+
// ORIGINATOR
3+
// ==============================
4+
5+
class Browser {
6+
private currentPage: string;
7+
8+
constructor(currentPage: string) {
9+
this.currentPage = currentPage;
10+
}
11+
12+
public saveCurrentPage(): Bookmark {
13+
return new Bookmark(this.currentPage);
14+
}
15+
16+
public getCurrentPage(): string {
17+
return this.currentPage;
18+
}
19+
20+
public setCurrentPage(webpage: string): void {
21+
this.currentPage = webpage;
22+
}
23+
}
24+
25+
// ==============================
26+
// MEMENTO
27+
// ==============================
28+
29+
class Bookmark {
30+
private webpage: string;
31+
32+
constructor(webpage: string) {
33+
this.webpage = webpage;
34+
}
35+
36+
public getPage(): string {
37+
return this.webpage;
38+
}
39+
}
40+
41+
// ==============================
42+
// CARETAKER
43+
// ==============================
44+
45+
class BookmarksManager {
46+
private bookmarks: Bookmark[] = [];
47+
48+
public addBookmark(bookmark: Bookmark): void {
49+
this.bookmarks.push(bookmark);
50+
}
51+
52+
public getBookmark(index: number): Bookmark {
53+
return this.bookmarks[index];
54+
}
55+
}
56+
57+
// ==============================
58+
// CLIENT CODE
59+
// ==============================
60+
61+
let browser: Browser = new Browser("http://www.badacadabra.net"),
62+
bookmark: Bookmark = browser.saveCurrentPage(),
63+
bookmarksManager: BookmarksManager = new BookmarksManager();
64+
65+
bookmarksManager.addBookmark(bookmark);
66+
browser.setCurrentPage("https://www.google.com");
67+
browser.setCurrentPage("https://www.amazon.com");
68+
bookmark = browser.saveCurrentPage();
69+
bookmarksManager.addBookmark(bookmark);
70+
browser.setCurrentPage("https://www.twitter.com");
71+
72+
console.log("Current page: " + browser.getCurrentPage());
73+
console.log("Latest bookmark: " + bookmark.getPage());
74+
console.log("First bookmark: " + bookmarksManager.getBookmark(0).getPage());
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
// ==============================
4+
// ORIGINATOR
5+
// ==============================
6+
7+
var Browser = (function() {
8+
function Browser(currentPage) {
9+
this._currentPage = currentPage;
10+
}
11+
12+
Browser.prototype.saveCurrentPage = function () {
13+
return new Bookmark(this._currentPage);
14+
};
15+
16+
Browser.prototype.getCurrentPage = function () {
17+
return this._currentPage;
18+
};
19+
20+
Browser.prototype.setCurrentPage = function (webpage) {
21+
this._currentPage = webpage;
22+
};
23+
24+
return Browser;
25+
})();
26+
27+
// ==============================
28+
// MEMENTO
29+
// ==============================
30+
31+
var Bookmark = (function () {
32+
function Bookmark(webpage) {
33+
this._webpage = webpage;
34+
}
35+
36+
Bookmark.prototype.getPage = function () {
37+
return this._webpage;
38+
};
39+
40+
return Bookmark;
41+
})();
42+
43+
// ==============================
44+
// CARETAKER
45+
// ==============================
46+
47+
var BookmarksManager = (function () {
48+
var bookmarks = [];
49+
50+
function BookmarksManager() {}
51+
52+
BookmarksManager.prototype.addBookmark = function (bookmark) {
53+
bookmarks.push(bookmark);
54+
};
55+
56+
BookmarksManager.prototype.getBookmark = function (index) {
57+
return bookmarks[index];
58+
};
59+
60+
return BookmarksManager;
61+
})();
62+
63+
// ==============================
64+
// CLIENT CODE
65+
// ==============================
66+
67+
var browser = new Browser("http://www.badacadabra.net"),
68+
bookmark = browser.saveCurrentPage(),
69+
bookmarksManager = new BookmarksManager();
70+
71+
bookmarksManager.addBookmark(bookmark);
72+
browser.setCurrentPage("https://www.google.com");
73+
browser.setCurrentPage("https://www.amazon.com");
74+
bookmark = browser.saveCurrentPage();
75+
bookmarksManager.addBookmark(bookmark);
76+
browser.setCurrentPage("https://www.twitter.com");
77+
78+
console.log("Current page: " + browser.getCurrentPage());
79+
console.log("Latest bookmark: " + bookmark.getPage());
80+
console.log("First bookmark: " + bookmarksManager.getBookmark(0).getPage());
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// ==============================
2+
// ORIGINATOR
3+
// ==============================
4+
5+
class Browser {
6+
constructor(currentPage) {
7+
this._currentPage = currentPage;
8+
}
9+
10+
saveCurrentPage() {
11+
return new Bookmark(this._currentPage);
12+
}
13+
14+
getCurrentPage() {
15+
return this._currentPage;
16+
}
17+
18+
setCurrentPage(webpage) {
19+
this._currentPage = webpage;
20+
}
21+
}
22+
23+
// ==============================
24+
// MEMENTO
25+
// ==============================
26+
27+
class Bookmark {
28+
constructor(webpage) {
29+
this._webpage = webpage;
30+
}
31+
32+
getPage() {
33+
return this._webpage;
34+
}
35+
}
36+
37+
// ==============================
38+
// CARETAKER
39+
// ==============================
40+
41+
class BookmarksManager {
42+
constructor() {
43+
this._bookmarks = [];
44+
}
45+
46+
addBookmark() {
47+
this._bookmarks.push(bookmark);
48+
}
49+
50+
getBookmark(index) {
51+
return this._bookmarks[index];
52+
}
53+
}
54+
55+
// ==============================
56+
// CLIENT CODE
57+
// ==============================
58+
59+
let browser = new Browser("http://www.badacadabra.net"),
60+
bookmark = browser.saveCurrentPage(),
61+
bookmarksManager = new BookmarksManager();
62+
63+
bookmarksManager.addBookmark(bookmark);
64+
browser.setCurrentPage("https://www.google.com");
65+
browser.setCurrentPage("https://www.amazon.com");
66+
bookmark = browser.saveCurrentPage();
67+
bookmarksManager.addBookmark(bookmark);
68+
browser.setCurrentPage("https://www.twitter.com");
69+
70+
console.log("Current page: " + browser.getCurrentPage());
71+
console.log("Latest bookmark: " + bookmark.getPage());
72+
console.log("First bookmark: " + bookmarksManager.getBookmark(0).getPage());

0 commit comments

Comments
 (0)