Skip to content

Commit 5827beb

Browse files
author
Badacadabra
committed
Add Proxy (ES5 + ES6 + CoffeeScript)
1 parent c0bcc51 commit 5827beb

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

doc/GoF/Structural/Proxy/Proxy.dia

1.93 KB
Binary file not shown.

doc/GoF/Structural/Proxy/Proxy.png

13.7 KB
Loading

doc/GoF/Structural/Proxy/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Synopsis
2+
3+
I need some cash, so I have to find an ATM (Automated Teller Machine).
4+
5+
# Problem
6+
7+
Money withdrawal is under control. Everyone has a personal bank account which is only accessible with a specific credit card and a related code.
8+
9+
For this reason, we cannot interact directly with the "real" object (our bank account). We must interact with another object that will be responsible for the communication with the real object.
10+
11+
# Solution
12+
13+
We could consider an ATM as a proxy whose aim is to verify access to a bank account. Here the Proxy design pattern requires:
14+
15+
* An abstract representation of a financial entity (abstract class or interface)
16+
* A concrete bank account which allows some operations
17+
* A proxy (ATM) which secures these operations and delegates when necessary
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict"
2+
3+
# ==============================
4+
# ABSTRACT FINANCIAL ENTITY
5+
# ==============================
6+
7+
class FinancialEntity
8+
constructor: ->
9+
throw new Error "You cannot instantiate an abstract class!" if @constructor is FinancialEntity
10+
11+
withdrawMoney: ->
12+
throw new Error "You cannot call an abstract method!"
13+
14+
# ==============================
15+
# CONCRETE BANK ACCOUNT
16+
# ==============================
17+
18+
class BankAccount extends FinancialEntity
19+
withdrawMoney: ->
20+
"Enjoy the cash!"
21+
22+
# ==============================
23+
# CONCRETE ATM
24+
# ==============================
25+
26+
class ATM extends FinancialEntity
27+
constructor: (@_bankAccount) ->
28+
29+
withdrawMoney: (code) ->
30+
throw type: "AuthenticationException", message: "Invalid code" if code isnt 1337
31+
@_bankAccount.withdrawMoney()
32+
33+
# ==============================
34+
# CLIENT CODE
35+
# ==============================
36+
37+
bankAccount = new BankAccount
38+
proxy = new ATM bankAccount
39+
40+
try
41+
console.log proxy.withdrawMoney 1234 # Wrong code
42+
catch e
43+
console.log "#{e.type} - #{e.message}" # AuthenticationException - Invalid code
44+
finally
45+
console.log proxy.withdrawMoney 1337 # Enjoy the cash!
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
// ==============================
4+
// ABSTRACT FINANCIAL ENTITY
5+
// ==============================
6+
7+
var FinancialEntity = (function () {
8+
function FinancialEntity() {
9+
if (this.constructor === FinancialEntity) {
10+
throw new Error("You cannot instantiate an abstract class!");
11+
}
12+
}
13+
14+
FinancialEntity.prototype.withdrawMoney = function () {
15+
throw new Error("You cannot call an abstract method!");
16+
};
17+
18+
return FinancialEntity;
19+
})();
20+
21+
// ==============================
22+
// CONCRETE BANK ACCOUNT
23+
// ==============================
24+
25+
var BankAccount = (function () {
26+
function BankAccount() {}
27+
BankAccount.prototype = Object.create(FinancialEntity.prototype);
28+
BankAccount.prototype.constructor = BankAccount;
29+
30+
BankAccount.prototype.withdrawMoney = function () {
31+
return "Enjoy the cash!";
32+
};
33+
34+
return BankAccount;
35+
})();
36+
37+
// ==============================
38+
// CONCRETE ATM
39+
// ==============================
40+
41+
var ATM = (function () {
42+
var bankAccount;
43+
44+
function ATM(account) {
45+
bankAccount = account;
46+
}
47+
ATM.prototype = Object.create(FinancialEntity.prototype);
48+
ATM.prototype.constructor = ATM;
49+
50+
ATM.prototype.withdrawMoney = function (code) {
51+
if (code !== 1337) {
52+
throw {
53+
type: "AuthenticationException",
54+
message: "Invalid code"
55+
};
56+
}
57+
return bankAccount.withdrawMoney();
58+
};
59+
60+
return ATM;
61+
})();
62+
63+
// ==============================
64+
// CLIENT CODE
65+
// ==============================
66+
67+
var bankAccount = new BankAccount(),
68+
proxy = new ATM(bankAccount);
69+
70+
try {
71+
console.log(proxy.withdrawMoney(1234)); // Wrong code
72+
} catch (e) {
73+
console.log(e.type + " - " + e.message); // AuthenticationException - Invalid code
74+
} finally {
75+
console.log(proxy.withdrawMoney(1337)); // Enjoy the cash!
76+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ==============================
2+
// ABSTRACT FINANCIAL ENTITY
3+
// ==============================
4+
5+
class FinancialEntity {
6+
constructor() {
7+
if (new.target !== undefined) {
8+
throw new Error("You cannot instantiate an abstract class!");
9+
}
10+
}
11+
12+
withdrawMoney() {
13+
throw new Error("You cannot call an abstract method!");
14+
}
15+
}
16+
17+
// ==============================
18+
// CONCRETE BANK ACCOUNT
19+
// ==============================
20+
21+
class BankAccount extends FinancialEntity {
22+
withdrawMoney() {
23+
return "Enjoy the cash!";
24+
}
25+
}
26+
27+
// ==============================
28+
// CONCRETE ATM
29+
// ==============================
30+
31+
class ATM extends FinancialEntity {
32+
constructor(bankAccount) {
33+
super();
34+
this._bankAccount = bankAccount;
35+
}
36+
37+
withdrawMoney(code) {
38+
if (code !== 1337) {
39+
throw {
40+
type: "AuthenticationException",
41+
message: "Invalid code"
42+
};
43+
}
44+
return this._bankAccount.withdrawMoney();
45+
}
46+
}
47+
48+
// ==============================
49+
// CLIENT CODE
50+
// ==============================
51+
52+
let bankAccount = new BankAccount(),
53+
proxy = new ATM(bankAccount);
54+
55+
try {
56+
console.log(proxy.withdrawMoney(1234)); // Wrong code
57+
} catch (e) {
58+
console.log(`${e.type} - ${e.message}`); // AuthenticationException - Invalid code
59+
} finally {
60+
console.log(proxy.withdrawMoney(1337)); // Enjoy the cash!
61+
}

0 commit comments

Comments
 (0)