Skip to content

Commit 8812f7b

Browse files
author
Hassan Azimi
committed
Add all files
0 parents  commit 8812f7b

File tree

23 files changed

+716
-0
lines changed

23 files changed

+716
-0
lines changed

AdapterPattern/Adapter.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// OLD INTERFACE
2+
class Shipping {
3+
request(zipStart, zipEnd, weight) {
4+
// ...
5+
6+
return "$50.00";
7+
}
8+
}
9+
10+
// NEW INTERFACE
11+
class AdvancedShipping {
12+
login(credentials) {
13+
// ...
14+
}
15+
setStart(start) {
16+
// ...
17+
}
18+
setDestination(destination) {
19+
// ...
20+
}
21+
calculate(weight) {
22+
return "$80.00";
23+
}
24+
}
25+
26+
class ShippingAdapter {
27+
constructor(credentials) {
28+
this.credentials = credentials;
29+
this.shipping = new AdvancedShipping();
30+
this.shipping.login(this.credentials);
31+
}
32+
33+
request(zipStart, zipEnd, weight) {
34+
this.shipping.setStart(zipStart);
35+
this.shipping.setDestination(zipEnd);
36+
return this.shipping.calculate(weight);
37+
}
38+
}
39+
40+
const shipping = new Shipping();
41+
const credentials = { token: "asdasd" };
42+
const adapter = new ShippingAdapter(credentials);
43+
44+
// original shipping object and interface
45+
46+
const oldCost = shipping.request("78712", "10012", "2kg");
47+
console.log('Old Cost: ' + oldCost); // $50.00
48+
49+
// new shipping object with adapted interface
50+
const newCost = adapter.request("78712", "10012", "2kg");
51+
console.log('New Cost: ' + newCost);

BridgePattern/Bridge.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// input devices
2+
class Gesture {
3+
constructor(output) {
4+
this.output = output;
5+
}
6+
tap() {
7+
this.output.click();
8+
}
9+
swipe() {
10+
this.output.move();
11+
}
12+
pan() {
13+
this.output.drag();
14+
}
15+
pinch() {
16+
this.output.zoom();
17+
}
18+
}
19+
20+
class Mouse {
21+
constructor(output) {
22+
this.output = output;
23+
}
24+
click() {
25+
this.output.click();
26+
}
27+
move() {
28+
this.output.move();
29+
}
30+
down() {
31+
this.output.drag();
32+
}
33+
wheel() {
34+
this.output.zoom();
35+
}
36+
}
37+
// output devices
38+
39+
class Screen {
40+
click() {
41+
console.log('Screen Clicked');
42+
}
43+
move() {
44+
console.log('Screen moved');
45+
}
46+
drag() {
47+
console.log('Screen dragged');
48+
}
49+
zoom() {
50+
console.log('Screen zoomed');
51+
}
52+
}
53+
54+
class Audio {
55+
click() {
56+
console.log('Audio Clicked');
57+
}
58+
move() {
59+
console.log('Audio waved');
60+
}
61+
drag() {
62+
console.log('Audio volume down');
63+
}
64+
zoom() {
65+
console.log('Audio volume up');
66+
}
67+
}
68+
69+
const screen = new Screen();
70+
const audio = new Audio();
71+
72+
const hand = new Gesture(screen);
73+
const mouse = new Mouse(audio);
74+
75+
// Screen
76+
hand.tap();
77+
hand.swipe();
78+
hand.pinch();
79+
80+
// Audio
81+
mouse.click();
82+
mouse.move();
83+
mouse.down();

BuilderPattern/Builder.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class Shop {
2+
build(builder) {
3+
builder.step1()
4+
builder.step2()
5+
return builder.get()
6+
}
7+
}
8+
9+
class CarBuilder {
10+
constructor() {
11+
this.car = null
12+
}
13+
step1() {
14+
this.car = new Car()
15+
}
16+
17+
step2() {
18+
this.car.addParts()
19+
}
20+
21+
get() {
22+
return this.car
23+
}
24+
}
25+
26+
class TruckBuilder {
27+
constructor() {
28+
this.truck = null
29+
}
30+
step1() {
31+
this.truck = new Truck()
32+
}
33+
34+
step2() {
35+
this.truck.addParts()
36+
}
37+
38+
get() {
39+
return this.truck
40+
}
41+
}
42+
43+
class Car {
44+
constructor() {
45+
this.doors = 0;
46+
}
47+
48+
addParts() {
49+
this.doors = 4;
50+
}
51+
52+
say() {
53+
console.log(`I have ${this.doors} doors`);
54+
}
55+
}
56+
57+
58+
class Truck {
59+
constructor() {
60+
this.doors = 0;
61+
}
62+
63+
addParts() {
64+
this.doors = 2;
65+
}
66+
67+
say() {
68+
console.log(`I have ${this.doors} doors`);
69+
}
70+
}
71+
72+
const shop = new Shop();
73+
74+
const carBuilder = new CarBuilder();
75+
const truckBuilder = new TruckBuilder();
76+
77+
const car = shop.build(carBuilder);
78+
const truck = shop.build(truckBuilder);
79+
80+
car.say();
81+
truck.say();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class Checker {
2+
setSuccessor(successor) {
3+
this.successor = successor;
4+
}
5+
6+
check(status) {
7+
throw new Error('You should implement this method');
8+
}
9+
10+
next(status) {
11+
if (this.successor) {
12+
this.successor.check(status);
13+
}
14+
}
15+
}
16+
17+
class Lock extends Checker {
18+
check(status) {
19+
if (!status.locked) {
20+
// throw new Error('The door is not locked');
21+
console.error('The door is not locked');
22+
}
23+
super.next(status);
24+
}
25+
}
26+
27+
class Alarm extends Checker {
28+
check(status) {
29+
if (!status.alarmOn) {
30+
// throw new Error('The alarm is not on);
31+
console.error('The alarm is not on');
32+
}
33+
super.next(status);
34+
}
35+
}
36+
37+
class Light extends Checker {
38+
check(status) {
39+
if (!status.lightOff) {
40+
// throw new Error('TThe light is not off');
41+
console.error('The light is not off');
42+
}
43+
super.next(status);
44+
}
45+
}
46+
47+
48+
class Status {
49+
constructor() {
50+
this.locked = true;
51+
this.alarmOn = true;
52+
this.lightOff = true;
53+
}
54+
}
55+
56+
57+
const lock = new Lock();
58+
const alarm = new Alarm();
59+
const light = new Light();
60+
61+
lock.setSuccessor(alarm);
62+
alarm.setSuccessor(light);
63+
64+
const status = new Status();
65+
lock.check(status);
66+
// alarm.check(status);
67+
// light.check(status);

DecoratorPattern/Decorator.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class User {
2+
constructor(name) {
3+
this.name = name;
4+
}
5+
getName() {
6+
return this.name;
7+
}
8+
9+
say() {
10+
console.log(`My name is ${this.name}`);
11+
}
12+
}
13+
14+
class DecorateUser {
15+
constructor(user, street, city) {
16+
this.user = user;
17+
this.street = street;
18+
this.city = city;
19+
this.name = user.name;
20+
}
21+
getName() {
22+
return this.user.getName();
23+
}
24+
25+
say() {
26+
console.log(`Decorated User: ${this.name}, ${this.street}, ${this.city}.`);
27+
}
28+
}
29+
30+
const user = new User('Amir');
31+
user.say();
32+
33+
const decorated = new DecorateUser(user, '123 Main Street', 'London');
34+
decorated.say();

FacadePattern/Facade.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Mortgage {
2+
constructor(name) {
3+
this.name = name;
4+
}
5+
6+
apply(amount) {
7+
let result = 'approved';
8+
9+
if (!new Bank().get(this.name, amount)) {
10+
result = 'declined';
11+
} else if (!new Credit().get(this.name)) {
12+
result = 'declined';
13+
} else if (!new Background().get(this.name)) {
14+
result = 'declined';
15+
}
16+
17+
return this.name + ' has been ' + result + ' for a ' + amount + ' mortgage';
18+
}
19+
}
20+
21+
class Bank {
22+
get(name, amount) {
23+
// ...
24+
return true;
25+
}
26+
}
27+
28+
class Credit {
29+
get(name) {
30+
// ...
31+
return true;
32+
}
33+
}
34+
35+
class Background {
36+
get(name) {
37+
// ...
38+
return true;
39+
}
40+
}
41+
42+
const mortgage = new Mortgage('John');
43+
const result = mortgage.apply(300000);
44+
45+
console.log(result);

0 commit comments

Comments
 (0)