Skip to content

Commit b8901ed

Browse files
committed
Move code to ES2015
- Strategy/1 - Strategy/2 - Strategy/2_1 - Strategy/3 - Strategy/4 - Template/withHook - Template/withoutHook - Try-Finally - Check previous design patterns
1 parent 453e252 commit b8901ed

File tree

107 files changed

+1215
-742
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1215
-742
lines changed

Adapter/scripts/TurkeyAdapter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import Duck from './Duck';
22

3+
const MAX_FLIES = 5;
4+
35
class TurkeyAdapter extends Duck {
46
constructor(oTurkey) {
57
super(oTurkey);
68
this.oTurkey = oTurkey;
79
}
810

911
fly() {
10-
for (let index = 0, maxFly = 5; index < maxFly; index++) {
12+
for (let index = 0; index < MAX_FLIES; index++) {
1113
this.oTurkey.fly();
1214
}
1315
}

Chaining/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
<div id="source">
88
<h2>Source</h2>
99
<pre>
10-
var oChainable = new Chainable();
11-
// This must return "21"
12-
oChainable.add(3).add(4).multiply(3).toString();
10+
import Chainable from './Chainable';
11+
12+
let chainable = new Chainable();
13+
14+
console.log(chainable.add(3));
15+
console.log(chainable.add(4));
16+
console.log(chainable.multiply(3));
1317
</pre>
1418
</div>
1519
<div id="console">

Chaining/scripts/main.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Chainable from './Chainable';
22

3-
var oChainable = new Chainable();
4-
// This must return "21"
5-
console.log(oChainable.add(3).add(4).multiply(3).toString());
3+
let chainable = new Chainable();
4+
5+
console.log(chainable.add(3));
6+
console.log(chainable.add(4));
7+
console.log(chainable.multiply(3));

Command/1/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<div id="source">
88
<h2>Source</h2>
99
<pre>
10-
import Light from './Light';
11-
import LightOnCommand from './LightOnCommand';
12-
import SimpleRemoteControl from './SimpleRemoteControl';
10+
import Light from '../../common/Light';
11+
import LightOnCommand from '../../common/LightOnCommand';
12+
import SimpleRemoteControl from '../../common/SimpleRemoteControl';
1313

1414
let oSimpleRemote = new SimpleRemoteControl();
1515
let oLight = new Light();

Command/2/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<h2>Source</h2>
99
<pre>
1010
import SimpleRemoteControl from './SimpleRemoteControl';
11-
import Light from './Light';
11+
import Light from '../../common/Light';
1212
import LightOnCommand from './LightOnCommand';
1313

1414
let simpleRemoteControl = new SimpleRemoteControl();

Command/2/scripts/Command.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Command from '../../common/Command';
22

3-
class Command2 extends Command {
3+
class CommandWithUndo extends Command {
44
undo() {
55
throw new Error('This method must be overwritten!');
66
}
77
}
88

9-
export default Command2;
9+
export default CommandWithUndo;

State/1/index.html

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
<html>
22
<head>
3-
<link type="text/css" rel="stylesheet" href="../../css/style.css"/>
3+
<link type="text/css" rel="stylesheet" href="../../statics/css/style.css"/>
44
<title>State Pattern</title>
5-
<script type="text/javascript" src="../../js/jquery.js"></script>
6-
<script type="text/javascript" src="Download.js"></script>
7-
<script type="text/javascript" src="states/State.js"></script>
8-
<script type="text/javascript" src="states/DownloadFailedState.js"></script>
9-
<script type="text/javascript" src="states/DownloadPausedState.js"></script>
10-
<script type="text/javascript" src="states/DownloadedState.js"></script>
11-
<script type="text/javascript" src="states/DownloadingState.js"></script>
12-
<script type="text/javascript" src="states/ReadyState.js"></script>
5+
<script type="text/javascript" src="../../statics/js/jquery.js"></script>
136
</head>
147
<body>
158
<input type="button" value="download" id="download_button"/>
@@ -36,8 +29,7 @@ <h2>Console</h2>
3629
<ul></ul>
3730
<h1>STATE</h1>
3831
</div>
39-
<script type="text/javascript" src="../../js/utils.js"></script>
40-
<script type="text/javascript">
41-
</script>
32+
<script type="text/javascript" src="../../statics/js/utils.js"></script>
33+
<script type="text/javascript" src="dist/scripts/main.js"></script>
4234
</body>
4335
</html>

State/1/scripts/Download.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,43 @@ class Download {
88
constructor() {
99
this.state = new ReadyState(this);
1010
}
11+
1112
setState(state) {
1213
this.state = state;
1314
}
15+
1416
download() {
1517
this.state.download();
1618
}
19+
1720
pause() {
1821
this.state.pause();
1922
}
23+
2024
fail() {
2125
this.state.fail();
2226
}
27+
2328
finish() {
2429
this.state.finish();
2530
}
31+
2632
getReadyState() {
2733
return new ReadyState(this);
2834
}
35+
2936
getDownloadingState() {
3037
return new DownloadingState(this);
3138
}
39+
3240
getDownloadPausedState() {
3341
return new DownloadPausedState(this);
3442
}
43+
3544
getDownloadedState() {
3645
return new DownloadedState(this);
3746
}
47+
3848
getDownloadedFailedState() {
3949
return new DownloadFailedState(this);
4050
}
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import State from './State';
22

3-
var DownloadFailedState = function(oDownload)
4-
{
5-
State.apply(this);
6-
this.oDownload = oDownload;
7-
};
8-
DownloadFailedState.prototype = new State();
9-
DownloadFailedState.prototype.download = function(){
10-
this.oDownload.setState(this.oDownload.getDownloadingState());
11-
console.log("Try to Download again!");
12-
};
13-
DownloadFailedState.prototype.pause = function(){
14-
throw new Error("You can't pause a failed download!");
15-
};
16-
DownloadFailedState.prototype.fail = function(){
17-
throw new Error("A failed download can't fail itself!");
18-
};
19-
DownloadFailedState.prototype.finish = function(){
20-
throw new Error("A failed download is not finished!");
21-
};
3+
class DownloadFailedState extends State {
4+
constructor(download) {
5+
super();
6+
this._download = download;
7+
}
8+
9+
download() {
10+
this._download.setState(this._download.getDownloadingState());
11+
console.log("Try to Download again!");
12+
}
13+
14+
pause() {
15+
throw new Error("You can't pause a failed download!");
16+
}
17+
18+
fail() {
19+
throw new Error("A failed download can't fail itself!");
20+
}
21+
22+
finish() {
23+
throw new Error("A failed download is not finished!");
24+
}
25+
}
26+
27+
export default DownloadFailedState;
Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import State from './State';
22

3-
var DownloadPausedState = function(oDownload)
4-
{
5-
State.apply(this);
6-
this.oDownload = oDownload;
7-
};
8-
DownloadPausedState.prototype = new State();
9-
DownloadPausedState.prototype.download = function(){
10-
this.oDownload.setState(this.oDownload.getDownloadingState());
11-
console.log("Continue Download!");
12-
};
13-
DownloadPausedState.prototype.pause = function(){
14-
throw new Error("You can't pause a paused download!");
15-
};
16-
DownloadPausedState.prototype.fail = function(){
17-
this.oDownload.setState(this.oDownload.getDownloadedFailedState());
18-
console.log("Download has failed!");
19-
};
20-
DownloadPausedState.prototype.finish = function(){
21-
this.oDownload.setState(this.oDownload.getDownloadedState());
22-
console.log("Download has finished!");
23-
};
3+
class DownloadPausedState extends State {
4+
constructor(download) {
5+
super();
6+
this._download = download;
7+
}
8+
9+
download() {
10+
this._download.setState(this._download.getDownloadingState());
11+
console.log("Continue Download!");
12+
}
13+
14+
pause() {
15+
throw new Error("You can't pause a paused download!");
16+
}
17+
18+
fail() {
19+
this._download.setState(this._download.getDownloadedFailedState());
20+
console.log("Download has failed!");
21+
}
22+
23+
finish() {
24+
this._download.setState(this._download.getDownloadedState());
25+
console.log("Download has finished!");
26+
}
27+
}
28+
29+
export default DownloadPausedState;

0 commit comments

Comments
 (0)