Skip to content

Commit aa91c9f

Browse files
author
Tomas Corral
committed
Add Bridge Design Pattern
1 parent c3f1051 commit aa91c9f

File tree

11 files changed

+128
-0
lines changed

11 files changed

+128
-0
lines changed

Bridge/index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<html>
2+
<head>
3+
<link type="text/css" rel="stylesheet" href="../statics/css/style.css"/>
4+
<title>Bridge Pattern</title>
5+
</head>
6+
<body>
7+
<div id="source">
8+
<h2>Source</h2>
9+
<pre>
10+
import GreyColor from './GreyColor';
11+
import FamilyCar from './FamilyCar';
12+
import MatteBlackColor from './MatteBlackColor';
13+
import Adventure4x4Car from './Adventure4X4Car';
14+
import RedColor from './RedColor';
15+
import UrbanCar from './UrbanCar';
16+
17+
const familyCar = new FamilyCar(new GreyColor());
18+
const adventureCar = new Adventure4x4Car(new MatteBlackColor());
19+
const urbanCar = new UrbanCar(new RedColor());
20+
21+
familyCar.applyColor();
22+
adventureCar.applyColor();
23+
urbanCar.applyColor();
24+
</pre>
25+
</div>
26+
<div id="console">
27+
<h2>Console</h2>
28+
<ul></ul>
29+
<h1>BRIDGE</h1>
30+
</div>
31+
32+
33+
<script type="text/javascript" src="../statics/js/utils.js"></script>
34+
<script type="text/javascript" src="dist/scripts/main.js"></script>
35+
</body>
36+
</html>

Bridge/scripts/Adventure4X4Car.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Car from './Car';
2+
3+
class Adventure4x4Car extends Car {
4+
constructor(color) {
5+
super('4x4 Adventure car', 'For people that does not care about existing paths', 55000, 2, color);
6+
}
7+
}
8+
9+
export default Adventure4x4Car;

Bridge/scripts/Car.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Car {
2+
constructor(name, description, price, places = 2, color, brand = 'Cartisfaction') {
3+
this.brand = brand;
4+
this.name = name;
5+
this.description = description;
6+
this.price = price;
7+
this.places = places;
8+
this.color = color;
9+
}
10+
applyColor() {
11+
console.log(`${this.name} car painted with color ${this.color.applyColor()}`);
12+
}
13+
}
14+
15+
export default Car;

Bridge/scripts/Color.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Color {
2+
applyColor() {
3+
throw new Error('This method should be overwritten');
4+
}
5+
}
6+
7+
export default Color;

Bridge/scripts/FamilyCar.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Car from './Car';
2+
3+
class FamilyCar extends Car {
4+
constructor(color) {
5+
super('Family car', 'Enjoy with your family', 30000, 5, color);
6+
}
7+
}
8+
9+
export default FamilyCar;

Bridge/scripts/GreyColor.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Color from './Color';
2+
3+
class GreyColor extends Color {
4+
applyColor() {
5+
return 'grey';
6+
}
7+
}
8+
9+
export default GreyColor;

Bridge/scripts/MatteBlackColor.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Color from './Color';
2+
3+
class MatteBlackColor extends Color {
4+
applyColor() {
5+
return 'matte black';
6+
}
7+
}
8+
9+
export default MatteBlackColor;

Bridge/scripts/RedColor.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Color from './Color';
2+
3+
class RedColor extends Color {
4+
applyColor() {
5+
return 'red';
6+
}
7+
}
8+
9+
export default RedColor;

Bridge/scripts/UrbanCar.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Car from './Car';
2+
3+
class UrbanCar extends Car {
4+
constructor(color) {
5+
super('Urban car', 'Small and designed for the city', 12000, 2, color);
6+
}
7+
}
8+
9+
export default UrbanCar;

Bridge/scripts/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import GreyColor from './GreyColor';
2+
import FamilyCar from './FamilyCar';
3+
import MatteBlackColor from './MatteBlackColor';
4+
import Adventure4x4Car from './Adventure4X4Car';
5+
import RedColor from './RedColor';
6+
import UrbanCar from './UrbanCar';
7+
8+
const familyCar = new FamilyCar(new GreyColor());
9+
const adventureCar = new Adventure4x4Car(new MatteBlackColor());
10+
const urbanCar = new UrbanCar(new RedColor());
11+
12+
familyCar.applyColor();
13+
adventureCar.applyColor();
14+
urbanCar.applyColor();

0 commit comments

Comments
 (0)